Donc j'essaye de créer un jeu de labyrinthe qui fonctionne sur localhost, et le code ci-dessous fonctionne presque (il compile). Le programme utilise soi-disant des boutons comme touches de déplacement (UP, DOWN, LEFT, RIGHT) et j'utilise un analyseur pour obtenir la valeur retournée par le bouton. Cependant, puisque le tout est dans une boucle, chaque fois que je bouge (ou que je passe la valeur de la chaîne de données), il revient aux valeurs initiales de px et py (coordonnées). Ma question est, comment puis-je écraser ces valeurs à chaque boucle successive?cgi - transmettre des valeurs dans des variables à l'intérieur d'une boucle
#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
Voici l'algorithme principal du labyrinthe:
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;
}
quoi de mal à utiliser un fichier txt et à écrire? –