In: Computer Science
Fly By Night Games Company has decided to hire you to program their new board game simulator for the game “Chutes & Ladders”. The board has squares which are numbered from 1 to 100 and players have counters which start on the theorectical square 0. The players take turns at spinning a spinner with the numbers 1 to 6 on it, and each moves his or her counter forward the number of squares corresponding to the number on the spinner. The first person to reach square 100 is the winner. The interest is caused by the fact that pairs of squares are connected together by "ladders" (which connect a lower-numbered square to a higher-numbered square) and "chutes" (which run from high to low). If a counter lands on the start of a chute or ladder, the counter is moved to the corresponding square at the end of the chute or ladder. Note that landing on the end square of a ladder or a chute has no effect, only the start square counts. If a player is on square 95 or higher, then a spin which takes them past 100 must be ignored (for example, a player on square 99 must ignore all spins which are not 1) and the player should be told that the spin is ignored. Ladders and Chutes are shown in the tables below (on page 2). In C++, using functions and loops: 1. Design your game so the user plays in the following manner: Allow two players to play the game and ask the users for their names. The player "spins" and is told the result of their spin. If they land on the start of a chute or ladder, they are told about it. The player should be told where they end up. The other player then spins. Once a player has reached 100, the game ends with an appropriate message. Then, offer to play again. 2. Note that once a player has reached 100, the game should immediately end. The other player does not get another turn. 3. When your program starts execution, it needs to display a welcome message that describes the program to user in a short, but clear message. Blank lines appear before and after the welcome message to help user read the screen. In addition, goodbye message should be displayed to wrap up the program before exiting. Again, blank lines appear before and after the goodbye message to help user read the screen.
#include<cstdlib>
#include <iostream>
using namespace std;
int counter1;
int counter2;
int spinner;
int spin;
char name1[20];
char name2[20];
int opinion1;
int opinion2;
void player1()
{
cout<<"Now it is the turn of "<<name1<<" to play"<<endl;
cout<<"enter 1 to spin";
while(spin!=1)
cin>>spin;
spinner=rand()%6+1;
cout<<"The result of spin is "<<spinner<<endl;
if(counter1>=95 && counter1+spinner>100)
cout<<"spin is ignored"<<endl;
else
{
counter1+=spinner;
cout<<"You ended up at the square "<<counter1<<endl;
}
}
void player2()
{
cout<<"Now it is the turn of "<<name2<<" to play"<<endl;
cout<<"enter 1 to spin";
while(spin!=1)
cin>>spin;
spinner=rand()%6+1;
cout<<"The result of spin is "<<spinner<<endl;
if(counter2>=95 && counter2+spinner>100)
cout<<"spin is ignored"<<endl;
else
{
counter2+=spinner;
cout<<"You ended up at the square "<<counter2<<endl;
}
}
void start()
{
cout<<"\n"<<"Welcome to Chutes and Ladders"<<"\n";
cout<<"\nEnter the name of player1";
cin>>name1;
cout<<"Enter the name of player2";
cin>>name2;
}
int main() {
start();
while(1)
{
counter1=0;
counter2=0;
while(counter1!=100 && counter2!=100)
{
spin=0;
player1();
spin=0;
player2();
}
if(counter1==100)
cout<<name1<<" is the winner";
else
cout<<name2<<" is the winner";
cout<<"You are invited to play again"<<endl;
cout<<name1<<" enter 1 to play again";
cin>>opinion1;
cout<<name2<<" enter 1 to play again";
cin>>opinion2;
if(opinion1!=1 || opinion2!=1)
exit(0);
}
cout<<"\nGoodbye"<<endl;
}