in C++ write a program that simulates one turn of the Monopoly game example. For
ID: 3867642 • Letter: I
Question
in C++ write a program that simulates one turn of the Monopoly game example. For this program, only the following squares exist: RegularSquare, GoSquare, IncomeTaxSquare, JailSquare, and GoToJailSquare.
Using the UML sketches in the text as your blueprint, the design utilizes the following patterns: polymorphism, indirection, pure fabrication, and protected variations.
Here are the UML sketches from the text from which to base your implementation:
A player has a location, but thanks to the polymorphism pattern, that location could have different effects based on the landedOn responsibility.
Figure 25.3 is not the final design. A new class by Pure Fabrication is introduced at the end. The final design for your implementation is reflected in figure 25.9 which replaces figure 25.3. However, there is still important information here for the design, so it's included here.
Note in figure 25.3 a player passes itself to the square through the landedOn message.
When landing on a RegularSquare, nothing happens. In this case, landedOn is a NO-OP method.
IncomeTaxSquare can send a message to Player because it receives a reference to the Player through the landedOn message.
IncomeTaxSquare has to ask the player for it's net worth so it can figure out the amount of tax and then send a message to Player telling it how much to reduce it's cash attribute.
Here, the GoToJailSquare is initialized with a reference to JailSquare so it can be passed as an argument to Player.
By Pure Fabrication, we can lower coupling and increase cohesion by introducing a "made up" class named Cup to roll the dice for us. Figure 25.9 should replace figure 25.3 in your final implementation.
Make sure your identifiers match up with the UML. For example, class Player needs to have a variable named loc and class Board must have a method named getTotal with two parameters. Have the player start the turn with $1500 and display the cash total at the end of the turn. Have the Player display the dice roll total to the screen. Have each square display to the user they landed on the square.
Yes, we're violating the Model-View separation principle for the assignment so we don't have to include a UI layer and we're not following the layered architecture pattern.
Grading will be based on how accurately your solution implements the above design, identifiers and all.
Hints:
Create a array of Shape references/pointers in class Board. These references can be used for the loc attribute in class Player.
Since there are only 5 squares in our simple program, the array should be 5 elements long.
Everything you need to create design classes is provided by the UML sketches. Some design class attributes are implicit.
There is no requirement for a layered architecture in this assignment.
Incorporate the logic for taking the turn within the Player class constructor. As a result, your main function will only contain a statement instantiating the Player object.
Don't panic, this looks like a lot, but it's really not. Each class usually has one method. Divide and conquer. Just take it one class at a time.
Explanation / Answer
#include #include #include using namespace std; //status equivalence to the L, S, D, O, W enum State { N, L, S, D, O, W}; //N means nothing, it is my default situation means no situation const SquareNum = 100; //number of squares const TokenNum =4; //number of players const SnakeNum =9; //number of snake tails const LadderNum =9; //number of ladders const LadderShift =16; //min number of squares to advance for ladders const LadderScale = 15; //max -min of number of squares to advance for ladders const SnakeShift = 12; //min number of squares to retreat for snake const SnakeScale = 37; //range of snake = max -min of snake //array of game int board[SquareNum] = {0}; //array of player, record the index of array, not the position int token[TokenNum] = {0}; //the variable recording numbers of turns int turnCounter = 1; //check status of the new position, and write the new position back to array of game //if there is any change State checkState(int player, int step); //a general function to generate random number for range of scale, and shift by shift int randNum(int scale, int shift); //roll 1 dice int roll1Dice(); //roll 2 dice int roll2Dice(); //play game void playGame(); //initialize the array of game void initialize(); //display the array of game void displayBoard(); //this is the cpp file game.cpp #include "game.h" int main() { char choice[3]; //in order to try different seed to get different outcome of same code coutchoice; if (strcmp("yes", choice)==0) { srand(248); } else { srand(time(0)); } initialize(); displayBoard(); playGame(); return 0; } //check specific position to see if there is other token on it bool checkPlayer(int newPos) { for (int i=0; iRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.