border test for simple battleship game

-having problems debugging border detection code
-does not allow me to move at all when in any corner
-when approaching regular borders, players will cross over the border and end up on the other side of the board
-program is supposed to take in all of the player's actions at once and execute them accordingly
-the problem with this is if I'm stuck in a corner, turn and then try to move, the program still won't allow me to move
Here is the border detection code along with the menu and switch statement as well as the forward function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
      cout << "Select 3 actions from the menu (actions can be repeated)" << endl;
      cout << "********************************************************" << endl;
      cout << "* 1: Forward (move straight one space)                 *" << endl;
      cout << "* 2: Left (turn to the left within the same square)    *" << endl;
      cout << "* 3: Right (turn to the right withing the same square) *" << endl;
      cout << "* 4: Ping (check to see if the sub is within range)    *" << endl;
      cout << "* 5: Depth Charge (attempt to attack the target)       *" << endl;
      cout << "********************************************************" << endl;
            cin >> pcChoice[0] >> pcChoice[1] >> pcChoice[2];
            for(int i = 0; i < 3; i++)
            {
                while(pcChoice[i] == 1 && 
                     (0 >= des.pos1 - 1 || (SIZE - 1) <= des.pos1 + 1) &&
                     (0 >= des.pos2 - 1 || (SIZE - 1) <= des.pos2 + 1))
                {
                    cout << "You are facing " << des.direction << " and cannot move forward." << endl;
                    cout << "Please choose a different action: ";
                    cin >> pcChoice[i];
                }
            }
            for (int i = 0; i < 3; i++)
            {
                switch(pcChoice[i])
                {
                    case 1:
                        forward(des.pos1, des.pos2, des.direction);
                        break;
                    case 2:
                        turnLeft(des.direction);
                        cout << "You are now facing " << des.direction << "." << endl;
                        break;
                    case 3:
                        turnRight(des.direction);
                        cout << "You are now facing " << des.direction << "." << endl;
                        break;
                    case 4:
                        pingSub(sub.pos1, sub.pos2, des.pos1, des.pos2);
                        break;
                    case 5:
                        depthCharge(sub.pos1, sub.pos2, des.pos1, des.pos2, sub.hp, des.numAtk);
                        break;
                }
            }

void forward(int &uniPos1, int &uniPos2, char uniDirection)
{
    //if the direction is N
    if(uniDirection == 'N')
        //uniPos2 += 1
        uniPos1 -= 1;
    //else if the direction is S
    else if(uniDirection == 'S')
        //uniPos2 -= 1
        uniPos1 += 1;
    //else if the direction is E
    else if(uniDirection == 'E')
        //uniPos1 += 1
        uniPos2 += 1;
    //else uniPos1 -= 1
    else
        uniPos2 -= 1;
}


Any comments, thoughts, or ideas would be greatly appreciated!!
For your border checking wouldn't you want to allow them to move to 0 and size - 1 like this:
1
2
3
4
while(pcChoice[i] == 1 && 
                     (0 > des.pos1 - 1 || (SIZE - 1) < des.pos1 + 1) &&
                     (0 > des.pos2 - 1 || (SIZE - 1) < des.pos2 + 1))
                {

It would seem that the ship should be able to move to position (0, 0), (0, Size - 1), (Size - 1, 0), and (Size - 1, Size - 1)

Not sure exactly how this works, as we can't see exactly how this fits into the rest of your program... but I'm assuming that the + and - 1's in that code are the result of the ship's desired movement.


Also, if you reset pChoice[i] wouldn't you want to check it again (put an i--; after the cin >> pChoice[i]; in the while loop):
1
2
3
4
5
6
7
8
9
while(pcChoice[i] == 1 && 
                     (0 >= des.pos1 - 1 || (SIZE - 1) <= des.pos1 + 1) &&
                     (0 >= des.pos2 - 1 || (SIZE - 1) <= des.pos2 + 1))
                {
                    cout << "You are facing " << des.direction << " and cannot move forward." << endl;
                    cout << "Please choose a different action: ";
                    cin >> pcChoice[i];
                    i--;
                }

Last edited on
Topic archived. No new replies allowed.