簡體   English   中英

cgi-在循環內的變量中傳遞值

[英]cgi - passing values in variables inside a loop

因此,我試圖創建一個在localhost上運行的迷宮游戲,並且下面的代碼幾乎可以正常工作(它可以編譯)。 該程序據說使用按鈕作為移動鍵(上,下,左,右),而我使用解析器來基本上獲取按鈕返回的值。 但是,由於整個過程都是循環的,所以每次我移動(或傳遞數據字符串的值)時,它都會一直返回到px和py(坐標)的初始值。 我的問題是,如何在每個后續循環中覆蓋這些值?

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#define BUFLEN 22

void parse_button(char *data){
    char temp[100];
    int a=0;
    int count=0;
    while(data[count]!='='){
        count++;
    }
    count++;
    while(data[count]!=NULL){
        temp[a]=data[count];
        a++;
        count++;
    }
    strcpy(data,temp);
}

int main()
{
    printf("Content-type:text/html\n\n");
    printf("<html><body>");

    char *data;
    FILE *fp = fopen ("maze.txt", "r"); 
    char maze[20][BUFLEN];
    int i = 0;
    int j = 0;
    int end = 0;
    int px = 1;
    int py = 0;



    data = getenv("QUERY_STRING");
    if(data){
        printf("%s", data);
    }
    parse_button(data);
    printf("<br>%s",data); //for checking if parser works

這是迷宮的主要算法:

    while(end!=1){
        printf("<table>");
        printf("<tr>");
            for (i=0; i<20; i++)    {
                fgets(maze[i], 22, fp);
            }

            for (i=0; i<20; i++){                           
                for (j=0; j<20; j++){       
                    printf("<td>");                 
                    printf("%c",maze[i][j]);                            
                    printf("</td>");                
                }
            printf("</tr>");        
            }
        printf("</table>");
        fclose(fp);
        if (!strcmp(data,"UP")){
            if (maze[px-1][py]=='X'){
                end++;
                printf("Nice! You've finished the maze! Congratulations!\n");
            }else if(maze[px-1][py]==' '){
                maze[px-1][py]='P';
                maze[px][py]=' ';               
                px--;   
            }else if(maze[px-1][py]=='*'){
                printf("Ouch! You've hit a wall! Try again!\n");
            }else
                break;
        }else if (!strcmp(data,"DOWN")){
            if (maze[px+1][py]=='X'){
                end++;  
                printf("Nice! You've finished the maze! Congratulations!\n");
            }else if(maze[px+1][py]==' '){
                maze[px+1][py]='P';
                maze[px][py]=' ';               
                px++;   
            }else if(maze[px+1][py]=='*'){
                printf("Ouch! You've hit a wall! Try again!\n");
            }else
                    break;
        }else if (!strcmp(data,"LEFT")){
            if (maze[px][py-1]=='X'){
                end++;
                printf("Nice! You've finished the maze! Congratulations!\n");
            }else if(maze[px][py-1]==' '){
                maze[px][py-1]='P';
                maze[px][py]=' ';               
                py--;   
            }else if(maze[px][py-1]=='*'){
                printf("Ouch! You've hit a wall! Try again!\n");
            }
        }else if (!strcmp(data,"RIGHT")){
            if (maze[px][py+1]=='X'){
                end++;
                printf("Nice! You've finished the maze! Congratulations!\n");
            }else if(maze[px][py+1]==' '){
                maze[px][py+1]='P';
                maze[px][py]=' ';               
                py++;   
            }else if(maze[px][py+1]=='*'){
                printf("Ouch! You've hit a wall! Try again!\n");
            }else
                break;      
        }else{
            printf("Invalid input! Enter w/s/a/d only!\n");
        }   
            printf("<form action=\"http://localhost/cgi-bin/maze.cgi\">");
    printf("<input type=submit name=\"button\" value='UP'>");
    printf("<input type=submit name=\"button\" value='DOWN'>");
    printf("<input type=submit name=\"button\" value='LEFT'>");
    printf("<input type=submit name=\"button\" value='RIGHT'>");
    printf("</form>");
    printf("</body></html>");
    }


    return 0;
}

每當用戶按下按鈕時,CGI將啟動該程序的新副本。 因此,您需要在外部將游戲狀態存儲在文件或數據庫中。

您還需要一種識別用戶的方法,以便可以檢索該用戶的游戲狀態。 一種方法是使用會話cookie。 創建和檢索會話Cookie的方式取決於您所使用的Web服務器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM