Answered step by step
Verified Expert Solution
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 MAXCOMMANDLENGTH
#define MAXTOKENS
#define BOARDSIZE
#define NUMMINES
Game board representation
char boardBOARDSIZEBOARDSIZE;
int revealedBOARDSIZEBOARDSIZE; if revealed, if hidden
Function declarations
void rungame;
char getlinechar input;
char gettokenschar input;
int processcommandchar tokens;
void commandnewchar tokens;
void commandshow;
void initializeboard;
void placemines;
int countadjacentminesint x int y;
void revealint x int y;
void printboard;
calls the run
int main
rungame;
return ;
Sets up the gameboard
void rungame
char inputMAXCOMMANDLENGTH;
char tokens;
int status ;
initializeboard;
placemines;
while loop to keep the game running until the 'status' string is NULL
while status
printf;
getlineinput;
tokens gettokensinput;
status processcommandtokens;
for int i ; tokensi NULL; i
freetokensi;
freetokens;
function to read text input from user and store it in the 'input' string
char getlinechar input
fgetsinput MAXCOMMANDLENGTH, stdin;
inputstrcspninput
;
return input;
function to read text input from user and store it in the 'input' string
char gettokenschar input
char tokens mallocMAXTOKENS sizeofchar;
char token;
int i ;
token strtokinput;
while token NULL && i MAXTOKENS
tokensi strduptoken;
token strtokNULL;
tokensi NULL; Nullterminate the array
return tokens;
function to interpret and process the commands entered by the user
int processcommandchar tokens
if strcmptokens "new"
commandnewtokens;
else if strcmptokens "show"
commandshow;
else if strcmptokens "quit"
return ;
return ;
Function that resets the game state and prepares the game board for a new round of play.
void commandnewchar tokens
initializeboard;
placemines;
printfNew game started.
;
Shows a new board
void commandshow
printboard;
Creates the board using a D array
void initializeboard
for int i ; i BOARDSIZE; i
for int j ; j BOARDSIZE; j
boardij;
revealedij;
Function that uses the rand operation to randomly place mines
void placemines
srandtimeNULL;
for int i ; i NUMMINES;
int x rand BOARDSIZE;
int y rand BOARDSIZE;
if boardxy
boardxy;
i;
int countadjacentminesint x int y
int count ;
for int dx ; dx ; dx
for int dy ; dy ; dy
int nx x dx;
int ny y dy;
if nx && nx BOARDSIZE && ny && ny BOARDSIZE && boardnxny
count;
return count;
Function that counts the number of mines adjacent to a specific cell on the game board
void revealint x int y
if x x BOARDSIZE y y BOARDSIZE revealedxy return;
revealedxy;
if boardxy return;
int adjacentmines countadjacentminesx y;
Function that prints the official board with every component hidden
void printboard
for int i ; i BOARDSIZE; i
for int j ; j BOARDSIZE; j
if revealedij
if boardij
printf;
else
int adjmines countadjacentminesi j;
if adjmines
printfd adjmines;
else
printf;
else
printf; Hidden cells
printf
;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started