#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <unistd.h>
using namespace std;
void welcome();
void goodbye();
void CheckLadders();
void CheckChutes();
int spin();
string orderPlay();
const char YES = 'Y';
const int MAX = 6;
const int MIN = 1;
int currPos1, currPos2, dice, newPos1, newPos2;
const int END = 100;
string playerName1, playerName2;
int main()
{
srand(time(0)); // srand(time(NULL));
newPos1 = 0;
newPos2 = 0;
currPos2 = 0;
currPos1 = 0;
char choice;
//dice = 0;
//initialize all the variables as zero (reset)
do{
//while (newLocation < 100 && currentLocation >= 99)
orderPlay();
cout << "Press enter to spin...";
cin.ignore();
cout << "You threw " << dice << ".";
spin();
cout << playerName1 << "Your current location is: " << currPos1 << endl;
cout << playerName2 << "Your current location is: " << currPos2 << endl;
if (playerTurn == "p1" || playerTurn == "P2"){ //call plyerTurn from orderPlay...dont knw if its correct
newPos1 = currPos1 + dice;
cout << playerName1 << "Your new location is: " << newPos1 << endl;
}else{
newPos2 = currPos2 + dice;
cout << playerName2 << "Your new location is: " << newPos2 << endl;
}
//////////////////////////////////////////////////////////////////////////////////////
cout << "Do you want to play again? (y/n) ";
cin >> choice;
cout << endl;
} while (choice == YES);
if(choice != YES)
goodbye();
return 0;
}
void welcome(){
cout << "Welcome to the chutes and ladders game. Both " << endl;
cout << "players start at 0, and the first one to 100 " << endl;
cout << "wins the game. However, if you land on a chute," << endl;
cout << "your player will move down, but a ladder " << endl;
cout << "will move you up." << endl;
}
void goodbye(){
cout << endl
<< endl;
cout << "Thank you for playing the game, I hope you enjoyed it!";
cout << endl
<< endl;
}
int spin(){
dice = rand() % ((MAX - MIN) + 1) + MIN;
return dice;
}
string orderPlay(){ // should only be for player turn without names i think
string playerTurn;
cout << "Please initiate which player is going first (P1/P2): ";
cin >> playerTurn;
if (playerTurn == "P1" || playerTurn == "p1"){
cout << "Player 1 enter your name: ";
getline(cin, playerName1);
cout << endl;
cout << playerName1 << "'s turn" << endl;
cout << endl;
}else{
cout << "Player 2 enter your name: ";
getline(cin, playerName2);
cout << endl;
cout << playerName2 << "'s turn" << endl;
cout << endl;
}
return playerTurn;
}
void CheckChutes (int NewPos)
{
//checks for chutes
if (NewPos == 98)
NewPos = 78;
else if (NewPos == 95)
NewPos = 75;
else if (NewPos == 93)
NewPos = 70;
else if (NewPos == 87)
NewPos = 24;
else if (NewPos == 64)
NewPos = 60;
else if (NewPos == 62)
NewPos = 19;
else if (NewPos == 56)
NewPos = 53;
else if (NewPos == 49)
NewPos = 11;
else if (NewPos == 48)
NewPos = 26;
else if (NewPos == 16)
NewPos = 6;
else
NewPos = NewPos;
cout << "You landed on a chute, and have to move down" << endl;
}
void CheckLadders (int NewPos)
{
//checks for ladders
if (NewPos == 1)
NewPos = 38;
else if (NewPos == 4)
NewPos = 14;
else if (NewPos == 9)
NewPos = 21;
else if (NewPos == 23)
NewPos = 44;
else if (NewPos == 28)
NewPos = 84;
else if (NewPos == 36)
NewPos = 44;
else if (NewPos == 51)
NewPos = 66;
else if (NewPos == 71)
NewPos = 90;
else if (NewPos == 80)
NewPos = 100;
else
NewPos = NewPos;
cout << "You landed on a ladder, and get to move up the board!" << endl;
}
I am fairly new to C++ and programming overall, I am in the middle of doing my assignment chutes and ladders, and I have encountered a problem.
I don't know how to use the playerTurn variable from orderPlay function in the main function, please be nice I know this might sound stupid, all help is greatly appreciated.
please disregard the comments some of them are stupid.