do-while循环,无限循环

时间:2016-02-19 00:23:44

标签: c++ loops do-while infinite

我有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 个答案:

答案 0 :(得分:1)

我没有完整的解决方案,但有几件事情。

  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. 简化“事件循环”。让它称为复杂的功能 “ProcessAction”通过这种方式,您可以判断在遇到问题时是否正忙于处理,或者您是否遇到事件循环问题。

    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);
       }
     }
    
  4. 不知道这是否有帮助,但它可以帮助您解决问题。