cgi - 在循环内的变量中传递值

时间:2015-11-05 17:34:03

标签: c loops cgi

所以我试图创建一个在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;
}

1 个答案:

答案 0 :(得分:0)

每次用户按下按钮时,CGI都会启动该程序的新副本。因此,您需要在外部存储游戏状态,即在文件或数据库中。

您还需要一种识别用户的方法,以便您可以检索该用户的游戏状态。一种方法是使用会话cookie。如何创建和检索会话cookie取决于您正在使用的Web服务器。