A3 - Solution For CIS1500
A3 - Solution For CIS1500
h>
/* 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);
/* 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;
if(picks <= 0)
return 1;
}while(!done);
return 0;
}
// Declare variables
FILE * filePtr = NULL;
char filename[80];
char gameBoard[BOARD_SIZE][BOARD_SIZE];
int boardSize;
// 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");
}
}