Question
CS 115: Assignment 1 Overview In this assignment, you will simulate a simple board game. The board is a grid, and starts with a pile
CS 115: Assignment 1
Overview
In this assignment, you will simulate a simple board game. The board is a grid, and starts with a pile of money in each cell. Players take turns rolling four dice to pick a cell, and then taking the money from there. If the cell is rolled again, the money will already be gone. The program will be divided into a main function and three supporting modules. A C++ module is a combination of a header (.h) file and a source (.cpp)filewiththesamebasefilename,e.g.,Dice.handDice.cpp. Atestprogramwillbeprovided for each module. When the game runs, it will print out the board, the numbers rolled and the cell hit, and how much money the player receives. Then, the player will have the chance to quit the game. If he/she doesn't, the process will be repeated for the next player.
The main purpose of this assignment is to give you practice using two-dimensional arrays, including passing two-dimensional arrays to functions. For Part A, you will add constants and functions relating to the size of the game board. For Part B, you will add functions to handle the game board itself. For Part C, you will add constants and functions related to simulated dice. For Part D, you will use the constants and functions from Parts A, B, and C to write the game program. Then, for Part E, you will improve the display of the game board.
Another purpose of this assignment is to familiarize you with multi-file programs. To keep the assignment from getting to be too long, the files are small and the functions are mostly short. Many functions only contain a single line of code.
Warning: The test programs used in this course sometimes capture and tests the output from the coutbuffer. Therefore, you must use cout to print your output. If you print it in some other way, (e.g. using printf or cerr), the test program will not see it. Then you will get a mark of zero for that portion of the assignment.
The Game Board
The game board will be represented as a 2-dimensional array. It is 7 x 7 cells in size. The elements of the array will store the amount of money in the different grid cells. The grid rows will be named using the (uppercase)letters'A'to'G',andthegridcolumnswillbenamedusingthedigits'0'to'6'. Thus, each cell will have a unique name such as A2 (row 0, column 2) or E6 (row 4, column 6).
The cells on the board will start with more or less money depending on how close they are to the center. Specifically, the cells on the edge will each have $1, the next row of cells inside them will each have $2, then $3, and the center cell will have $4. At game start, the board will look as follows:
$1 $1 $1 $1 $1 $1 $1 $1 $2 $2 $2 $2 $2 $1 $1 $2 $3 $3 $3 $2 $1 $1 $2 $3 $4 $3 $2 $1 $1 $2 $3 $3 $3 $2 $1 $1 $2 $2 $2 $2 $2 $1 $1 $1 $1 $1 $1 $1 $1
CS 115: Assignment 1
In Part E the display of the game board will be revised to look as follows:
0123456 +-------------------------------------+ || A| $1 $1 $1 $1 $1 $1 $1 |A ||
B| $1 $2 $2 $2 $2 $2 $1 |B || C| $1 $2 $3 $3 $3 $2 $1 |C || D| $1 $2 $3 $4 $3 $2 $1 |D || E| $1 $2 $3 $3 $3 $2 $1 |E || F| $1 $2 $2 $2 $2 $2 $1 |F || G| $1 $1 $1 $1 $1 $1 $1 |G || +-------------------------------------+ 0123456
The Dice
The simulated dice have four sides with the values 0, 1, 2, and 3. When choosing a grid cell in the game, two dice are rolled and their values are added to give the row number. For example, if 1 and 3 were rolled, the row would be 1 + 3 = 4. Another two dice are rolled and added to choose the column. As a result, the cells near the center will be chosen more frequently.
The program will use symbols to print the dice values so that they look like they are on dice. For example:
Row Column +---+ +---+ +---+ +---+ |2||3| |1||0| +---+ +---+ +---+ +---+
Here, row2 + 3 = 5and column1 + 0 = 1were rolled, so the cell chosen isF1. Requirements
Part A: The Board Size [20% test program]
In Part A, you will create constants and functions related to the board size. Put the constants and function prototypes in a file named BoardSize.h and the function implementations in a file named BoardSize.cpp. Throughout this course, make sure that you use exactly the specified file names, whicharecasesensitive. Here,BandSareuppercaseandtheremaininglettersarelowercase.
By the end of Part A, you will have functions with the following prototypes:
-
char getRowName (int row);
-
char getColumnName (int column);
Perform the following steps:
-
As the first line in BoardSize.h and every other header (.h) file for the rest of the course, put the following line: #pragma once This line tells the C++ compiler to only compile this file once even if it happens to be #included multiple times:
-
InBoardSize.h,defineBOARD_SIZEandBOARD_CELL_COUNTasintegerconstants. BOARD_SIZE should have a value of 7 and BOARD_CELL_COUNT should have a value of BOARD_SIZE * BOARD_SIZE.
-
In BoardSize.h, copy in the function prototypes shown above. You are now finished BoardSize.h.
-
Near the top of BoardSize.cpp, add the following command: #include "BoardSize.h"
-
Add an implementation for the getRowName function that returns the name character for that row. For example, if 0 is passed in as the row parameter, then the name character is 'A', so 'A' should be returned.
Hint: You can use mathematical operators, such as addition and subtraction, on chars. The uppercase letters form a sequence, so you can add 'A' + 2 and get 'C'. The same kinds of operations apply to lowercase letters and to digits.
-
Add an implementation for the getColumnName function that returns the character name of that column.
Hint: Columnnamesarecharsstartingwith'0',and'0'+3=='3'.
-
TestyourBoardSizemoduleusingtheTestBoardSize1.cppprogramprovided. Youwillalso
need the TestHelper.h and TestHelper.cpp files. Note: AllthetestprogramsforthegamewillusethesameTestHelper.hand
TestHelper.cpp files. Note: For additional information on compiling programs consisting of multiple files, please
consult link.
Part B: The Game Board [25% = 20% test program + 2% output + 3% code]
In Part B, you will create functions related to the board itself. Put the function prototypes in a file namedBoard.h and the function implementations in a file named Board.cpp.
By the end of Part B, you will have functions with the following prototypes:
-
void boardInit (Board board);
-
int boardGetAt (const Board board,
int row, int column);
-
void boardSetAt (Board board, int row, int column,
int value); void boardPrint (const Board board);
In Part E, you will add helper functions with the following prototypes:
-
void boardPrintColumnNameRow ();
-
void boardPrintBorderRow ();
-
void boardPrintEmptyRow ();
-
void boardPrintDataRow (const Board board,
int row);
Perform the following steps:
-
InBoard.cpp,use#includetoincludethe
library,"BoardSize.h",and "Board.h". Remember to put using namespace std;.
-
InBoard.h,usetypedeftodefineaBoardtypecorrespondingtoa2Darrayofintswith dimensionsBOARD_SIZEandBOARD_SIZE. ThetypedefusestheBOARD_SIZEconstant,so Board.h should #include "BoardSize.h".
-
In Board.h, copy in the function prototypes shown above.
-
In Board.cpp, add an implementation for the boardInit function that sets all the elements of the array. The values should depend on the array position, as described in The Game Board above. You must use at least two loops in the implementation.
Hint: Use a pair of nested for loops for the initialization. Hint: Start by initializing all the elements of the board to the same value, such as 1. Then,
after that works, change your function to set them to the correct values.
Hint:Theabsfunctionfromthe
libraryreturnstheabsolutevalueofavariable. So abs(v - MIDDLE) will return how far v is from MIDDLE. It will help you to figure out how far you are from the middle of the array. -
Add an implementation for the boardGetAt function that returns the amount of money at the specified position. The function must determine the value from the array.
-
Add an implementation for the boardSetAt function that sets the amount of money at the specified array position to the specified value.
-
Add an implementation for the boardPrint function that prints the array contents in the first (simpler) format shown under The Game Board above. The four functions named boardPrint* listed above will be used in Part E to print the board in the second format.
Note:The*inboardPrint*isacommoncomputerscienceconventionfor"anything". So boardPrint* means boardPrintColumnNameRow, boardPrintBorderRow, boardPrintEmptyRow, and boardPrintDataRow. You can use the same syntax on Hercules (or any similar system) when entering commands. For example, g++ -c *.cpp will compile all the files in the current directory that have a .cpp extension.
-
T
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