1

I have 2 things that aren't working with this code. The first thing is that in my second do-while loop in main, when I input 'g' or 'G' and I put in coordinates and select if it is left or right it loops continuously.
My second thing that doesn't work right is that when I shoot my laser, a baffle should turn it and make it go a different direction. This works but it doesn't turn the laser until the box after the baffle is located. I am so stuck with this, any help would be great, and anything that you also see that I should fix would be greatly appreciated.

#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
}
Sarah
  • 27
  • 4
  • Have a look at this: http://stackoverflow.com/questions/25475384/when-and-why-do-i-need-to-use-cin-ignore-in-c – Chad Feb 19 '16 at 00:26

1 Answers1

1

I don't have a complete solution for you but a couple of things.

  1. You should make your while loop test simpler. How about this (pseudocode):

    bool done = false;
    while (!done) {
        // Process action loop
        cin >> action; 
        action = toupper(action);   // Make everything upper case
    
        processTheAction(action);    
    
        done   = areWeDoneYet(action);
    
    }
    
  2. Break up the complex "are we done yet" task and other things into functions. (Divide and conquer)

    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. Make your "event loop" simple. Have it call a complex function "ProcessAction" That way you can tell if you are busy processing when you have an issue, or if your having an issue with the event loop.

    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);
       }
     }
    

Don't know if this helps, but it might help you to break down the problem.

Ross Youngblood
  • 502
  • 1
  • 3
  • 16