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

Please Code it in c++ or c langauge. You will run the program providing command-

ID: 3675619 • Letter: P

Question

Please Code it in c++ or c langauge.

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!!!

Explanation / Answer

Minesweeper game:

/* 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();
}