Dungeon Crawler Monster Problem

Hi, I already posted another topic about this program, but since then, I have done more work on it and added monsters into the program. This completely messed everything up, and whenever I tried to run the program, it terminates with status -1073741819. I didn't have the slightest idea what that meant. I tried running the debugger on the program, and I got this error, Program received signal SIGSEGV, Segmentation fault. I also don't have the slightest idea what that means. Could someone please explain to me what the problem is, and give me some tips on how to solve it. Thanks!

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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include <iostream>
#include <conio.h>
#include <cstdlib>
#include <ctime>

using namespace std;
class monster
{
public:
    int monsterRow;
    int monsterColumn;
};
void outputBoard(monster monsters[]);
char board [8] [21];
int playerRow = 0, playerColumn = 10;
int numOfMoves = 0;
int playerMove();
int numOfMonsters;
int monsterMove(monster monsters[]);

int main()
{
    cout << "How many monsters would you like there to be? (max 4)\n";
    do
    {
     cin >> numOfMonsters;
     if (numOfMonsters > 4)
     {
         cout << "Too many monsters, please input a number less than 5.\n";
     }
    }while(numOfMonsters > 4);
    monster monsters[numOfMonsters];
    outputBoard(monsters);
    do{
    monsterMove(monsters);
    playerMove();
    outputBoard(monsters);
    ++numOfMoves;
    }while(playerRow < 8 || numOfMonsters != 0);
    cout << "You win!\n";
    return 0;
}

void outputBoard(monster monsters[])
{

    board[playerRow] [playerColumn] = 'O';
    for (int x = 0; x<numOfMonsters; ++x)
    {
        board [monsters[x].monsterRow] [monsters[x].monsterColumn] = 'X';
    }
    cout << " ---------   ---------\n";
    for (int x = 7; x>=0; --x)
    {
        cout << "|";
        for (int y = 0; y<21; ++y)
        {
            cout << board[x] [y];
        }
        cout << "|\n";
    }
    cout << " ---------------------\n";
    board [playerRow] [playerColumn] = ' ';
}
int playerMove ()
{
    bool validInput;
    char playerMoveChoice;
    do
    {
        validInput = true;
        playerMoveChoice = getch();
        if (playerMoveChoice == 'W' || playerMoveChoice == 'w')
        {
            if (playerRow+1 != 8 || (playerColumn > 9 && playerColumn < 12))
            {
            ++playerRow;
            }
            else
            {
                cout << "Invalid input, you cannot leave the board, please re-input.\n";
                validInput = false;
            }
        }
        else if (playerMoveChoice == 'A' || playerMoveChoice == 'a')
        {
            if (playerColumn > 0)
            {
                --playerColumn;
            }
            else
            {
                cout << "Invalid input, you cannot leave the board, please re-input.\n";
                validInput = false;
            }
        }
        else if (playerMoveChoice == 'D' || playerMoveChoice == 'd')
        {
            if (playerColumn+1 != 21)
            {
                ++playerColumn;
            }
            else
            {
                cout << "Invalid input, you cannot leave the board, please re-input.\n";
                validInput = false;
            }
        }
        else if ((playerMoveChoice == 'S' || playerMoveChoice == 's') && numOfMoves != 0)
        {
            if (playerRow >0 && playerRow <=8)
            {
            --playerRow;
            }
            else
            {
                cout << "Invalid input, you cannot leave the board, please re-input.\n";
                validInput = false;
            }
        }

        else
        {
            cout << "Invalid input, please re-input.\n";
            validInput = false;
        }
    }while(!validInput);
    return 0;
}
int monsterMove(monster monsters[])
{
    srand(time(NULL));
    for (int x = 0; x<numOfMonsters; ++x)
    {
        monsters[x].monsterRow = rand() % 8;
        monsters[x].monsterColumn = rand() % 21;
    }
    return 0;
}
What compiler / IDE are you using? Technically you shouldn't be able to compile this at all, because on line 32 you're attempting to create an array of non-const size on the stack.
Well one thing I see is on line 50. You're using uninitialized variables as a subscript.
@xismn
I'm using Code::Blocks

@Cody0023
I thought that the way I am doing it, that I am using things from the class so they would be initialized.
1
2
3
4
5
6
class monster
{
public:
    int monsterRow;
    int monsterColumn;
};


Have you considered using a struct since it's a POD.
1
2
3
4
struct Monster
{
    int monsterRow, monsterColumn;
};


Or even a pair.

1
2
3
4
#include <utility>

std::pair<int, int> monster; //monser.first = row
//monster.second = column 
Last edited on
@giblit
What would be different if I used a struct instead of a class?
No difference, it is just that people often use structs for POD(plain-old-data) structures and classes for everything else.
http://stackoverflow.com/questions/146452/what-are-pod-types-in-c
Thanks, for the help, but doing the things y'all have suggested hasn't fixed the program.
Last edited on
Can we see the new code?

The major problems I see cody and xism already mentioned.

line 32 should be: monster *monsters = new monster[numOfMonsters];

Then when you call line 33 outputBoard(monsters); and get to board [monsters[x].monsterRow] [monsters[x].monsterColumn] = 'X'; you are going to be placing x's at random out of bounds locations more than likely. You never assigned rows/columns for the monsters. The compiler can't read minds it has no idea where you want them to be.
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include <iostream>
#include <conio.h>
#include <cstdlib>
#include <ctime>

using namespace std;
struct monster
{
public:
    int monsterRow;
    int monsterColumn;
};
void outputBoard(monster monsters[]);
char board [8] [21];
int playerRow = 0, playerColumn = 10;
int numOfMoves = 0;
int playerMove();
int numOfMonsters;
int monsterMove(monster monsters[]);

int main()
{
    cout << "How many monsters would you like there to be? (max 4)\n";
    do
    {
     cin >> numOfMonsters;
     if (numOfMonsters > 4)
     {
         cout << "Too many monsters, please input a number less than 5.\n";
     }
    }while(numOfMonsters > 4);
    monster *monsters = new monster[numOfMonsters];
    outputBoard(monsters);
    srand(time(NULL));
    for (int x = 0; x<numOfMonsters; ++x)
    {
        monsters[x].monsterRow = rand() % 8;
        monsters[x].monsterColumn = rand() % 21;
    }
    do{
    monsterMove(monsters);
    playerMove();
    outputBoard(monsters);
    ++numOfMoves;
    }while(playerRow < 8 || numOfMonsters != 0);
    cout << "You win!\n";
    return 0;
}

void outputBoard(monster monsters[])
{

    board[playerRow] [playerColumn] = 'O';
    for (int x = 0; x<numOfMonsters; ++x)
    {
        board [monsters[x].monsterRow] [monsters[x].monsterColumn] = 'X';
    }
    cout << " ---------   ---------\n";
    for (int x = 7; x>=0; --x)
    {
        cout << "|";
        for (int y = 0; y<21; ++y)
        {
            cout << board[x] [y];
        }
        cout << "|\n";
    }
    cout << " ---------------------\n";
    board [playerRow] [playerColumn] = ' ';
    for (int x = 0; x<numOfMonsters; ++x)
    {
        board [monsters[x].monsterRow] [monsters[x].monsterColumn] = ' ';
    }
}
int playerMove ()
{
    bool validInput;
    char playerMoveChoice;
    do
    {
        validInput = true;
        playerMoveChoice = getch();
        if (playerMoveChoice == 'W' || playerMoveChoice == 'w')
        {
            if (playerRow+1 != 8 || (playerColumn > 9 && playerColumn < 12))
            {
            ++playerRow;
            }
            else
            {
                cout << "Invalid input, you cannot leave the board, please re-input.\n";
                validInput = false;
            }
        }
        else if (playerMoveChoice == 'A' || playerMoveChoice == 'a')
        {
            if (playerColumn > 0)
            {
                --playerColumn;
            }
            else
            {
                cout << "Invalid input, you cannot leave the board, please re-input.\n";
                validInput = false;
            }
        }
        else if (playerMoveChoice == 'D' || playerMoveChoice == 'd')
        {
            if (playerColumn+1 != 21)
            {
                ++playerColumn;
            }
            else
            {
                cout << "Invalid input, you cannot leave the board, please re-input.\n";
                validInput = false;
            }
        }
        else if ((playerMoveChoice == 'S' || playerMoveChoice == 's') && numOfMoves != 0)
        {
            if (playerRow >0 && playerRow <=8)
            {
            --playerRow;
            }
            else
            {
                cout << "Invalid input, you cannot leave the board, please re-input.\n";
                validInput = false;
            }
        }

        else
        {
            cout << "Invalid input, please re-input.\n";
            validInput = false;
        }
    }while(!validInput);
    return 0;
}
int monsterMove(monster monsters[])
{
    for (int x = 0; x<numOfMonsters; ++x)
    {
        srand(time(NULL));
        int monsterDirection = rand() % 4+1;
        if (monsterDirection == 1)
        {
            ++monsters[x].monsterRow;
        }
        else if (monsterDirection == 2)
        {
            --monsters[x].monsterRow;
        }
        else if (monsterDirection ==3)
        {
            --monsters[x].monsterColumn;
        }
        else
        {
            ++monsters[x].monsterColumn;
        }
    }
    return 0;
}
Again you must give the locations before calling the output.

1
2
3
4
5
6
7
    outputBoard(monsters); //should be after the next part
    srand(time(NULL));
    for (int x = 0; x<numOfMonsters; ++x)
    {
        monsters[x].monsterRow = rand() % 8;
        monsters[x].monsterColumn = rand() % 21;
    }


Also I see you use srand multiple times. You only need to call that once to seed the original pseudo-random sequence.
I see, thanks for the help!
Topic archived. No new replies allowed.