#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
usingnamespace 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.
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...:)