1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
| #include <stdio.h>
#include <stdlib.h>
#include <string.h>
void display_grid(char**);
char** instruction(char**);
int width = 0; //wide of the grid
int height = 0; // height of the grid
int flag = 10; //Numbers of max flag
int main()
{
char **grid=NULL; //Array for the grid, we don't know the size yet
//initialization of the grid
while(flag>0) //while it remains any flags
{
grid = instruction(grid);//main program
}
//display_grid(grid);
return 0;
}
char** instruction(char **grid)
{
char test[1]; //for the type of instruction
int i,j;
int x[1]; //width
int y[1]; //height
printf("\nInstruction ?\n\ng:Initialize the grid\nb:Mine placement\nu:Uncover a tile\nf:Place a flag\n=> ");
scanf("%s %d %d",test,x,y);
printf("x:%d y:%d\n",x[0],y[0]);
switch(test[0]) //which instruction is asked
{
case 'g': //set up the grip
{
if(x[0]>100 || y[0]>100 || (x[0]*y[0]<10)) { //check the grid size
printf("grid error\n");
exit(1);
}
width = x[0];
height = y[0];
//allocation
grid = (char **) malloc (height * sizeof(char *));
for(i=0; i<width*height; i++)
grid[i] = (char *) malloc (width * sizeof(char *));
for(i=0;i<height;i++)
{
for(j=0;j<width;j++)
{
grid[i][j] = '*'; //fill up the grid with *
}
}
display_grid(grid);
int mines = 10;
while(mines>9)
{
printf("\nYou have %d mines remaining\nPlease choose an emplacement\n",mines);
scanf("%s %d %d",test,x,y);
if(test[0]!='b' || x[0] < 0 || x[0]>width-1 || y[0]<0 || y[0]> height-1 || grid[x[0]][y[0]]=='b')
{
printf("mine-error\n");
exit(1);
}
grid[x[0]][y[0]]='b';
printf("Mine successfully placed in (%d,%d)\n",x[0],y[0]);
mines--;
}
break;
}
case 'u': //uncover tile
{
if(x[0] < 0 || x[0]>width-1 || y[0]<0 || y[0]> height-1 || grid[x[0]][y[0]]!='*')
{
if(grid[x[0]][y[0]]=='b') {
printf("You have uncovered a mine !\nYou have lost !\n");
}
else {
printf("uncover-error\n");
}
exit(1);
}
int counter=0; //numbers of mines around the uncover tile
//check the neighbor mines
if(x[0]>0 && x[0]<width-1 && y[0]>0 && y[0]<height-1)
{
for(j=-1;j<=1;j++)
{
for(i=-1;i<=1;i++)
{
if(grid[x[0]+i][y[0]+j]=='b')
{
counter++;
}
}
}
char tmp[1];
sprintf(tmp,"%d",counter);//to convert int in char
grid[x[0]][y[0]] = tmp[0];
}
display_grid(grid);
break;
}
case 'f': //drop a flag
{
if(x[0] < 0 || x[0]>=width-1 || y[0]<0 || y[0]>= height-1 || grid[x[0]][y[0]]=='f')
{
printf("uncover-error");
exit(1);
}
grid[x[0]][y[0]]='f';
printf("Flag successfully placed in (%d,%d)\n",x[0],y[0]);
flag--;
break;
}
}
return grid;
}
void display_grid(char **grid) //display the grid
{
int i,j;
for(i=0;i<height;i++)
{
printf("|");
for(j=0;j<width;j++)
{
/*if(grid[i][j]=='b')
{
printf("*");
}
else
{*/
printf("%c",grid[i][j]);
//}
}
printf("|\n");
}
} |
Partager