Code: Select all
#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 //I don't really no why I used #define as I don't what the difference is...
#define WIDTH 12 //It works though :)
#define TRAPS 4 //How many traps? The more traps the longer it takes to load
#define ENEMIES 3 //How many enemies? The more traps 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;
if (posy == tosy && posx == tosx)
{
cout << "You win!\n";
quit = true;
}
break;
}
case 4:
if (posx == 0)
break;
else
{
GameBoard[posy][posx] = board;
posx -= 1;
GameBoard[posy][posx] = player;
if (posy == tosy && posx == tosx)
{
cout << "You win!\n";
quit = true;
}
break;
}
case 6:
if (posx == (WIDTH-1))
break;
else
{
GameBoard[posy][posx] = board;
posx += 1;
GameBoard[posy][posx] = player;
if (posy == tosy && posx == tosx)
{
cout << "You win!\n";
quit = true;
}
break;
}
case 8:
if (posy == 0)
break;
else
{
GameBoard[posy][posx] = board;
posy -= 1;
GameBoard[posy][posx] = player;
if (posy == tosy && posx == tosx)
{
cout << "You win!\n";
quit = true;
}
break;
}
}
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);
//WORK IN PROGRESS
}

Oh, and some comments are simply notes for me so I don't forget. Also, if you don't have windows just delete the windows.h include and the Sleep function and it should work pretty much the same.
Thanks
EDIT:
Oh, and my algorithm to prevent traps and enemies from going on top of each other is not working what so ever when I don't see a problem with it although am sure there is. Thanks again
