Dungeon Crawler Start

Hi, I'm making a dungeon crawler type game, and the first thing I am trying to do is get the board to work with a player that can move around on it. For some reason, it outputs the board the first time and then asks for input then doesnt output the board again. Thanks for your help!

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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <iostream>
#include <conio.h>

using namespace std;
void outputBoard(int playerRow, int playerColumn);
char board [8] [21];
char entrance[3], exit[3];
int playerRow = 0, playerColumn = 11;
int numOfMoves = 1;
int playerMove(int playerRow, int playerColumn, char entrance[3]);
int main()
{
    outputBoard(playerRow, playerColumn);
    do{
    playerMove(playerRow, playerColumn, entrance);
    outputBoard(playerRow, playerColumn);
    ++numOfMoves;
    }while(playerRow < 9);
    return 0;
}

void outputBoard(int playerRow, int playerColumn)
{
    if (numOfMoves == 1)
    {
        entrance[1] = 'O';
    }
    else
    {
        board[playerRow] [playerColumn] = 'O';
    }
    cout << " ---------"<<exit[0]<<exit[1]<<exit[2]<<"---------\n";
    for (int x = 0; x<8; ++x)
    {
        cout << "|";
        for (int y = 0; y<21; ++y)
        {
            cout << board[x] [y];
        }
        cout << "|\n";
    }
    cout << " ---------"<<entrance[0]<<entrance[1]<<entrance[2]<<"---------\n";
    if (numOfMoves == 1)
    {
        entrance[1] = ' ';
    }
    else
    {
        board [playerRow] [playerColumn] = ' ';
    }
}
int playerMove (int playerRow, int playerColumn, char entrance[3])
{
    bool validInput = true;
    char playerMoveChoice;
    do
    {
        cin >> playerMoveChoice;
        if (playerMoveChoice == 'W' || playerMoveChoice == 'w')
        {
            ++playerRow;
        }
        else if ((playerMoveChoice == 'A' || playerMoveChoice == 'a') && playerColumn > 0)
        {
            if(numOfMoves == 0)
            {
                entrance[0] = 'O';
            }
            else
            {
                --playerColumn;
            }
        }
        else if ((playerMoveChoice == 'D' || playerMoveChoice == 'd') && playerColumn < 22)
        {
            if (numOfMoves == 0)
            {
                entrance[2] = 'O';
            }
            else
            {
                ++playerColumn;
            }
        }
        else if ((playerMoveChoice == 'S' || playerMoveChoice == 's') && numOfMoves != 0)
        {
            --playerRow;
        }
        else
        {
            cout << "Invalid input, please re-input.\n";
            validInput = false;
        }
    }while(validInput);
    return 0;
}
The do while loop in playerMove() will go on as long as input is valid i'm guessing it should be the other way around. You could try something like this:

1
2
3
4
5
6
7
8
9
bool validInput = false;
do {
    if(/*valid move*/) {
        //do stuff
        validInput = true;
    else {
        cout << "Invalid input" << endl;
    }
}while(!validInput);

Thank you! Now, when I input the move choice, it doesn't move the player correctly, it moves it like so:
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
Starting position.
 ---------   ---------
|                     |
|                     |
|                     |
|                     |
|                     |
|                     |
|                     |
|                     |
 --------- O ---------

After 1st input of 'W.'
 ---------   ---------
|                     |
|                     |
|                     |
|                     |
|                     |
|                     |
|                     |
|                     |
 --------- O ---------
After 2nd input of 'W.'
 ---------   ---------
|           O         |
|                     |
|                     |
|                     |
|                     |
|                     |
|                     |
|                     |
 ---------   ---------


I can't find the reason why.
Last edited on
Could you post your update code?
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <iostream>
#include <conio.h>

using namespace std;
void outputBoard(int playerRow, int playerColumn);
char board [8] [21];
char entrance[3], exit[3];
int playerRow = 0, playerColumn = 11;
int numOfMoves = 1;
int playerMove(int playerRow, int playerColumn, char entrance[3]);
int main()
{
    outputBoard(playerRow, playerColumn);
    do{
    playerMove(playerRow, playerColumn, entrance);
    outputBoard(playerRow, playerColumn);
    ++numOfMoves;
    }while(playerRow < 9);
    return 0;
}

void outputBoard(int playerRow, int playerColumn)
{
    if (numOfMoves == 1)
    {
        entrance[1] = 'O';
    }
    else
    {
        board[playerRow] [playerColumn] = 'O';
    }
    cout << " ---------"<<exit[0]<<exit[1]<<exit[2]<<"---------\n";
    for (int x = 0; x<8; ++x)
    {
        cout << "|";
        for (int y = 0; y<21; ++y)
        {
            cout << board[x] [y];
        }
        cout << "|\n";
    }
    cout << " ---------"<<entrance[0]<<entrance[1]<<entrance[2]<<"---------\n";
    if (numOfMoves == 1)
    {
        entrance[1] = ' ';
    }
    else
    {
        board [playerRow] [playerColumn] = ' ';
    }
}
int playerMove (int playerRow, int playerColumn, char entrance[3])
{
    bool validInput = true;
    char playerMoveChoice;
    do
    {
        playerMoveChoice = getch();
        if (playerMoveChoice == 'W' || playerMoveChoice == 'w')
        {
            ++playerRow;
        }
        else if ((playerMoveChoice == 'A' || playerMoveChoice == 'a') && playerColumn > 0)
        {
            if(numOfMoves == 0)
            {
                entrance[0] = 'O';
            }
            else
            {
                --playerColumn;
            }
        }
        else if ((playerMoveChoice == 'D' || playerMoveChoice == 'd') && playerColumn < 22)
        {
            if (numOfMoves == 0)
            {
                entrance[2] = 'O';
            }
            else
            {
                ++playerColumn;
            }
        }
        else if ((playerMoveChoice == 'S' || playerMoveChoice == 's') && numOfMoves != 0)
        {
            --playerRow;
        }
        else
        {
            cout << "Invalid input, please re-input.\n";
            validInput = false;
        }
    }while(!validInput);
    return 0;
}
Since your variables are global and then you declare new ones for the function they use the locally declared ones instead of globals.

1
2
3
4
5
6
int playerRow = 0;  //this is the one you want to use

void movePlayer(int playerRow) {
    playerRow++;  //This one is the one you are using so now you have locally playerRow = 1
                            //and globally a playerRow = 0
}


PS: sorry if i couldn't explain properly rushing out of my house.
Last edited on
Thank you!
Topic archived. No new replies allowed.