Question

In: Computer Science

Each player puts their counter on the tile that says 'start here'.

write in C++

write a program that plays snakes and ladders.  

How to play:

Each player puts their counter on the tile that says 'start here'.
Take it in turns to roll the dice. Move your counter forward the number of spaces shown on the dice.
If your counter lands at the bottom of a ladder, you can move up to the top of the ladder.
If your counter lands on the head of a snake, you must slide down to the bottom of the snake.
The first player to get to the space that says 'home' is the winner.

What data structures do you need?

A Tile

A Tile has at least one property - you may decide to add more properties - the destination property.

The tile's destination property describes five kinds of tiles - ordinary, head of snake, tail of snake, top of ladder, and bottom of ladder.

A ladder's bottom's destination property is the ladder's top's position in the array.  

What should a ladder's top's destination property be?

What should a snake's head's destination property be?

What should a snake's tail's destination property be?

A dynamic 1D array of Tiles

Read a description of the tiles from a file. You don't have to read the properties of ordinary tiles, snakes' tails or ladders' tops.

Solutions

Expert Solution

Solution:

#include
#include
#include
#include

using namespace std;


int main ()

{
//Declaring variables
int p; //player, will be inside player array
int n;//board dimmensions
char type;
int offset;
int pos;
int dice;
string text;
  
  
srand(time(NULL));
  
//The program is opening the text file
fstream inputStream;
inputStream.open ("input.txt"); //opens file
  
// if (! inputStream)
// cout<<"file"<   
  
//inputStream is getting the first 3 values in the text file
inputStream >> n; //board
inputStream >> p; //players
inputStream >> dice;
  
cout << "Hello this is a "<< p <<" Player, "<< n <<"x"<< n << " Snake and Ladders Game\n";
  
  
int boardsize [145]; //establishes board size as array
int players[5]; //establishes players as array
  
for (int i = 0; i players[i]=0;
  
for (int i=0; i<145; i++) //sets board cells to 0
boardsize[i]=0;
  
while (inputStream >>type>>pos>>offset) {//getting information for last info on text file, retreving the Snakes and Ladders
if (type =='S') //The offset has to be set to be negative if the player gets a Snake
boardsize[pos]= (offset * -1);
if (type == 'L')
boardsize[pos]= offset;
//cout<   
}
inputStream.close();
  
//game begins
  
bool lose= true;
//WHILE LOOP UNTIL SOMEONE WINS
while (lose){
for (int i = 0; i < p; i++) //adding dice and position
{
cout << "It is player " << i+1 << "'s turn" << endl;
int totalRoll = 0; //initiating dice value and total dice value
int diceRoll = 0;
  
  
do{
  
diceRoll = (rand () % dice ) +1; //randome dice roll
cout << "-- Rolled a " << diceRoll << endl;
//totalRoll = totalRoll + diceRoll; //dice is rolling until diceroll is not =to 6
players[i] = players[i] + diceRoll; //new position of player //
  
  
if( boardsize[players[i]] != 0){//jump to correct cell if S or L,,,, print if board size is <0 if >0
//cout<< ",board test "<< boardsize[players[i]]< if (boardsize[players[i]] >0)
cout<<"You have landed on a ladder, please move up "< if(boardsize[players[i]] <0)
cout<< "You have landed on a snake, please move back "<   
  
cout << "current position = " < players[i] = players[i]+ boardsize[players[i]];
//cout<< "offset "<< boardsize[players[i]]<   
}
cout <<"player "<< i << " "<<"is at cell " << players[i] <   
if (players[i] >= n*n){
lose = false;
cout << "Player " << i+1 << " in position: " << players[i] <<" "<<"WINS!!"<<" "<< endl;// players pos
}
  
}
while(diceRoll == 6 && lose!=false);
  
if (players[i]>=n*n)
cout< //cout<< "This game is over" < }
  
  
//END OF WHILE
  
  
for(int i = 0; i < p; i++)
if (players[i] >= n*n){
cout << "Player " << i << " is at " << players[i] << endl;
  
  
}
}
return 0;
  
}

Here I am reading tiles from file "input.txt" as

10
4
6
S 15 6
L 20 10
L 12 8
S 54 12
L 64 9
S 73 30
L 80 10
S 99 15


Related Solutions

Specifications for a DVD player says that it should weight between 24 and 25 ounces. The...
Specifications for a DVD player says that it should weight between 24 and 25 ounces. The process has a normal distribution with a mean of 24.5 ounces and a standard deviation of .25 ounces. What percentage of parts will not meet the weight specifications? (The answer should be a percentage with two decimal places; no percentage sign).
Player I and player II each have two pennies. Each player holds 0, 1, or 2...
Player I and player II each have two pennies. Each player holds 0, 1, or 2 pennies in his left hand and the remainder of the pennies (2, 1, or 0 respectively) in his right hand. Each player reveals both hands simultaneously. If the number of coins in one of player I's hands is greater than the number of coins in the respective hand of player II, player I wins the difference in pennies; otherwise, no money is exchanged. (a)....
Below is a game between player A and player B. Each player has two possible strategies:...
Below is a game between player A and player B. Each player has two possible strategies: 1 or 2. The payoffs for each combination of strategies between A and B are in the bracket. For example, if A plays 1 and B plays 1, the payoff for A is -3, and the payoff for B is -2. Player B Strategy 1 Strategy 2 Player A Strategy 1 (-3,-2) (10,0) Strategy 2 (0,8) (0,0) How many pure strategy Nash equilibria does...
Below is a game between player A and player B. Each player has two possible strategies:...
Below is a game between player A and player B. Each player has two possible strategies: 1 or 2. The payoffs for each combination of strategies between A and B are in the bracket. For example, if A plays 1 and B plays 1, the payoff for A is 1, and the payoff for B is 0. Player B Strategy 1 Strategy 2 Player A Strategy 1 (1,0) (0,1) Strategy 2 (0,1) (1,0) How many pure strategy Nash equilibria does...
1) A one-shot prisoners' dilemma pays $500 to each player for cooperation, $200 to each player...
1) A one-shot prisoners' dilemma pays $500 to each player for cooperation, $200 to each player for a double defection, and $878 to a player who defects when the other player cooperates. Assume the game is repeated indefinitely, players play a GRIM strategy, and the time discount factor is 0.56. Calculate the present value of defection. 2) The cooperative payoffs in a prisoners' dilemma are $890 for each player, while the defection payoff is $1,498. The game is repeated indefinitely,...
Each player throws both dice once per turn. The player only scores when the player throws...
Each player throws both dice once per turn. The player only scores when the player throws doubles. Double-6 scores 25 points. A double-3 cancels out a player’s score and puts the score back to zero. Any double other than a 3 or 6 scores 5 points. Scores are recorded and the first player to obtain a total score of fifty points wins the game. Write a MATLAB program to simulate the FIFTY dice game that can: 1. Play the FIFTY...
1. What does the court here mean when it says that liquidateddamages clauses allow the...
1. What does the court here mean when it says that liquidated damages clauses allow the parties to “allocate and incorporate the risks [of the transaction] in their negotiations”?2. Why is it relevant that the plaintiff Ingram was engaged in a childcustody dispute and wanted to move to California as soon as possible?3. What, in plain language, is the issue here?4. How does the court’s resolution of the issue seem to the court the better analysis?5. Why did the plaintiff...
People here spend money trying to counter baldness while people in other countries are starving to...
People here spend money trying to counter baldness while people in other countries are starving to death.On the other hand, foreign aid may be distributed as political favors rather than going to those most in need. Should the U.S. work on achieving the Millennium Aid goal for contributions to increase the U.S. contribution to 0.7% of its GDP or help countries that are starving? I was thinking about Persistent poverty, Economic growth, Human Capital but it seems that it does...
A tile is selected from seven tiles, each labeled with a different letter from the first...
A tile is selected from seven tiles, each labeled with a different letter from the first seven letters of the alphabet. The letter selected will be recorded as the outcome. Consider the following events. Event X : The letter selected comes before " D ". Event Y : The letter selected is found in the word " CAGE ". Give the outcomes for each of the following events. If there is more than one element in the set, separate them...
The Brisbane Manufacturing Company produces a single model of a CD player. Each player is sold...
The Brisbane Manufacturing Company produces a single model of a CD player. Each player is sold for $192 with a resulting contribution margin of $70. Brisbane's management is considering a change in its quality control system. Currently, Brisbane spends $41,000 a year to inspect the CD players. An average of 2,000 units turn out to be defective - 1,600 of them are detected in the inspection process and are repaired for $85. If a defective CD player is not identified...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT