You are to create a game where there are two players and a \"board\" consisting
ID: 675450 • Letter: Y
Question
You are to create a game where there are two players and a "board" consisting of 20 spaces. The spaces have a sequence of values consisting of 10. 20, 30 .... 190, 200. You are to create a circular list of these spaces - print the values in the list (i.e. should print values from 10 to 200). Both players are starting at the 10 space with 0 points Then each player will roll the pair of dice resulting in a value between 1 and 12 - use a random number generator to generate this number (see below). The players will be moved that many spaces and add the value of that space to their score. Print out the dice roll amount and the value of the space that they land on. After each set of moves (i.e. player 1 and player 2 have moved) print the total scores. The game ends when a player reaches 1000 or more points. Print out which player won the game. On the BACK of this page create a HIGH level decomposition diagram and test plan.Explanation / Answer
#include <iostream>
#include <cstdlib>
using namespace std;
class CircularList {
const static int ARRAY_SIZE = 20;
private:
int spaces[ARRAY_SIZE];
int currentPos;
public:
CircularList() {
//prepare the list
currentPos = 0;
int i;
for(i = 0; i < ARRAY_SIZE; i++)
spaces[i] = (i+1) * 10;
}
void printList() {
int i;
for(i = 0; i < ARRAY_SIZE; i++)
cout << spaces[i] << " ";
cout <<endl;
}
int getSpaceVal(int steps) {
currentPos = currentPos+steps;
currentPos = currentPos % ARRAY_SIZE;
return spaces[currentPos];
}
};
int rollDice() {
int num;
/* generate secret number between 1 and 12: */
num = rand() % 12 + 1;
return num;
}
int main() {
//for moving spaces location differently for two players maintaining two lists with same values
CircularList p1spaces = CircularList();
CircularList p2spaces = CircularList();
p1spaces.printList();
int p1score = 0, p2score = 0;
int p1roll, p2roll, count = 0;
int p1val, p2val;
while(p1score < 1000 && p2score < 1000) {
p1roll = rollDice();
p2roll = rollDice();
p1val = p1spaces.getSpaceVal(p1roll);
p2val = p2spaces.getSpaceVal(p2roll);
p1score += p1val;
p2score += p2val;
count++;
cout << "Player 1 rolls a " << p1roll << "- at space with value " << p1val << endl;
cout << "Player 2 rolls a " << p2roll << "- at space with value " << p2val << endl;
cout << "After roll " << count << " - Player 1 score is " << p1score << ", Player 2 score is " << p2score <<endl;
}
if(p1score >= 1000)
cout << "The winner is Player 1 - Player 1 score is " << p1score << " to player 2 score of " <<p2score <<endl;
else if(p2score >= 1000)
cout << "The winner is Player 2 - Player 2 score is " << p1score << " to player 1 score of " <<p2score <<endl;
}
-----------output-------------------
10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160 170 180 190 200
Player 1 rolls a 6- at space with value 70
Player 2 rolls a 12- at space with value 130
After roll 1 - Player 1 score is 70, Player 2 score is 130
Player 1 rolls a 11- at space with value 180
Player 2 rolls a 5- at space with value 180
After roll 2 - Player 1 score is 250, Player 2 score is 310
Player 1 rolls a 6- at space with value 40
Player 2 rolls a 5- at space with value 30
After roll 3 - Player 1 score is 290, Player 2 score is 340
Player 1 rolls a 7- at space with value 110
Player 2 rolls a 7- at space with value 100
After roll 4 - Player 1 score is 400, Player 2 score is 440
Player 1 rolls a 11- at space with value 20
Player 2 rolls a 9- at space with value 190
After roll 5 - Player 1 score is 420, Player 2 score is 630
Player 1 rolls a 6- at space with value 80
Player 2 rolls a 6- at space with value 50
After roll 6 - Player 1 score is 500, Player 2 score is 680
Player 1 rolls a 2- at space with value 100
Player 2 rolls a 4- at space with value 90
After roll 7 - Player 1 score is 600, Player 2 score is 770
Player 1 rolls a 2- at space with value 120
Player 2 rolls a 12- at space with value 10
After roll 8 - Player 1 score is 720, Player 2 score is 780
Player 1 rolls a 8- at space with value 200
Player 2 rolls a 3- at space with value 40
After roll 9 - Player 1 score is 920, Player 2 score is 820
Player 1 rolls a 4- at space with value 40
Player 2 rolls a 1- at space with value 50
After roll 10 - Player 1 score is 960, Player 2 score is 870
Player 1 rolls a 4- at space with value 80
Player 2 rolls a 1- at space with value 60
After roll 11 - Player 1 score is 1040, Player 2 score is 930
The winner is Player 1 - Player 1 score is 1040 to player 2 score of 930
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.