Loop Causes Crash

I have this code (for the Dungeon Crawler exercise):
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include <iostream>
#include <time.h>   //Both of these are needed for Code::Blocks to use rand
#include <stdlib.h> //Both of these are needed for Code::Blocks to use rand
#include <windows.h>

//ADD TWO PLAYER WHERE THEY HAVE TO TRAP EACH OTHER - ONE CREATES TRAPS, OTHER ATTEMPTS TO ATTACK THE OTHER PLAYER
//ADD TRAPS AS AN ARRAY (MULTI-DIMENSIONAL) WITH RANDOM GRID LOCATOR - EASIER TO CREATE MULTIPLE TRAPS ESPECIALLY FOR TWO PLAYER

#define HEIGHT 8 
#define WIDTH 12 
#define TRAPS 4 //How many traps? The more traps the longer it takes to load
#define ENEMIES 3 //How many enemies? The more enemies the longer it takes to load

char GameBoard[HEIGHT][WIDTH]; //GameBoard

int i, j; //Global Counters

int action, enemyMove;
bool quit = false;
int posy = 0, posx = 0; //Player pOSition - X-grid/Y-grid
int tosy = (HEIGHT-1), tosx = (WIDTH-1); //Treasure pOSition
int TrapLocations[TRAPS][2]; //Trap location array
int EnemyLocations[ENEMIES][2]; //Enemy location array

char player = 'G'; //The Player Character
char board = '.'; //Blank Board
char trap = 'T'; //Trap symbol
char treasure = 'X'; //Treasure symbol
char enemy = 'E'; //Enemy character

using namespace std;

void BlankBoard(char GameBoard[HEIGHT][WIDTH]); //Used once to create a blank board
void PrintBoard(char GameBoard[HEIGHT][WIDTH]); //Prints out the current board
void ActionLoop(char GameBoard[HEIGHT][WIDTH]); //Handles all events around a user's turn
void EnemyMove(char GameBoard[HEIGHT][WIDTH]);

int main()
{
    cout << "Please wait while the game loads...";

    for (i = 0; i < TRAPS; i++)
    {
        srand( (unsigned)time(0) );
        TrapLocations[i][0] = (rand() % (HEIGHT) + 1);
        TrapLocations[1][i] = (rand() % (WIDTH) + 1);
        Sleep(125); //Because seed is based on time this increases the randomness although decreases portability because
                    //of the need of <windows.h>
    }

    for (i = 0; i < ENEMIES; i++)
    {
        srand( (unsigned)time(0) );
        EnemyLocations[i][0] = (rand() % (HEIGHT) + 1);
        EnemyLocations[1][i] = (rand() % (WIDTH) + 1);
        Sleep(125); //Because seed is based on time this increases the randomness although decreases portability because
                    //of the need of <windows.h>
    }

    cout << string(100, '\n');
    cout << "Welcome to Dungeon Crawl!\n";
    cout << "Use the arrows on the number pad followed by enter to move\nNOTE: Make sure NUM LOCK is on!\n";
    cout << "To begin press ENTER\n> ";
    cin.get();

    BlankBoard(GameBoard);
    GameBoard[posy][posx] = player; //Creates the player on the board
    GameBoard[tosy][tosx] = treasure; //Creates the treasure
    for (i = 0; i < TRAPS; i++) //Creating traps
    {
        while (GameBoard[TrapLocations[i][0]][TrapLocations[1][i]] == trap || GameBoard[TrapLocations[i][0]][TrapLocations[1][i]] == treasure || GameBoard[TrapLocations[i][0]][TrapLocations[1][i]] == enemy || GameBoard[TrapLocations[i][0]][TrapLocations[1][i]] == player)
        {
            TrapLocations[i][0] = (rand() % (HEIGHT) + 1);
            TrapLocations[1][i] = (rand() % (WIDTH) + 1);
        }
        GameBoard[TrapLocations[i][0]][TrapLocations[1][i]] = trap;
    }
    for (i = 0; i < ENEMIES; i++) //Creating enemies
    {
        while (GameBoard[EnemyLocations[i][0]][EnemyLocations[1][i]] == trap || GameBoard[EnemyLocations[i][0]][EnemyLocations[1][i]] == treasure || GameBoard[EnemyLocations[i][0]][EnemyLocations[1][i]] == enemy || GameBoard[EnemyLocations[i][0]][EnemyLocations[1][i]] == player)
        {
            EnemyLocations[i][0] = (rand() % (HEIGHT) + 1);
            EnemyLocations[1][i] = (rand() % (WIDTH) + 1);
        }
        GameBoard[EnemyLocations[i][0]][EnemyLocations[1][i]] = enemy;
    }

    while (quit == false)
    {
        cout << string(100, '\n');
        ActionLoop(GameBoard);
    }

    cout << "\n\nPress ENTER to exit\n";
    cin.get();
    cin.get();
    return 0;
}

void BlankBoard(char GameBoard[HEIGHT][WIDTH])
{
    for (i = 0; i < HEIGHT; i++)
    {
        for (j = 0; j < WIDTH; j++)
        {
            GameBoard[i][j] = board;
        }
    }
}

void PrintBoard(char GameBoard[HEIGHT][WIDTH])
{
    for (i = 0; i < HEIGHT; i++)
    {
        for (j = 0; j < WIDTH; j++)
        {
            cout << GameBoard[i][j];
        }
        cout << endl;
    }
}

void ActionLoop(char GameBoard[HEIGHT][WIDTH])
{
    PrintBoard(GameBoard);
    cout << "\n> ";
    cin >> action;
    while (action != 2 && action != 4 && action != 6 && action != 8)
    {
        cout << "Please only use 2, 4, 6, and 8 on your number pad as arrows.\n";
        cout << "Once you choose a number press ENTER to confirm\n> ";
        cin >> action;
    }

    switch (action)
    {
        case 2:
        if (posy == (HEIGHT-1))
        break;
        else
        {
            GameBoard[posy][posx] = board;
            posy += 1;
            GameBoard[posy][posx] = player;
            break;
        }

        case 4:
        if (posx == 0)
        break;
        else
        {
            GameBoard[posy][posx] = board;
            posx -= 1;
            GameBoard[posy][posx] = player;
            break;
        }

        case 6:
        if (posx == (WIDTH-1))
        break;
        else
        {
            GameBoard[posy][posx] = board;
            posx += 1;
            GameBoard[posy][posx] = player;
            break;
        }

        case 8:
        if (posy == 0)
        break;
        else
        {
            GameBoard[posy][posx] = board;
            posy -= 1;
            GameBoard[posy][posx] = player;
            break;
        }
    }
    if (posy == tosy && posx == tosx)
    {
        cout << "You win!\n";
        quit = true;
    }
    for (i = 0; i < TRAPS; i++)
    {
        if (GameBoard[TrapLocations[i][0]][TrapLocations[1][i]] == player)
        {
            cout << "You lose...\n";
            quit = true;
        }
    }
    for (i = 0; i < ENEMIES; i++)
    {
        if (GameBoard[EnemyLocations[i][0]][EnemyLocations[1][i]] == player)
        {
            cout << "You lose...\n";
            quit = true;
        }
    }
}

void EnemyMove(char GameBoard[HEIGHT][WIDTH])
{
    srand ( (unsigned)time(0) );
    enemyMove = (rand() % 4 + 1);
    //WORK IN PROGRESS
}


and there are several areas which sometimes crash. After the action switch is completed in the ActionLoop function a crash sometimes occurs. I have no idea why this is so if anyone has an idea that would be greatly appreciated. Another problem I have is with the enemy location and trap location algorithms. They seem sound but they often do not do what I intend them to do (which is to make sure there is no overlap). Again, I am unsure of what the problem is so if anyone has a clue about either problem, that would be great. Thanks in advance.

NOTE: There are lots of notes but many of them are really only for me so they can be ignored.
You have incorrect indexing on your arrays.. Replace all indexing [1][i] to [i][1]
I don't know how I made that mistake, thanks. Although it seems to have stopped crashing, it still does not place the correct amount of traps and enemies all the time. I'm going to take a look at the algorithm again but if you or anyone else sees the problem...:)
Topic archived. No new replies allowed.