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

For this assignment, you will implement the game minesweeper. This game was made

ID: 3676421 • Letter: F

Question

For this assignment, you will implement the game minesweeper. This game was made popular by the Windows operating system in the early 1990s, and it has continued to be a pre-installed game until Windows 8. You can watch a video of how to play minesweeper on YouTube or read about it on Wikipedia:

https://www.youtube.com/watch?v=Z0EAysRluJk

https://en.wikipedia.org/wiki/Microsoft_Minesweeper

The game is very simple. The object of the game is to open/reveal every cell on the board without detonating a mine. Every cell contains a number or a mine. The numbers tell you how many mines surround the cell in the horizontal, vertical, and diagonal directions, e.g. at most, 8 possible mines surrounding a cell. You can flag a cell, which helps you remember where you think there is a mine to not detonate. If you select to open/reveal a cell that has a mine, then you automatically lose the game!

Our game will be a little different, since we do not have a graphical user interface for clicking. You will run the program providing command-line arguments for the number of rows, columns, and mines in any order. Therefore, the number must be preceded by an option designator, -r for rows, -c for columns, and –m for mines.

You will then randomly distribute the mines on the board and setup the numbers describing how many adjacent mines to each cell. After that, you will display a blank board, and ask the user to flag or open a cell on the board until the user selects a cell that contains a mine (losing the game) or selects all cells free of mines (winning the game). After the user wins or loses, you will ask the user if he/she wants to play again.

If so, you must get the number of rows, columns, and mines for the new game and create a new board for a new game. BTW, you can display row and column numbers on board to make it easier to read/know which row and column to select!!!

Example run of a game:

a.o ut –r 9 –c 9 –m 10

-------------------

| | | | | | | | | |

-------------------

| | | | | | | | | |

-------------------

| | | | | | | | | |

-------------------

| | | | | | | | | |

-------------------

| | | | | | | | | |

-------------------

| | | | | | | | | |

-------------------

| | | | | | | | | |

-------------------

| | | | | | | | | |

-------------------

| | | | | | | | | |

-------------------

Flag(1) or Open(2): 2 row, col: 0 4

You lose!!!

-------------------

|1|1|0|1|*|1|0|0|0|

-------------------

|*|2|1|2|2|2|0|0|0|

-------------------

|2|*|2|2|*|1|0|0|0|

-------------------

|1|1|3|*|3|1|0|0|0|

-------------------

|1|1|3|*|2|0|0|0|0|

-------------------

|1|*|3|2|2|0|0|0|0|

-------------------

|1|1|2|*|1|0|0|0|0|

-------------------

|1|2|3|2|1|0|0|0|0|

-------------------

|1|*|*|1|0|0|0|0|0|

Do you want to play again (1-yes, 2-no): 1 How many rows, cols? 9 9

How many mines? 10

-------------------

| | | | | | | | | |

-------------------

| | | | | | | | | |

-------------------

| | | | | | | | | |

-------------------

| | | | | | | | | |

-------------------

| | | | | | | | | |

-------------------

| | | | | | | | | |

-------------------

| | | | | | | | | |

-------------------

| | | | | | | | | |

-------------------

| | | | | | | | | |

-------------------

Flag(1) or Open(2): 2 row, col: 0 0

-------------------

|1| | | | | | | | |

-------------------

| | | | | | | | | |

-------------------

| | | | | | | | | |

-------------------

| | | | | | | | | |

-------------------

| | | | | | | | | |

-------------------

| | | | | | | | | |

-------------------

| | | | | | | | | |

-------------------

| | | | | | | | | |

-------------------

| | | | | | | | | |

Flag(1) or Open(2): 2 row, col: 8 0

-------------------

|1| | | | | | | | |

-------------------

| | | | | | | | | |

-------------------

| | | | | | | | | |

-------------------

| | | | | | | | | |

-------------------

| | | | | | | | | |

-------------------

| | | | | | | | | |

-------------------

| | | | | | | | | |

-------------------

| | | | | | | | | |

-------------------

|1| | | | | | | | |

-------------------

Flag(1) or Open(2): 2 row, col: 5 5

-------------------

|1| | | | |1|0|0|0|

-------------------

| | | | | |2|0|0|0|

-------------------

| | | | | |1|0|0|0|

-------------------

| | | | |3|1|0|0|0|

-------------------

| | | | |2|0|0|0|0|

-------------------

| | | | |2|0|0|0|0|

-------------------

| | | | |1|0|0|0|0|

-------------------

| | | |2|1|0|0|0|0|

-------------------

|1| | |1|0|0|0|0|0|

Flag(1) or Open(2): 1 row, col: 6 3

-------------------

|1| | | | |1|0|0|0|

-------------------

| | | | | |2|0|0|0|

-------------------

| | | | | |1|0|0|0|

-------------------

| | | | |3|1|0|0|0|

-------------------

| | | | |2|0|0|0|0|

-------------------

| | | | |2|0|0|0|0|

-------------------

| | | |!|1|0|0|0|0|

-------------------

| | | |2|1|0|0|0|0|

-------------------

|1| | |1|0|0|0|0|0|

-------------------

Flag(1) or Open(2): 2 row, col: 7 1

-------------------

|1|1|0|1|!|1|0|0|0|

-------------------

|!|2|1|2|2|2|0|0|0|

-------------------

|2|!|2|2|!|1|0|0|0|

-------------------

|1|1|3|!|3|1|0|0|0|

-------------------

|1|1|3|!|2|0|0|0|0|

-------------------

|1|!|3|2|2|0|0|0|0|

-------------------

|1|1|2|!|1|0|0|0|0|

-------------------

|1|2| |2|1|0|0|0|0|

-------------------

|1|!|!|1|0|0|0|0|0|

Flag(1) or Open(2): 2 row, col: 7 2 Congratulations!!!

-------------------

|1|1|0|1|*|1|0|0|0|

-------------------

|*|2|1|2|2|2|0|0|0|

-------------------

|2|*|2|2|*|1|0|0|0|

-------------------

|1|1|3|*|3|1|0|0|0|

-------------------

|1|1|3|*|2|0|0|0|0|

-------------------

|1|*|3|2|2|0|0|0|0|

-------------------

|1|1|2|*|1|0|0|0|0|

-------------------

|1|2|3|2|1|0|0|0|0|

-------------------

|1|*|*|1|0|0|0|0|0|

-------------------

Do you want to play again (1-yes, 2-no): 2

Requirements for game:

You must provide a usage message, if the user enters incorrect command-line arguments

You must not have functions over 15-20 lines long (-10 automatically)

You must not use global variables (-10 automatically)

You must ask the user if he/she wants to play again

(If no, then end.

If yes, then prompt for rows, cols, and mines for new game.)

You must not have any memory leaks (-10 automatically)

You must detect these errors:

(Invalid row or column to flag or open

Opening a cell that has already been opened)

Extra Credit: Recursively open all cells adjacent to 0 cell

If the user selects a cell that has no mines surrounding it, then you will help the user by recursively opening all adjacent cells to empty cells, until you reach cells with a surround mine.

Explanation / Answer

Please find below the code for the game Minesweeper. I have provided the comments where ever possible for better understanding.

/* Include files */
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <time.h>

/* Definitions */
#define BOARD_SIZE 6
#define TRUE 0
#define FALSE 1

/* Global Variables */
char board[BOARD_SIZE][BOARD_SIZE];
char game_board[BOARD_SIZE][BOARD_SIZE];
int lost = 0;

/* Function prototypes */
void display_welcome();
void build_board();
void build_gboard();
void create_mines();
void print_board();
void print_fullboard();
void start();
int play_game();
void play_again();
int check_win_game();
void check_for_mine(int, int);
int check_for_nearby_mines(int, int);

// Main function
int main ()
{
display_welcome();
printf(" Any time you're ready... Just press ENTER. :)");

// Get rid of welcome screen
getchar();
system("cls");

start();

return 0;

}

/* Build board used for created random mines */
void build_board()
{

int i, j;

for(i = 0; i < BOARD_SIZE; i++)
for(j = 0; j < BOARD_SIZE; j++)
board[i][j] = 'o';

create_mines();
}

/* Build game board for user input */
void build_gboard()
{

int i, j;
int row, col, cur = 4;
printf("Creating game board.... Ready..set.. PLAY! ");

for(i = 0; i < BOARD_SIZE; i++)
for(j = 0; j < BOARD_SIZE; j++)
game_board[i][j] = 'o';

for(col = 0; col < BOARD_SIZE; col++)
printf("%d ", col + 1);

printf(" ");

for(row = 0; row < BOARD_SIZE; row++)
{
for(col = 0; col < BOARD_SIZE; col++)
{
printf("%c ", game_board[row][col]);
}
printf(" %d ", row + 1);
printf(" ");
}
}

/* Create random places in the array for mines */
void create_mines()
{
int i, random;

srand(time(0));

for (i = 0; i < BOARD_SIZE; i++)
{
random = rand() % (BOARD_SIZE);
board[random][i] = '*';
}

}

/* Print the game board */
void print_board()
{
int row, col;

system("cls");
for(col = 0; col < BOARD_SIZE; col++)
printf("%d ", col + 1);

printf(" ");
for(row = 0; row < BOARD_SIZE; row++)
{
for(col = 0; col < BOARD_SIZE; col++)
{
printf("%c ", game_board[row][col]);
}
printf(" %d ", row + 1);
printf(" ");
}
}

/* Print the full board showing mines */
void print_fullboard()
{
int row, col;

system("cls");
for(col = 0; col < BOARD_SIZE; col++)
printf("%d ", col + 1);

printf(" ");
for(row = 0; row < BOARD_SIZE; row++)
{
for(col = 0; col < BOARD_SIZE; col++)
{
printf("%c ", board[row][col]);
}
printf(" %d ", row + 1);
printf(" ");
}
}

/* Take user input for playing of the game */
int play_game()
{
int r_selection = 0, c_selection = 0,
nearbymines = 0, nearbymines2 = 0,
nearbymines3 = 0, nearbymines4 = 0,
nearbymines5 = 0, nearbymines6 = 0,
nearbymines7 = 0, nearbymines8 = 0,
i = 0;

do {
printf(" Make a selection (ie. row [ENTER] col): ");
printf("Row--> ");
scanf("%d", &r_selection);
printf("Col--> ");
scanf("%d", &c_selection);

} while(r_selection < 1 || r_selection > BOARD_SIZE || c_selection < 1 || r_selection > BOARD_SIZE);
// ^ Checks for any invalid input statements from user.

check_for_mine(r_selection - 1, c_selection - 1);

if(lost == 1)
return -1;

nearbymines = check_for_nearby_mines(r_selection - 1, c_selection - 1);
game_board[r_selection - 1][c_selection - 1] = (char)( ((int)'0') + nearbymines );

  
if(nearbymines == 0)
{
if(c_selection != BOARD_SIZE)
{
i = 0;
while(nearbymines == 0 && (c_selection - 1 + i) < BOARD_SIZE)
{
// This is checking elements to the right
nearbymines = check_for_nearby_mines(r_selection - 1, (c_selection - 1 + i));
if(nearbymines != -1)
{
game_board[r_selection - 1][(c_selection - 1) + i] = (char) ( ((int)'0') + nearbymines );
i++;
}
}
if(r_selection != 1)
{
i = 0;
while(nearbymines5 == 0 && (c_selection - 1 + i) < BOARD_SIZE && (r_selection - 1 - i) > 0)
{
// This is checking elements to the diagonal-uright
nearbymines5 = check_for_nearby_mines((r_selection - 1 - i), (c_selection - 1 + i));
if(nearbymines5 != -1)
{
game_board[(r_selection - 1) - i][(c_selection - 1) + i] = (char) ( ((int)'0') + nearbymines5);
i++;
}
}
}
if(r_selection != BOARD_SIZE)
{
i = 0;
while(nearbymines6 == 0 && (r_selection - 1 + i) < BOARD_SIZE && (c_selection - 1 + i) < BOARD_SIZE )
{
// This is checking elements to the diagonal-dright
nearbymines6 = check_for_nearby_mines((r_selection - 1 + i), (c_selection - 1 + i));
if(nearbymines != -1)
{
game_board[(r_selection - 1) + i][(c_selection - 1) + i] = (char) ( ((int)'0') + nearbymines6);
i++;
}
}
}
}

if(r_selection != BOARD_SIZE)
{
i = 0;
while(nearbymines2 == 0 && (r_selection - 1 + i) < BOARD_SIZE)
{
// This is checking elements heading down
nearbymines2 = check_for_nearby_mines((r_selection - 1 + i), c_selection - 1);
if(nearbymines2 != -1)
{
game_board[(r_selection - 1) + i][c_selection - 1] = (char) ( ((int)'0') + nearbymines2 );
i++;
}
}

if(c_selection != BOARD_SIZE)
{
i = 0;
while(nearbymines7 == 0 && (r_selection - 1 + i) < BOARD_SIZE && (c_selection - 1 - i) > 0)
{
// This is checking elements to the diagonal-dleft
nearbymines7 = check_for_nearby_mines((r_selection - 1 + i), (c_selection - 1 - i));
if(nearbymines != -1)
{
game_board[(r_selection - 1) + i][(c_selection - 1) - i] = (char) ( ((int)'0') + nearbymines7);
i++;
}
}
}
}

if(r_selection != 1)
{
i = 0;
while(nearbymines3 == 0 && (r_selection - i) > 0)
{
// This is checking elements heading up
nearbymines3 = check_for_nearby_mines((r_selection - 1 - i), c_selection - 1);
if(nearbymines3 != -1)
{
game_board[(r_selection - 1) - i][c_selection - 1] = (char) ( ((int)'0') + nearbymines3 );
i++;
}
}
if(c_selection != BOARD_SIZE)
{
while(nearbymines8 == 0 && (c_selection - 1 - i) > 0 && (r_selection - 1 - i) > 0)
{
// This is checking elements to the diagonal-uleft
nearbymines8 = check_for_nearby_mines((r_selection - 1 - i), (c_selection - 1 - i));
if(nearbymines8 != -1)
{
game_board[(r_selection - 1) - i][(c_selection - 1) - i] = (char) ( ((int)'0') + nearbymines8);
i++;
}
}
}
}

if(c_selection != 1)
{
i = 0;
while(nearbymines4 == 0 && (c_selection - i) > 0)
{
// This is checking elements to the left
nearbymines4 = check_for_nearby_mines(r_selection - 1, (c_selection - 1 - i));
if(nearbymines4 != -1)
{
game_board[r_selection - 1][(c_selection - 1) - i] = (char) ( ((int)'0') + nearbymines4 );
i++;
}
}
}
}

// Handles a player winning.
if(check_win_game() == TRUE)
{
system("cls");
print_fullboard();
printf(" You've won the game!! Congrats!! ");
play_again();
}

return 0;
}

/* Check whether user input has selected a mine */
void check_for_mine(int r_select, int c_select)
{
if(board[r_select][c_select] == '*')
{
printf(" You've hit a mine! You lose! ");
getchar(); getchar();
lost = 1;
}
}

/* Another ridiculous function to find nearby mines.
* I know, I know...it's messy, and needs a rewrite. */
int check_for_nearby_mines(int r_select, int c_select)
{
int nearby_mine_count = 0;

if(board[r_select][c_select] == '*')
{
return -1;
}
// Check for mines below and to the right.
if(r_select < (BOARD_SIZE - 1) && c_select < (BOARD_SIZE - 1))
{
// Check for mine below
if(board[r_select + 1][c_select] == '*')
nearby_mine_count++;
// Check for mine to the right.
if(board[r_select][c_select + 1] == '*')
nearby_mine_count++;
// Check for mine diagonal-dright.
if(board[r_select + 1][c_select + 1] == '*')
nearby_mine_count++;

// Check whether the columns to the left can be checked
if(c_select != 0)
{
// Check for mine diagonal-dleft
if(board[r_select + 1][c_select - 1] == '*')
nearby_mine_count++;
// Check for mine to the left
if(board[r_select][c_select - 1] == '*')
nearby_mine_count++;
}
// Check whether the rows above can be checked
if(r_select != 0)
{
// Check for mine above
if(board[r_select - 1][c_select] == '*')
nearby_mine_count++;
// Check for mine diagonal-uright
if(board[r_select - 1][c_select + 1] == '*')
nearby_mine_count++;
// Check whether columns to the left can be checked
if(c_select != 0)
{
// Check for mine diagonal-uleft
if(board[r_select - 1][c_select - 1] == '*')
nearby_mine_count++;
}
}
}
// Check if selection is in last row
if(r_select == (BOARD_SIZE - 1) && c_select != (BOARD_SIZE - 1))
{
// Check for mine above
if(board[r_select - 1][c_select] == '*')
nearby_mine_count++;
// Check for mine diagonal-uright
if(board[r_select - 1][c_select + 1] == '*')
nearby_mine_count++;
}
// Check if selection is in last column
if(c_select == (BOARD_SIZE - 1) && r_select != (BOARD_SIZE - 1))
{
// Check for mine to the left
if(board[r_select][c_select - 1] == '*')
nearby_mine_count++;
// Check for mine diagonal-dleft
if(board[r_select + 1][c_select - 1] == '*')
nearby_mine_count++;
}
// Check whether selection is last in element
if(r_select == (BOARD_SIZE - 1) && c_select == (BOARD_SIZE - 1))
{
// Check for mine to the left
if(board[r_select][c_select - 1] == '*')
nearby_mine_count++;
// Check for mine diagonal-dleft
if(board[r_select - 1][c_select - 1] == '*')
nearby_mine_count++;
// Check for mine above
if(board[r_select - 1][c_select] == '*')
nearby_mine_count++;
}

return nearby_mine_count;
}

/* Check if user has won game */
int check_win_game()
{
int row, col;

for(row = 0; row < BOARD_SIZE; row++)
for(col = 0; col < BOARD_SIZE; col++)
{
if(game_board[row][col] == 'o' && board[row][col] != '*')
return FALSE;
}

return TRUE;
}
// Ask user if they wish to play again.
void play_again()
{
char ans;

printf(" Would you like to play again? (y/n) --> ");
scanf(" %c", &ans);

if(toupper(ans) == 'Y')
{
system("cls");
start();
}

else
{
printf(" Thanks for playing! Bye.");
(void) getchar();
exit(EXIT_SUCCESS);
}
}
// Displays the welcome message, and the GNU License
void display_welcome()
{
puts("-----------------------Welcome to Minesweeper!---------------------------");
puts("Version: 1.01");
puts(" ");
printf("This program is free software: you can redistribute it and/or modify "
"it under the terms of the GNU General Public License as published by "
"the Free Software Foundation, either version 3 of the License, or "
"(at your option) any later version.");
puts(" ");
}

void start()
{
lost = 0; // User hasn't lost yet
// Build both game boards (one for the user to see,
// and the one with the mines).
build_board();
build_gboard();

// Start playing game
do
{
play_game();
print_board();
} while(lost != 1); // While the user hasn't lost, loop.

// Once user is lost, print the board with all the mines.
print_fullboard();

// Play again?
play_again();
}

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