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 210 211 212 213 214 215 216 217 218 219 220 221 222 223
|
//WIP: Dungeon Crawler Game
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <vector>
#define HEIGHT 10
#define WIDTH 10
using namespace std;
class MapObject
{
public:
int x, y;
char icon;
MapObject(int type);
void MoveRandom();
bool MovePlayer(int PMove);
};
void NewMap();
void DisplayMap();
bool IsGameOver();
bool IsValid(int x, int y);
int IsOccupied(int x, int y);
int Map[HEIGHT][WIDTH];
int main()
{
int PMove;
char PlayAgain;
do
{
//Set up for a new game
srand(time(NULL));
NewMap();
//Create the player and other 'sprites'
vector<MapObject> MySprites;
MySprites.push_back(MapObject(80)); //80 is the ascii code for 'P'
MySprites.push_back(MapObject(88)); //88 is the ascii code for 'X'
for (int i = rand() % 3 + 2; i > 0; i--)
{
MySprites.push_back(MapObject(84)); //84 is the ascii code for 'T'
}
for (int i = rand() % 3 + 2; i > 0; i--)
{
MySprites.push_back(MapObject(69)); //69 is the ascii code for 'E'
}
vector<MapObject>::size_type VecSize = MySprites.size();
DisplayMap();
//Loop until the game is over
do
{
std::cout << std::string(4, '\n');
//Loop until we get some valid input
do
{
std::cout << std::endl << "Enter a number to move (check your numpad): ";
cin >> PMove;
} while (!MySprites[0].MovePlayer(PMove));
//Move the enemies
for (int i = 1; i < VecSize; i++)
{
MySprites[i].MoveRandom();
}
DisplayMap();
} while (!IsGameOver());
cout << "Play again? (Y/N) ";
cin >> PlayAgain;
} while (PlayAgain == 'Y' || PlayAgain == 'y');
return 0;
}
//Constructor for map object class
MapObject::MapObject(int type)
{
icon = type;
do
{
x = rand() % WIDTH;
y = rand() % HEIGHT;
} while (IsOccupied(x, y));
Map[y][x] += type - 42;
}
//Map object function to move to a random adjacent spot
void MapObject::MoveRandom()
{
//return if not enemy object
if (icon != 69)
return;
//count and store available spots
int GoodSpots = 0, PosX[8], PosY[8];
for (int i = y - 1; i < y + 2; i++)
for (int j = x - 1; j < x + 2; j++)
if (IsValid(j, i))
if (IsOccupied(j, i) < 2)
{
PosX[GoodSpots] = j;
PosY[GoodSpots] = i;
GoodSpots++;
}
//pick one of the available spots
//and 'move' to it
int MyPos = rand() % GoodSpots;
Map[PosY[MyPos]][PosX[MyPos]] += 69 - 42;
Map[y][x] -= 69 - 42;
x = PosX[MyPos];
y = PosY[MyPos];
}
//Function that handles the player input
//Only returns true if the player can move to the indicated spot
bool MapObject::MovePlayer(int PMove)
{
int a, b;
//return if not player...
if (icon != 80)
return false;
//check for valid input
if (PMove < 1 || PMove > 9 || PMove == 5)
return false;
//change the input to coordinates
if (PMove > 6)
b = y - 1;
else if (PMove < 4)
b = y + 1;
if (PMove % 3 == 0)
a = x + 1;
else if (PMove % 3 == 1)
a = x - 1;
if (!IsValid(a, b))
return false;
Map[b][a] += 80 - 42;
Map[y][x] -= 80 - 42;
x = a;
y = b;
return true;
}
//Clears the 'map', resetting it to default
void NewMap()
{
for (int i = 0; i < HEIGHT; i++)
for (int j = 0; j < WIDTH; j++)
Map[i][j] = 42;
}
//Displays the 'map' on the screen
void DisplayMap()
{
char ch;
std::cout << std::string(50, '\n');
for (int i = 0; i < HEIGHT; i++)
{
std::cout << "\t";
for (int j = 0; j < WIDTH; j++)
{
ch = Map[i][j];
std::cout << " " << ch << " ";
}
std::cout << std::endl;
}
}
//Checks if the game is over
//Outputs appropriate messages
bool IsGameOver()
{
for (int i = 0; i < HEIGHT; i++)
for (int j = 0; j < WIDTH; j++)
if (Map[i][j] > 100)
{
if (Map[i][j] == 80 + 88 - 42)
std::cout << "\n\nYou won! ";
else if (Map[i][j] == 80 + 84 - 42)
std::cout << "\n\nWatch out for those traps! ";
else
std::cout << "\n\nYou were eaten! ";
return true;
}
return false;
}
//Check if the coordinates are within the bounds of the map
bool IsValid(int x, int y)
{
if (x >= WIDTH || x < 0 || y >= HEIGHT || y < 0)
return false;
else
return true;
}
//Check if the coordinates are occupied
//returns an int indicating occupied by what
int IsOccupied(int x, int y)
{
switch (Map[y][x])
{
case 80: //'P' for player
return 1;
case 69: //'E' for enemy
return 2;
case 84: //'T' for trap
return 3;
case 88: //'X' for goal
return 4;
default:
return 0;
}
}
|