In the Simple Mine game, there is one mine which is fixed at one of the squares
ID: 3766691 • Letter: I
Question
In the Simple Mine game, there is one mine which is fixed at one of the squares on a 2-dimensional grid. The row and column of the mine is defined at the beginning of the code. The player needs to determine the mine location by clearing all the un-mined squares. Write a program simple.c that prints the current state of the board that is initially covered by all empty squares. The program then asks the user the integer row and column of a square to clear. (counted from 1 at the both sides). If the square contains the mine, the player loses, the program displays the mine by placing an ‘*’ on its square and prints the message “Game Over!” If the square does not contain the mine, the program displays ‘C’ in that square. If the player selects an already-chosen square, the program prints the message “Already cleared! Choose again!” asking the player to choose another square. If the player selects an out-of-range square, the program prints “Invalid square! Choose again!” asking the player to choose another square. At the beginning of your code, define the size of the board which is at most 6, the row and the column of the mine, namely #define SIZE = 3 #define mineRow = 2 #define mineColumn = 1 Use char currentBoard[SIZE][SIZE] to store the current board as it should be printed out. Write a function void printBoard(char[][SIZE]) that gets a two-dimensional array of the current board and prints it in the specified format on the screen. Write a function int domove(char[][SIZE], int row, int column) that gets the current board, the row and column of the selected square and update the current board based on the rules of the game. It also returns 1 if the game is over and 0 otherwise. Write a function int getmove() that asks the user of the row and column of their selected square and returns just one integer value to identify the square. Hint: To identify row 3 and column 4 with one value, you can return 34. Write a function int valid(int) that gets the selected square and returns 1 if its a valid square, 0 otherwise. A square is valid if it is within the range of size of the board. Note that you should pass the selected square to this function by just one integer variable.
Example:
Here is the board:
1 2 3
- - -
1 | | | |
- - -
2 | | | |
- - -
3 | | | |
- - -
Enter your move, (Row Column): 3 1
Here is the board :
1 2 3
- - -
1 | | | |
- - -
2 | | | |
- - -
3 | C | | |
- - -
Enter your move, (Row Column): 2 3
Here is the board :
1 2 3
- - -
1 | | | |
- - -
2 | | | C |
- - -
3 | C | | |
- - -
Enter your move, (Row Column): 3 1
Already cleared! Choose again!
Here is the board :
1 2 3
- - -
1 | | | |
- - -
2 | | | C |
- - -
3 | C | | |
- - -
Enter your move, (Row Column): 4 1
Invalid square! Choose again!
Enter your move, (row column): 2 1
1 2 3
- - -
1 | | | |
- - -
2 | * | | C |
- - -
3 | C | | |
- - -
Game Over!
Explanation / Answer
Here is the code for you. If you need any further refinements, just get back to me.
#include <stdio.h>
#define SIZE 3
#define mineRow 2
#define mineColumn 1
/*Gets a two-dimensional array of the current board and prints it in the specified format on the screen.*/
void printBoard(char X[][SIZE])
{
printf("Here is the board: ");
printf(" 1 2 3 ");
for(int i = 0; i < SIZE; i++)
{
printf("%i|", i+1);
for(int j = 0; j < SIZE; j++)
printf("%c|", X[i][j]);
printf(" - - - - ");
}
}
/*Gets the current board, the row and column of the selected square and
update the current board based on the rules of the game.
It also returns 1 if the game is over and 0 otherwise.*/
int domove(char X[][SIZE], int row, int column)
{
if(row == mineRow && column == mineColumn)
{
X[mineRow-1][mineColumn-1] = '*';
printf("Game Over! ");
return 1;
}
else if(X[row-1][column-1] == 'C')
{
printf("Already cleared! Choose again! ");
return 0;
}
else if(row < 1 || row > SIZE || column < 1 || column > SIZE)
{
printf("Invalid square! Choose again! ");
return 0;
}
else
{
X[row-1][column-1] = 'C';
for(int i = 0; i < SIZE; i++)
for(int j = 0; j < SIZE; j++)
{
if(i == mineRow-1 && j == mineColumn-1)
continue;
if(X[i][j] == ' ')
return 0;
}
}
printf("Yay. You cleared all the unmined squares. ");
return 1;
}
/*Asks the user of the row and column of their selected square
and returns just one integer value to identify the square.*/
int getmove()
{
int row, col;
printf("Enter your move, (Row Column): ");
scanf("%i%i", &row, &col);
return row*10 + col;
}
/* gets the selected square and returns 1 if its a valid square, 0 otherwise.*/
int valid(int num)
{
if(num % 10 > 0 && num % 10 <= SIZE && num / 10 > 0 && num / 10 <= SIZE)
return 1;
return 0;
}
int main()
{
char currentBoard[SIZE][SIZE];
for(int i = 0; i < SIZE; i++)
for(int j = 0; j < SIZE; j++)
currentBoard[i][j] = ' ';
printBoard(currentBoard);
int move = getmove();
while(!domove(currentBoard, move / 10, move % 10))
{
printBoard(currentBoard);
move = getmove();
}
}