无法使switch()功能正常工作 - Chutes&梯子计划

时间:2016-02-11 05:43:49

标签: c++ switch-statement void

我正在制作一个Chutes& Ladders游戏和我似乎无法获得我的虚拟RollDiceAndMove()函数来为我的玩家创造新的位置。它应该使用rand(time)创建一个新位置,然后将该新位置发送回int main()中的while循环。

任何帮助将不胜感激!我是C ++的新手。

#include <iostream>
#include <string>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cctype>

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 checks to see if anyone has won yet
void welcomeMessage(); // shows display message, and asks if they want to play
void goodbyeMessage(); // tells user goodbye after game

int RollDie;
int NewPos;
int NewPos1;




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



welcomeMessage();

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



while(choice == 'y'){
        while(Player1Loc != 100 && Player2Loc != 100){
                if(player1Turn){
                    cout << endl << endl;
                    cout << player1Name << ": Its your turn." << endl << endl;
                    cout << "Press Enter to Start Turn: ";
                    cin.get();
                    cout << endl << endl;
                    RollDiceAndMove();
                    cout << "New Position Amount: " << NewPos1 << endl;
                    /* CheckChutes();
                    CheckLadders() */;
                    player1Turn = false;
                }
                else{
                    cout << endl << endl;
                    cout << player2Name << ": Its your turn." << endl << endl;
                    cout << "Press Enter to Start Turn: ";
                    cin.get();
                    cout << endl;
                    RollDiceAndMove();
                    cout << "New Position Amount: " << NewPos << endl;
                    // CheckChutes();
                    // CheckLadders();
                    player1Turn = true;
                }
            }
        while (NewPos == 100) {
            whoWon();
        }
}


    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." << endl;

}

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

void RollDiceAndMove ()
{
    // rolls dice, and moves the player
    switch(NewPos = 100)
    {
        for (int Player1pos = 0; Player1pos < 100; Player1pos++)
        {
            RollDie = rand() % 6 + 1; 
            Player1pos = 1;
            NewPos1 = 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
    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;
}

2 个答案:

答案 0 :(得分:1)

RollDiceAndMove()中的switch语句语法不正确,因此代码只是每次都跳过整个函数。以下是一个简单的教程。

http://www.tutorialspoint.com/cplusplus/cpp_switch_statement.htm

此外,代码还有其他各种“问题”。您正在函数中创建局部NewPos变量,这会导致函数内部的未初始化(具有随机值)变量,而不是您期望的全局变量。在函数内部,NewPos指的是在调用函数时创建的局部变量,并在返回时被销毁; ::NewPos指的是您的全局变量。 cin>>player2Name;std::cin流中留下了一个换行符cin.get();,后续的while占用了一行,因此它的行为不会像您预期的那样。使用lLeft.setMaxHeight(USE_PREF_SIZE); lLeft.setMinHeight(USE_PREF_SIZE); 语句很奇怪。您的代码几乎完全由全局变量和全局void函数组成;尽可能避免这两种情况。

如果你想学习C ++,我建议你拿一本好书并学好语言。以下链接中的第一个是个人喜爱的。

The Definitive C++ Book Guide and List

答案 1 :(得分:0)

很抱歉,如果这是一个评论,但由于声誉太低,我无法发表评论。

在RollDiceAndMove函数中,switch语句的语法错误。查看cplusus.com了解详细信息,但基础知识是:

switch (var)
{
     case val:
         //code
         break; //optional
     case val2:
         //code
         break; //optional
      ....and so on 
 }

其中var是具有关联的整数值的对象,例如int或enumerator对象,case参数中的int val是switch var对象可以具有的值。

确保在case语句的末尾使用break关键字,以避免“落入”代码应执行的以下case语句。

Cplusplus链接:[http://www.cplusplus.com/doc/tutorial/control/]