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

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 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,...
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...
(HTML) Write a script that plays a “guess the number” game as follows: Your program chooses...
(HTML) Write a script that plays a “guess the number” game as follows: Your program chooses the number to be guessed by selecting a random integer in the range 1 to 1000. The script displays the prompt Guess a number between 1 and 1000 next to a text field. The player types a first guess into the text field and clicks a button to submit the guess to the script. If the player's guess is incorrect, your program should display...
Write a MIPS program that plays the first dozen or so notes of your new hit...
Write a MIPS program that plays the first dozen or so notes of your new hit single (or if you don't have your own hit single, then your favorite) (Go to the Help for information on MIPS instructions, pseudo instructions, directives, and syscalls.) (Experiment with system call 31, 32 and 33. (Put the notes to your song in a MIPS "array" (Format and comment your program appropriately in MARS.
Java Project Requirements: 1.Write a Java program that plays a word game with a user. The...
Java Project Requirements: 1.Write a Java program that plays a word game with a user. The program asks the user questions and then creates a paragraph using the user’s answers. 2.The program must perform the following: a.Uses a Scanner object to ask the user: (The program asks for no other information) i.Full Name (First and Last name only) - stores this Full Name in one String object variable. ii.Age – must be read in as an int. iii.Profession or expected...
For this assignment, I need to write a c program named stick which plays a matchstick-picking...
For this assignment, I need to write a c program named stick which plays a matchstick-picking game. Given an initial number of sticks, players take turns picking either 1, 2, 3 or 4 sticks from a pile. Whoever picks the last stick wins. Usage The user can run stick with or without command line arguments. i.e. somebody can enter the number of sticks to begin play when the program is launched or they can be prompted after the program begins...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT