C language to realize mine clearing games

Posted on

The example of this paper shares the specific code of mine clearing game with C language for your reference. The specific content is as follows

Main function: main. C

#include "game.h"
void Menu()
{
    printf("##########################n");
    printf("##1.play 0.exit##########n");
    printf("##########################n");
    printf("## Please Enter select! ##n");
}
  
int main()
{
    Menu();
    srand((unsigned int)time(NULL));
    int quit = 0;
    while (!quit)
    {
     int select = 0;
     Printf ("please enter your choice:  n");
     scanf("%d", &select);
     switch (select)
     {
     case 1:
     game();
     break;
     case 2:
     quit = 1;
     break;
     default :
     Printf ("you have entered it incorrectly, please re-enter it:  n");
     break;
     }
    }
    printf("Bye Bye!n");
    system("pause");
    return 0;
}

Subfunction: game. C

#include "game.h"
void game()
{
char mine[ROWS][COLS] = { 0 };
char show[ROWS][COLS] = { 0 };
Memset (mine, '0', sizeof (mine)); // set initialization array to 0
Memset (show, '*', sizeof (show)); // set initialization array to*
int no_y, no_x;
Set mine (mine, rows, cols, & no x, & no y); // mine '1' means mine
int x = 0;
int y = 0;
int time = 100 - NUM;
while (time > 0)
{
System ("CLS"); // clear screen
Show (show, rows, cols); // print chessboard
Printf ("please enter coordinates: n");
scanf
("%d%d", &x, &y);
if (x<1 || x>10 || y<1 || y>10)
{
Printf ("you have entered it incorrectly, please re-enter it: n");
continue;
}
if (show[x][y] != '*')
{
Printf ("you have entered it incorrectly, please re-enter it: n");
continue;
}
if (mine[x][y] == '1')
{
If (time = = 80) // if there is a thunder for the first time, use a thunder free one to exchange with this one
{
mine
[x][y] = '0';
mine
[no_y][no_y] = '1';
}
else
{
printf
("game over!n");
Show(mine, ROWS, COLS);
break;
}
}
show
[x][y] = get_mine_count(mine, x, y) + '0';
Expand(mine, show, x, y);
time
--;
}

}
Void set mine (char mine [rows] [cols], int col, int row, int * NO x, int * no y) // declare the mine function
{
Int count = num; // set the counter to count the number of mines
while (count > 0)
{
int x = rand() % (col-2) + 1;
int y = rand() % (col-2) + 1;
if ((mine[x][y]) == '0')
{
mine
[x][y] = '1';
count
--;
}
}
for (int i = 1; i <= 10; i++)
{
for (int j = 1; i <= 10; j++)
{
if (mine[i][j] == '0')
{
no_x
= i;
no_y
= j;
return;
}
}
}
}
Void show (char mine [rows] [cols], int row, int Col) // declare the print chessboard function
{
int i = 0;
int j = 0;
printf
(" ");
for (i = 1; i <= 10 ; i++)
{
printf
("%2d |", i);
}
printf
("n");
for (i = 1; i <= 11; i++)
{
printf
("----");
}
printf
("n");
for (i = 1; i <= 10 ; i++)
{
printf
("%2d |", i);
for (j = 1; j <= 10; j++)
{
printf
("%2c |", mine[i][j]);
}
printf
("n");
for (int i = 1; i <= 11; i++)
{
printf
("----");
}
printf
("n");
}
}
Int get me count (char mine [rows] [cols], int x, int y) // mine count
{
return mine[x - 1][y - 1]+ mine[x][y - 1] +
mine
[x + 1][y - 1]+ mine[x + 1][y]
+ mine[x + 1][y + 1]+ mine[x][y + 1] +
+ mine[x - 1][y + 1]+ mine[x - 1][y] - 8 * Posted in C

Leave a Reply

Your email address will not be published.