0% found this document useful (0 votes)
88 views6 pages

A3 - Solution For CIS1500

This document contains C code for a minesweeper-like game. It includes function prototypes and documentation comments for functions to: 1. Read a game board from a file and store it in a 2D array. 2. Print the game board to the screen with or without question marks obscuring the values. 3. Check if a given row and column are valid positions on the board. 4. Play the game by allowing the user to pick squares and tracking wins/losses. 5. Modify the board to change adjacent squares to be safe if a special "clear" square is chosen.

Uploaded by

Lucy Wilson
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
88 views6 pages

A3 - Solution For CIS1500

This document contains C code for a minesweeper-like game. It includes function prototypes and documentation comments for functions to: 1. Read a game board from a file and store it in a 2D array. 2. Print the game board to the screen with or without question marks obscuring the values. 3. Check if a given row and column are valid positions on the board. 4. Play the game by allowing the user to pick squares and tracking wins/losses. 5. Modify the board to change adjacent squares to be safe if a special "clear" square is chosen.

Uploaded by

Lucy Wilson
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

#include<stdio.

h>

// Define the MAXIMUM Size of the board, consider this a constant


value you can use anywhere.
// No other global variables can be used.
#define BOARD_SIZE 25

// Prototype for the Bonus question see below for details..


void modifyBoard( const int* row, const int* col, char gameBoard[]
[BOARD_SIZE]);

/* Function: readBoard
* Parameters: FILE - a pointer to a file. Assume the file has been
opened and ready to read from.
* int* boardSize - will be read from the file to actual
board size.
* char gameBoard[][BOARD_SIZE] - is a square grid of
char characters. It is a multi-dimensional array.
* Return type: void
* Description: This function should read a valid game board from a
file into the parameter gameBoard.
* You will first need to read the size of the board
then the game board itself.
* boardSize and gameBoard should contain the contents
of the file.
*/
void readBoard(FILE *filePtr,int * boardSize, char gameBoard[]
[BOARD_SIZE])
{
// Read the board
char ch;
fscanf(filePtr,"%d",boardSize);
fscanf(filePtr, "%c", &ch);

for(short i = 0; i < *boardSize;i++)


{
for( short y = 0; y < *boardSize; y++)
fscanf(filePtr, "%c", &gameBoard[i][y]);
fscanf(filePtr, "%c", &ch);
}
}

/* Function: printBoard
* Parameters:
* int boardSize -which is the size of the square
gameboard gride.
* char gameBoard[][BOARD_SIZE] - is a square grid of
char characters. It is a multi-dimensional array.
* const int useQuestionMarks = 0 indicates show the
characters in the board 1 indicates show ? for all squares.
* Return type: void
* Description: This function prints to the screen the gameboard or
character array. It contains one option 'useQuestionMark'
* which allows the board to be shown as all ? or shows
the actual square values.
*/
void printBoard(const int boardSize, const char gameBoard[]
[BOARD_SIZE], const int useQuestionMark)
{
for(short i = 0; i < boardSize;i++)
{
for( short y = 0; y < boardSize; y++)
{
if(useQuestionMark)
printf("?");
else
printf("%c", gameBoard[i][y]);
}
printf("\n");
}
}
/* Function: isValidSquare
* Parameters:
* int row - the row value you want to confirm is valid.
* int col - the column value you want to confirm is
valid
* int boardSize = the row and column length of the grid
board.
* Return type: int which is boolean for 1 == true, 0 == false
* Description: Returns whether the row and column values are valid in
the board size.
*/
int isValidSquare(int row, int col, int boardSize)
{
return row >= 0 && col >= 0 && row < boardSize && col <
boardSize;
}

/* Function: menu
* Parameters: void
* Return type:void
* Description: This prints the menu options for the game.
*/
void menu()
{
printf("\nFor this game you will will pick three squares.\n");
printf("If the square contains an 'S' it is safe.\n");
printf("If the square contains an 'B' you lose.\n");
printf("If the square contains an 'R' you must pick again.
\n");
printf("All row columns will be 0 based so 3 columns will be
referred to as 0,1, and 2\n\n");
}

/* Function: playGame
* Parameters:
* int boardSize = the row and column length of the grid
board.
* const char gameBoard[][] - is the gameboard that has
already been read.
* Return type: int which is true(1) if they won otherwise it returns
0
* Description: Runs the core mechanics of the game and returns if it
was won or not.
* It does not print if the game was won or not.
* It needs to identify when the game is over and return
the win/lose result.
* The game board should be all at and ready to be
played.
*/
int playGame(int boardSize,char gameBoard[][BOARD_SIZE])
{
// Declare variables to help play the game
int row, col;
int done = 0;
int picks = 3;

// Print the menu


menu();

// print the game boards with question marks


printBoard(boardSize,gameBoard,1);

// Play the game loop


do
{
// Collect the row and column for their guess.

printf("\nPlease input the row and column you want to


select\n");
scanf("%d %d", &row, &col);

// Perform the actions per square.


// Check if the game is over or not
// print what the square contained.
if( isValidSquare(row,col, boardSize) )
{
printf("The game board tile was %c\n",
gameBoard[row][col]);
switch(gameBoard[row][col])
{
case 'S':
printf("You are safe for
now.\n");
picks--;
break;
case 'B':
printf("You have hit a bad
square.\n");
done = 1;
break;
case 'R':
printf("You must pick again.
\n");
break;
/*
case 'C':
printf("Surrounding square
will be safely cleared.\n");
modifyBoard( &row, &col,
gameBoard);
picks--;
break;
*/
default:
printf("NONE OF THE VALID
RESPONSES\n");
}
}
else
{
printf("You've input an invalid square
position, please try again.\n");
}

if(picks <= 0)
return 1;

}while(!done);

return 0;
}

int main( int argc, char ** argv)


{
// Your main function should declare the variables you need
for the game.
// It should allow the user to input a filename.
// It should open that file or indicate an error ( this might
be a separate function).
// It should read the game board, make the function call to
play the game, and print the result of the game.
// All other code should be in functions

// Declare variables
FILE * filePtr = NULL;
char filename[80];

char gameBoard[BOARD_SIZE][BOARD_SIZE];
int boardSize;

// Request your game board file here.


printf("Please input the filename you wish to open:\n");
scanf("%s",filename);

// Open your game file


filePtr = fopen(filename,"r");

// If you have a valid board file. Read the board and play the
game.
if(filePtr)
{
readBoard(filePtr, &boardSize, gameBoard);

if( playGame(boardSize,gameBoard) )
printf("\nYou survived!\n");
else
printf("\nYou've lost the game.\n");

// Print the final board game.


printf("\nThe board game was\n");
printBoard(boardSize,gameBoard,0);
}
else
{
printf("\nError: Unable to read the filename
%s\n",filename);
printf("Please input a valid board game file.\n");
return 1;
}
return 0;
}

/* Bonus Part (5 Marks)


* Write a function that is performed after the user picks a square
with a new letter 'C'
* This function will covert all adjacent neighbour squares (distance
of 1) to 'S'
* This makes all those squares safe.
*
* Ex) with the board
* BBB
* BCB
* BBB
*
* If the user selects 'C' at 1,1 the 'B' squares are tranformed
to 'S' since they are adjacent neighbours.
* The C square should not be transformed into a 'S'.
*
* You must not modify the function prototype.
* You will need to modify your playGame function to achieve this.
* You should at least one other function in this function to
complete the task.
*/
void modifyBoard( const int* row, const int* col, char gameBoard[]
[BOARD_SIZE])
{
// Fill in for bonus marks
for( int i = -1; i<= 1; i++)
{
for( int y = -1; y<= 1; y++)
{
if(i == 0 && y == 0)
continue;

int rval = i+*row;


int cval = y+*col;
if( isValidSquare(rval,cval, BOARD_SIZE) )
{
gameBoard[rval][cval] = 'S';
}
}

}
}

You might also like