边做边循环,无限循环

do-while loop, infinity loop

本文关键字:无限循环 循环      更新时间:2023-10-16

我有 2 件事不适用于此代码。第一件事是,在我的第二个 do-while 循环中,当我输入"g"或"G"并输入坐标并选择它是左还是右时,它会连续循环。
我的第二件不正确的事情是,当我发射激光时,挡板应该转动它并使其朝不同的方向发展。 这有效,但在找到挡板后的盒子之前,它不会转动激光。我对此非常困,任何帮助都会很棒,您也看到我应该修复的任何内容都将不胜感激。

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
using namespace std;
struct velocity
{
    int X = 0, Y = 0;
};
struct position
{
    int X = 0, Y = 0;
    void addVelocity(velocity V)
    {
        X += V.X;
        Y += V.Y;
    }
};
int gameBoard[10][10];
int guessBoard[10][10];
int random(int);
void laser(int&);
int boxNum(position, velocity);
position calcExitPos(velocity&, int&);
position moveLaser(position&, velocity V);
velocity checkPos(position, velocity&);
bool checkWall(position, velocity);
int main()
{
    char level, action;
    int num = 0;
    int score = 0, guess = 0, shots = 0, LorR = 0;
    int X = 0, Y = 0;
    for (int i = 0; i < 10; i++) 
    {
        for (int j = 0; j < 10; j++)
        {
            gameBoard[i][j] = 0;
        }
    }
    for (int i = 0; i < 10; i++)
    {
        for (int j = 0; j < 10; j++)
        {
            guessBoard[i][j] = 0;
        }
    }
    cout << "Welcome to the Baffle Game." << endl;
    cout << "  10   11  12  13  14  15  16  17  18  19" << endl;
    cout << "9|___|___|___|___|___|___|___|___|___|___|20" << endl;
    cout << "8|___|___|___|___|___|___|___|___|___|___|21" << endl;
    cout << "7|___|___|___|___|___|___|___|___|___|___|22" << endl;
    cout << "6|___|___|___|___|___|___|___|___|___|___|23" << endl;
    cout << "5|___|___|___|___|___|___|___|___|___|___|24" << endl;
    cout << "4|___|___|___|___|___|___|___|___|___|___|25" << endl;
    cout << "3|___|___|___|___|___|___|___|___|___|___|26" << endl;
    cout << "2|___|___|___|___|___|___|___|___|___|___|27" << endl;
    cout << "1|___|___|___|___|___|___|___|___|___|___|28" << endl;
    cout << "0|___|___|___|___|___|___|___|___|___|___|29" << endl;
    cout << "   39  38  37  36  35  34  33  32  31  30" << endl << endl;
    cout << "This is the game board, you will shoot lasers and try to find     all the baffles. " << endl << endl;
    //cout << "For beginner there are 4 baffles, for intermediate there are 7 baffles and for advanced there are 10 baffles." << endl;
    //cout << "Please type B for beginner, I for intermediate, and A for advanced" << endl;
    do
    {
        cout << "For beginner there are 4 baffles, for intermediate there are 7 baffles" << endl;
        cout << "and for advanced there are 10 baffles." << endl;
        cout << "Please type B for beginner, I for intermediate, and A for advanced" << endl;
        cin >> level;
    } while (level != 'b' && level != 'B' && level != 'i' && level != 'I' && level != 'a' && level != 'A');
        if (level == 'B' || level == 'b')
        {
            //Baffles equals 4
            num = random(4); // function returns to num
        }
        else if(level == 'I' || level == 'i')
        {
            //Baffles equals 7
            num = random(7);
        }
        else
        {
            //Baffles equals 10
            num = random(10);
        }           
        cout << "We will begin the game now." << endl;
        cout << "Here are some commands that you will need to know to play." << endl;
        cout << "L: Laser shot, then pick a number on the board to shoot the laser from." << endl;
        cout << "G: Guess the location of one baffle with coordinance and L or R to choose a left or right baffle." << endl;
        cout << "S: When you want me to print the score that you have." << endl;
        cout << "P: Print the board showing the baffles that you have already found." << endl;
        cout << "Q: Quit the game at any time" << endl;
        cout << "C: This shows you the board with all the baffles and which direction they are." << endl;
        do
        {
            cout << "Please type in your command." << endl;
            cin >> action;
            if (action == 'L' || action == 'l')
            {
                laser(shots);
            }
            else if (action == 'G' || action == 'g')
            {
                cout << "We will guess where the baffle is." << endl;
                cout << "Please enter the X coordinate." << endl;
                cin >> X;
                cout << "Please enter the Y coordinate." << endl;
                cin >> Y;
                //cout << gameBoard[X][Y] << endl;
                cout << "Please enter L and R for Left and Right Baffles." << endl;
                cin >> LorR;
                //cout << gameBoard[X][Y];
                if (gameBoard[X][Y] == 1)//Right
                {
                    //cout << "1" << endl;
                    if (LorR == 'R' || LorR == 'r')
                    {
                        cout << "Good job you got it correct." << endl;
                        guessBoard[X][Y] = 'R';
                        //points++
                    }
                    else
                        cout << "Wrong!" << endl;
                }
                else if (gameBoard[X][Y] == 2)//Left
                {
                    //cout << "2" << endl;
                    if (LorR == 'L' || LorR == 'l')
                    {
                        cout << "Good job you got it correct." << endl;
                        guessBoard[X][Y] = 'L';
                        //points++
                    }
                    else
                        cout << "Wrong!" << endl;
                }
                else if (gameBoard[X][Y] == 0)
                {
                    cout << "Your are wrong!" << endl;
                }
            }
            else if (action == 'S' || action == 's')
            {
                cout << "Number of shots: " << shots << endl;
                cout << "Number of guesses: " << guess << endl;
                cout << "Current score: " << endl;//Get score
            }
            else if (action == 'P' || action == 'p')
            {
                for (int i = 0; i < 10; i++)
                {
                    for (int j = 0; j < 10; j++)
                    {
                        cout << setw(2) << guessBoard[i][j] << "  ";
                    }
                    cout << endl;
                }
            }
            else if (action == 'C' || action == 'c')
            {
                for (int i = 0; i < 10; i++)
                {
                    for (int j = 0; j < 10; j++)
                    { 
                        cout << setw(2) << gameBoard[i][j] << "  ";
                    }
                    cout << endl;
                }
            }
            else if (action == 'q' || action == 'Q')
            {
            }
            else if (action != 'q' && action != 'Q')
            {
                cout << "*** Illegal command - Try again ***" << endl;
            }
            cout << "loop" << endl;
        } 
        while (action == 'q' || action == 'Q' || action == 'L' || action == 'l' || action == 'G' || action == 'g' || 
        action == 'S' || action == 's' || action == 'P' || action == 'p' || action == 'C' || action == 'c');
}
//This function finds a random number for the find a random index
// in the array.
int random(int randnum)
{
    unsigned seed = time(0); //get the system time.
    srand(seed); //seed the random number generator.
    int x, y, randomNum = 0;
    int count;
    for (count = 0; count < randnum; count++)
    {
        do 
        {
            x = (rand() % (9 - 0 + 1)) + 0;
            y = (rand() % (9 - 0 + 1)) + 0;
        } while (gameBoard[x][y] != 0);
        cout << "(" << x << "," << y << ")" << endl;
        randomNum = rand();
        gameBoard[x][y] = (randomNum % 2) + 1;
        cout << gameBoard[x][y] << endl;
        //2 = Left baffle
        //1 = right baffle
    }
    return gameBoard[x][y];
}
void laser(int &shots)  //Shoots a laser where user asks it to
{
    int shoot;
    cout << "Please input what number you want to shoot the laser from." << endl;
    cin >> shoot;
    shots++;
    velocity V;
    position P = calcExitPos(V,shoot);
    //checkPos(P, V);
    //cout << "1" << endl;
    do
    {
        checkPos(P, V);
        moveLaser(P, V);
        //cout << "1" << endl;
    } while (checkWall(P, V) == false);

    cout << "Laser shot #" << shots << " exited the box at " << boxNum(P, V) << endl;
}
 int boxNum(position P, velocity V) //Figures out what number the laser exits
{
    if (P.X == 0 && V.X == -1)
    {
        return 9 - P.Y;
    }
    else if (P.Y == 0 && V.Y == -1)
    {
        return 10 + P.X;
    }
    else if (P.X == 9 && V.X == 1)
    {
        return P.Y + 20;
    }
    else if (P.Y == 9 && V.Y == 1)
    {
        return 39 - P.X;
    }
}
position calcExitPos(velocity &V, int &box)//Figures out where the laser shoots from by the number the user inputs
{
    position P1;
    if (box >= 0 && box <= 9)
    {
        V.X = 1;
        V.Y = 0;
        P1.X = 0;
        P1.Y = (9 - box);
    }
    else if (box >= 10 && box <= 19)
    {
        V.X = 0;
        V.Y = 1;
        P1.X = (box - 10);
        P1.Y = 0;
    }
    else if (box >= 20 && box <= 29)
    {
        V.X = -1;
        V.Y = 0;
        P1.X = 9;
        P1.Y = (box - 20);
    }
    else if (box >= 30 && box <= 39)
    {
        V.X = 0;
        V.Y = -1;
        P1.X = (39 - box);
        P1.Y = 9;
    }
    return P1;
}
position moveLaser(position &P, velocity V)
{
    int baffletype = gameBoard[P.X][P.Y];
    if (baffletype == 1)//Right
    {
        if (V.X == 1)
        {
            V.Y = -1;
            V.X = 0;
            P.Y--;
        }
        else if (V.X == -1)
        {
            V.Y = 1;
            V.X = 0;
            P.Y++;
        }
        else if (V.Y == 1)
        {
            V.X = -1;
            V.Y = 0;
            P.X--;
        }
        else if (V.Y == -1)
        {
            V.X = 1;
            V.Y = 0;
            P.X++;
        }
    }
    else if (baffletype == 2)//Left
    {
        if (V.X == 1)
        {
            V.Y = 1;
            V.X = 0;
            P.Y++;
        }
        else if (V.X == -1)
        {
            V.Y = -1;
            V.X = 0;
            P.Y--;
        }
        else if (V.Y == 1)
        {
            V.X = 1;
            V.Y = 0;
            P.X++;
        }
        else if (V.Y == -1)
        {
            V.X = -1;
            V.Y = 0;
            P.X--;
        }
    }
    else if (baffletype == 0)
    {
        if (V.X == 1)
        {
            V.Y = 0;
            V.X = 1;
            P.X++;
        }
        else if (V.X == -1)
        {
            V.Y = 0;
            V.X = -1;
            P.X--;
        }
        else if (V.Y == 1)
        {
            V.X = 0;
            V.Y = 1;
            P.Y++;
        }
        else if (V.Y == -1)
        {
            V.X = 0;
            V.Y = -1;
            P.Y--;
        }
    }
    cout << "(" << P.X << ","<< P.Y << ")" << endl;
    return P;
}
velocity checkPos(position P, velocity &V) //Directs the laser when the laser hits a baffle
{
    int baffletype = gameBoard[P.X][P.Y];
    if (baffletype == 1)//Right
    {
        if (V.X == 1)
        {
            V.Y = -1;
            V.X = 0;
        }
        else if (V.X == -1)
        {
            V.Y = 1;
            V.X = 0;
        }
        else if (V.Y == 1)
        {
            V.X = -1;
            V.Y = 0;
        }
        else if (V.Y == -1)
        {
            V.X = 1;
            V.Y = 0;
        }
    }
    else if (baffletype == 2)//Left
    {
        if (V.X == 1)
        {
            V.Y = 1;
            V.X = 0;
        }
        else if (V.X == -1)
        {
             V.Y = -1;
             V.X = 0;
        }
        else if (V.Y == 1)
        {
            V.X = 1;
            V.Y = 0;
        }
        else if (V.Y == -1)
        {
            V.X = -1;
            V.Y = 0;
        }
    }
    else if (baffletype == 0)
    {
        if (V.X == 1)
        {
            V.Y = 0;
            V.X = 1;
            P.X++;
        }
        else if (V.X == -1)
        {
            V.Y = 0;
            V.X = -1;
            P.X--;
         }
        else if (V.Y == 1)
        {
            V.X = 0;
            V.Y = 1;
            P.Y++;
        }
        else if (V.Y == -1)
        {
            V.X = 0;
            V.Y = -1;
            P.Y--;
        }
     }
    //cout << "( " << P.X << ", " << P.Y << ")" << endl;
    return V;
}
bool checkWall(position P, velocity V) //Checks to see if the laser hits the wall
{
    //cout << "2" << endl;
    P.addVelocity(V);
    return (P.X > 9 || P.X < 0 || P.Y > 9 || P.Y < 0); //Checks to see if the next position is out of bounds
}

我没有一个完整的解决方案给你,只有几件事。

  1. 您应该使while循环测试更简单。这个怎么样(伪代码):

    bool done = false;
    while (!done) {
        // Process action loop
        cin >> action; 
        action = toupper(action);   // Make everything upper case
        processTheAction(action);    
        done   = areWeDoneYet(action);
    }
    
  2. 将复杂的"我们完成了吗"任务和其他事情分解为功能。(分而治之)

    bool areWeDoneYet(action) {
    switch (action) {
      case A:
      case B:
        ... (cases where we are not done)
        return false;
      case E:
      case F:
        ... (cases where we are done)
        return true;
      }
    }
    
  3. 让您的"事件循环"变得简单。 让它调用一个复杂的函数"进程操作" 这样,您就可以判断在遇到问题时是否正忙于处理,或者事件循环是否有问题。

    void processTheAction( action ) {
    switch (action) {
       case A:
       case a:   // Optional method if you don't do topper on Action
                 // Code for action 'A' goes here
                 // If a *lot* of code make a function like "ProcessActionA()"
           break;   
       case B:
       case b:  // not needed if you do topper
               // Code for action 'B' goes here
          break;
       default: 
         printf("I'm hosed cause I forgot to manage action [%c]n",
         action);
       }
     }
    

不知道这是否有帮助,但它可能会帮助您解决问题。