Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a minesweeper game using C. Program needs: The game should have a basic g

ID: 3681466 • Letter: C

Question

Create a minesweeper game using C.

Program needs:

The game should have a basic game board comprised of a 10 by 10 grid.

The game shall hide a certain number of mines throughout the game board.

The game should allow the player to sweep a particular square on the grid.

When the user sweeps a square that contains a mine, the game ends and the player loses. When the user sweeps a square that does not contain a mine, the square shall then display the number of mines in its neighboring squares.

When a swept square does not have any neighboring squares containing mines, it shall display 0 and also uncover its neighboring squares. Doing so leads to a chain reaction, which can uncover a number of squares on a single turn.

The game shall end with the player winning upon having all squares that do not contain mines swept.

Explanation / Answer

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

struct cell
{
int visited;
int mine;
int neighbour_mines;
};
struct cell board[10][10];
int neigh_mines(int x,int y)
{
int count=0;
if(y!=9 && board[x][y+1].mine == 1) count++;
if(y!=9 && x!=9 && board[x+1][y+1].mine == 1) count++;
if(x!=9 && board[x+1][y].mine == 1) count++;
if(x!=0 && board[x-1][y].mine == 1) count++;
if(y!=0 && x!=0 && board[x-1][y-1].mine == 1 ) count++;
if(y!=0 && board[x][y-1].mine == 1 ) count++;
if(y!=9 && x!=0 && board[x-1][y+1].mine == 1 ) count++;
if(y!=0 && x!=9 && board[x+1][y-1].mine == 1 ) count++;
board[x][y].neighbour_mines=count;
return count;
}
void setmines(int n)
{
int i;
for( i=0;i<n;i++)
{
int k = rand() % 10 ;
int l = rand() % 10 ;
board[k][l].mine = 1;
}
}
void showboard()
{
int i,j;
for(i=0;i<10;i++)
{
printf(" ");
for( j=0;j<10;j++)
{
if(board[i][j].visited == 1)
printf("| %d ",board[i][j].neighbour_mines);
else printf("| ");
}

}
}
void clear()
{
int i;
for(i=0;i<100;i++)
{
printf(" ");
}
}
void chain(int r,int c)
{
board[r][c+1].visited = 1;
board[r-1][c].visited = 1;
board[r][c-1].visited = 1;
board[r+1][c].visited = 1;
board[r+1][c+1].visited = 1;
board[r+1][c-1].visited = 1;
board[r-1][c+1].visited = 1;
board[r-1][c-1].visited = 1;
clear();
showboard();
}
int main()
{

setmines(25);
showboard();
printf(" Press 1 to start the game");
int ch,r,c;
scanf("%d",&ch);
if(ch ==1)
{
while(ch==1)
{
printf(" Enter row:");
scanf("%d",&r);
printf(" Enter column:");
scanf("%d",&c);
board[r-1][c-1].visited = 1;
clear();
showboard();
if(board[r-1][c-1].mine ==1)
{
printf(" Mine found at this location!!! Game Over!!!"); break;
}
else chain(r-1,c-1);
printf(" Enter 1 again to continue:");
scanf("%d",&ch);

}
}
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote