In: Computer Science
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.
#include
#include
#include
#include
using namespace std;
int main ()
{
players[i]=0; Here I am reading tiles from file "input.txt" as 10
//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
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]]<
cout<<"You have landed on a ladder, please move up
"<
cout<< "You have landed on a snake, please move back
"<
cout << "current position = "
<
//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<
//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;
}
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