Question

In: Computer Science

Write a program that plays snakes and ladders.

 C++ program 

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

#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

Snakes and ladders is an ancient south Asian board game. It consists of 10X10 grid board...
Snakes and ladders is an ancient south Asian board game. It consists of 10X10 grid board which contains some snakes and ladders at specific boxes/indexes. One hundred is the maximum and a must to win score for each player. First player reaching 100 gets to win the game and is immediately declared as first Winner. You are required to do the following:  Create a snake board of 10 rows and 10 columns.  Randomly generate 09 snakes on the...
C Program and pseudocode for this problem. Write a C program that plays the game of...
C Program and pseudocode for this problem. Write a C program that plays the game of "Guess the number" as the following: Your program choose the number to be guessed by selecting an integer at random in the rang of 1 to 1000. The program then asks the use to guess the number. If the player's guess is incorrect, your program should loop until the player finally gets the number right. Your program keeps telling the player "Too High" or...
Write a program in Matlab where the program plays a hot and cold game. The user...
Write a program in Matlab where the program plays a hot and cold game. The user answers two inputs: x=input('what is the x location of the object?') y=input('what is the y location of the object?') You write the program so that the computer plays the game. Pick a starting point. Program Calculates the distance to the object. Program picks another point, calculates the distance to the object. Program knows its at the right spot when the distance is less than...
Can you write a program for snakes and ladder board game using linked lists in c++
Can you write a program for snakes and ladder board game using linked lists in c++
Write a program that plays an addition game with the user (imagine the user is a...
Write a program that plays an addition game with the user (imagine the user is a 5th grade student). First ask the user how many addition problems they want to attempt. Next, generate two random integers in the range between 10 and 50 inclusive. You must use the Math.random( ) method call.   Prompt the user to enter the sum of these two integers. The program then reports "Correct" or "Incorrect" depending upon the user's "answer".  If the answer was incorrect, show...
Write a Java program that plays the game Rock, Paper, Scissors. The program should generate a...
Write a Java program that plays the game Rock, Paper, Scissors. The program should generate a random choice (Rock, Paper or Scissors) then ask the user to choose Rock, Paper or Scissors. After that the program will display its choice and a message showing if the player won, lost or tied. Next, the program should prompt the user to play again or not. Once the player selects to stop playing the game, the program should print the number of wins,...
Using Python, Assignment Write a program that plays a game of craps. The program should allow...
Using Python, Assignment Write a program that plays a game of craps. The program should allow the player to make a wager before each “turn”. Before each turn, allow the user to either place a bet or exit the game. After each turn display the player’s current balance. Specifics Each player will start with $500.00. Initially, and after each turn give the user the option of betting or leaving the program. Implement this any way you wish, but make it...
Write in C programming using if and else statements only please!!! Write a program that plays...
Write in C programming using if and else statements only please!!! Write a program that plays the following card game: The user starts out with a pot of $100. At each hand of the game, the dealer and the player are dealt a random number between 1 and 52. The player wins $20 if his/her number is greater than the dealer's number; otherwise they lose $20.
For this problem, you will write a program in which the computer plays a dice game...
For this problem, you will write a program in which the computer plays a dice game called Craps. You might need to explore some Python documentation on random() for this homework assignment. PROVIDE A GRAPHICAL FLOWCHART as part of your submission for this solution Rules: The player rolls two six-sided dice. After rolling, the sum of what the dice show is calculated. If the sum is 7 or 11 on the first roll, the player wins. If the sum is...
1. Write a program that plays a simple dice game between the computer and the user....
1. Write a program that plays a simple dice game between the computer and the user. When the program runs, it asks the user to input an even number in the interval [2..12], the number of plays. Display a prompt and read the input into a variable called ‘plays’ using input validation. Your code must ensure that the input always ends up being valid, i.e. even and in [2..12]. 2. A loop then repeats for ‘plays’ iterations. Do the following...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT