chutes and ladders

Posts 1–2 of 2 · Page 1 of 1
chutes and ladders
remember that game we played as a kid? spin a wheel, and move up the squares, and whenever we land on chutes, we go back down, but whenever we land on ladders we move up? i have to code the game in c++, and im having some problems. heres the code:

Code:
#include <iostream>
#include <cctype>
#include <string>
#include <cstdlib>

using namespace std;

void RollDiceAndMove(); //game rolls dice, and moves the player
void CheckChutes(); // game checks to see if player landed on chutes
void CheckLadders(); // game checks to see if player landed on ladders
void whoWon(); // game checkes to see if anyone has won yet
void welcomeMessage(); // shows display message, ans asks if they want to play
void goodbyeMessage(); // tells user goodbye after game

int main()
{
	bool player1Turn = 0; //switches between player1 and player2's turns
	string player1Name, player2Name; // gets names from both players
	int answer = 'y'; // answer when asked if players want to play
	int Player1Loc = 0, Player2Loc = 0; // location of the players

	void welcomeMessage(int answer);

	cout << "please enter your names: " << endl;
	cout << "Player1: ";
	cin >> player1Name;
	cout << "Player2: ";
	cin >> player2Name;

	while(answer == 'y'){
		//reset game variables
		while(whoWon ( ) == 0){
			if(Player2Loc != 100 && Player2Loc != 100){
				cout << player1Name << ": Its your turn." << endl;
				void RollDiceAndMove();
				void CheckChutes();
				void CheckLadders();
				//number they are on
				player1Turn = false;
			}
			else{
				cout << player2Name << ": Its your turn." << endl;
				void RollDiceAndMove();
				void CheckChutes();
				void CheckLadders();
				player1Turn = true;
			}
		}
	}
	void goodbyeMessage();

	return 0;
}

void welcomeMessage(int choice)
{
	// shows welcome message, and asks if they want to play
    // IN: choice
	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. Do you want to play?" << endl;
	cout << "press 'y' to play, or 'n' to quit." << endl;
	cin >> choice;
}

void goodbyeMessage()
{
	// shows the goodbye message
	cout << "Thanks for playing!" << endl;
}

void RollDiceAndMove (int Playerpos, int NewPos = 0, int RollDie = 0)
{
	// rolls dice, and moves the player
	switch(NewPos = 0)
	{
		for (int Player1pos = 0; Player1pos < 100; Player1pos++)
		{
			RollDie = rand() % 6 + 1; 
			Player1pos = 1;
			NewPos = Player1pos + RollDie;
			CheckChutes();
			CheckLadders();
			cout << "Your new location is: " << NewPos << endl;
			break;
		}
		for (int Player2pos = 0; Player2pos < 100; Player2pos++)
		{
			RollDie = rand() % 6 + 1;
			Player2pos = 1;
			NewPos = Player2pos + RollDie;
			CheckChutes();
			CheckLadders();
			cout << "Your new location is: " << NewPos << endl;
			break;
		}
	}
}
void whoWon(int NewPos)
{
	// determines if user has won, when their location is at 100
	int RollDiceAndMove();
	if (NewPos == 100){
		cout << "You won!!" << endl;
	}
}
void CheckChutes (int NewPos)
{
	//checks if chutes, if yes, then moves player backwards
	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 chutes, and have to move down" << endl;
}

void CheckLadders (int NewPos)
{
	// checks if ladders. if yes, moves player forwards
	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 ladders, and get to move up the board!" << endl;
}
errors i get:

-error C2120: 'void' illegal with all types
-warning C4060: switch statement contains no 'case' or 'default' labels
-error C2556: 'int RollDiceAndMove(void)' : overloaded function differs only by return type from 'void RollDiceAndMove(void)'
-see declaration of 'RollDiceAndMove'
-error C2371: 'RollDiceAndMove' : redefinition; different basic types

any help is appreciated. Thanks in advance!!
So, you dont know how to fix those errors but you made it?

error C2120: 'void' illegal with all types
while(whoWon ( ) == 0)..

whoWon() returns void (nothing), why are you comparing with 0? Non sense.

-error C2556: 'int RollDiceAndMove(void)' : overloaded function differs only by return type from 'void RollDiceAndMove(void)'
&
-error C2371: 'RollDiceAndMove' : redefinition; different basic types

int RollDiceAndMove(); ???

so this is how you call a function? inside another funtion? Not to mention RollDiceAndMove() has parameters

---

So, please, grab a book and go learn.
Posts 1–2 of 2 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us