Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello, I have provided code for a basic Minesweeper program in C . I think I got the majority of the functions but I can

Hello, I have provided code for a basic Minesweeper program in C. I think I got the majority of the functions but I can quite get it to run properly (or even at all). Your help would be greatly appreciated if you can help me get it running.
#include
#include
#include
#include
#define MAX_COMMAND_LENGTH 100
#define MAX_TOKENS 10
#define BOARD_SIZE 10
#define NUM_MINES 10
//Game board representation
char board[BOARD_SIZE][BOARD_SIZE];
int revealed[BOARD_SIZE][BOARD_SIZE]; //0 if revealed, 1 if hidden
//Function declarations
void rungame();
char* getline(char* input);
char** gettokens(char* input);
int processcommand(char** tokens);
void command_new(char** tokens);
void command_show();
void initialize_board();
void place_mines();
int count_adjacent_mines(int x, int y);
void reveal(int x, int y);
void print_board();
//calls the run
int main(){
rungame();
return 0;
}
//Sets up the gameboard
void rungame(){
char input[MAX_COMMAND_LENGTH];
char** tokens;
int status =1;
initialize_board();
place_mines();
//while loop to keep the game running until the 'status' string is NULL
while (status){
printf(">");
getline(input);
tokens = gettokens(input);
status = processcommand(tokens);
for (int i =0; tokens[i]!= NULL; i++){
free(tokens[i]);
}
free(tokens);
}
}
//function to read text input from user and store it in the 'input' string
char* getline(char* input){
fgets(input, MAX_COMMAND_LENGTH, stdin);
input[strcspn(input,"
")]=0;
return input;
}
//function to read text input from user and store it in the 'input' string
char** gettokens(char* input){
char** tokens = malloc(MAX_TOKENS * sizeof(char*));
char* token;
int i =0;
token = strtok(input,"");
while (token != NULL && i < MAX_TOKENS){
tokens[i++]= strdup(token);
token = strtok(NULL,"");
}
tokens[i]= NULL; // Null-terminate the array
return tokens;
}
//function to interpret and process the commands entered by the user
int processcommand(char** tokens){
if (strcmp(tokens[0], "new")==0){
command_new(tokens);
}
else if (strcmp(tokens[0], "show")==0){
command_show();
}
else if (strcmp(tokens[0], "quit")==0){
return 0;
}
return 1;
}
//Function that resets the game state and prepares the game board for a new round of play.
void command_new(char** tokens){
initialize_board();
place_mines();
printf("New game started.
");
}
//Shows a new board
void command_show(){
print_board();
}
//Creates the board using a 2D array
void initialize_board(){
for (int i =0; i < BOARD_SIZE; i++){
for (int j =0; j < BOARD_SIZE; j++){
board[i][j]='';
revealed[i][j]=0;
}
}
}
//Function that uses the rand operation to randomly place mines
void place_mines(){
srand(time(NULL));
for (int i =0; i < NUM_MINES;){
int x = rand()% BOARD_SIZE;
int y = rand()% BOARD_SIZE;
if (board[x][y]!='*'){
board[x][y]='*';
i++;
}
}
}
//
int count_adjacent_mines(int x, int y){
int count =0;
for (int dx =-1; dx <=1; dx++){
for (int dy =-1; dy <=1; dy++){
int nx = x + dx;
int ny = y + dy;
if (nx >=0 && nx < BOARD_SIZE && ny >=0 && ny < BOARD_SIZE && board[nx][ny]=='*'){
count++;
}
}
}
return count;
}
//Function that counts the number of mines adjacent to a specific cell on the game board
void reveal(int x, int y){
if (x <0|| x >= BOARD_SIZE || y <0|| y >= BOARD_SIZE || revealed[x][y]) return;
revealed[x][y]=1;
if (board[x][y]=='*') return;
int adjacent_mines = count_adjacent_mines(x, y);
}
//Function that prints the official board with every component hidden
void print_board(){
for (int i =0; i < BOARD_SIZE; i++){
for (int j =0; j < BOARD_SIZE; j++){
if (revealed[i][j]){
if (board[i][j]=='*'){
printf("*");
}
else {
int adj_mines = count_adjacent_mines(i, j);
if (adj_mines >0){
printf("%d ", adj_mines);
}
else {
printf("");
}
}
}
else {
printf("."); // Hidden cells
}
}
printf("
");
}
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Processing Fundamentals Design

Authors: Marion Donnie Dutton Don F. Seaman

14th Edition Globel Edition

1292107634, 978-1292107639

More Books

Students also viewed these Databases questions