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
|
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
const int EMPTY = 0, WALL = 1, TRAP = 2, GOAL = 3, PLAYER = 4;
const int UP = 8, RIGHT = 6, DOWN = 2, LEFT = 4, QUIT = 5;
const int MAP_SIZE = 10, TRAP_COUNT = 10;
void populateMap(int [MAP_SIZE][MAP_SIZE]);
void printMap(int [MAP_SIZE][MAP_SIZE]);
void printState(int);
void printPrompt();
int getInput();
void movePlayer(int[2], int, int [MAP_SIZE][MAP_SIZE]);
int checkCollision(int[2], int, int [MAP_SIZE][MAP_SIZE]);
int main()
{
srand((unsigned int)time(NULL));
int map[MAP_SIZE][MAP_SIZE];
populateMap(map);
int player[2] = { 0,0 };
int state = 0, nextMove = 0;
bool quit = false;
while (!quit)
{
if (state == QUIT || state == TRAP || state == GOAL)
{
quit = true;
}
std::system("CLS");
printState(state);
printMap(map);
printPrompt();
if (!quit)
{
nextMove = getInput();
state = checkCollision(player, nextMove, map);
if (state != WALL)
{
movePlayer(player, nextMove, map);
}
}
}
}
void populateMap(int map[MAP_SIZE][MAP_SIZE])
{
// Initialize empty map.
for (int y = 0; y < MAP_SIZE; y++)
{
for (int x = 0; x < MAP_SIZE; x++)
{
map[y][x] = EMPTY;
}
}
// Set player and goal in opposing corners.
map[0][0] = PLAYER;
map[MAP_SIZE - 1][MAP_SIZE - 1] = GOAL;
// Distribute traps randomly throughout remaining cells.
for (int traps = 0; traps < TRAP_COUNT; traps++)
{
int x = rand() % 10, y = rand() % 10;
if (map[y][x] == EMPTY)
{
map[y][x] = TRAP;
}
}
}
void printMap(int map[MAP_SIZE][MAP_SIZE])
{
// Print map to screen.
std::cout << "+ - - - - - - - - - - +" << std::endl;
for (int y = 0; y < MAP_SIZE; y++)
{
std::cout << "|";
for (int x = 0; x < MAP_SIZE; x++)
{
switch (map[y][x])
{
case PLAYER: std::cout << " @";
break;
/* case TRAP: std::cout << " *";
break; */
case GOAL: std::cout << " X";
break;
default: std::cout << " .";
}
}
std::cout << " |" << std::endl;
}
std::cout << "+ - - - - - - - - - - +" << std::endl;
}
void printState(int state)
{
switch (state)
{
case EMPTY:
std::cout << "Welcome to tiny dungeon. Try to reach the treasure!" << std::endl;
break;
case WALL:
std::cout << "There's a wall there..." << std::endl;
break;
case TRAP:
std::cout << "You hit a trap! You died..." << std::endl;
break;
case GOAL:
std::cout << "You found the treasure! You win!" << std::endl;
break;
case QUIT:
std::cout << "Thanks for playing. Goodbye..." << std::endl;
break;
}
}
void printPrompt()
{
std::cout << "What is your next move (use numpad)?" << std::endl;
std::cout << "8. North." << std::endl;
std::cout << "2. South." << std::endl;
std::cout << "4. West." << std::endl;
std::cout << "6. East." << std::endl;
std::cout << "5. Quit." << std::endl;
}
void movePlayer(int player[2], int move, int map[MAP_SIZE][MAP_SIZE])
{
int x = 0, y = 1;
map[player[y]][player[x]] = EMPTY;
switch (move)
{
case UP: player[y] -= 1;
break;
case RIGHT: player[x] += 1;
break;
case DOWN: player[y] += 1;
break;
case LEFT: player[x] -= 1;
break;
default:
break;
}
map[player[y]][player[x]] = PLAYER;
}
int getInput()
{
int nextMove = 0;
do
{
std::cin >> nextMove;
} while (nextMove < 2 || nextMove > 8);
return nextMove;
}
int checkCollision(int player[2], int move, int map[MAP_SIZE][MAP_SIZE])
{
int x = 0, y = 1;
int hit = EMPTY;
switch (move)
{
case UP:
if (player[y] - 1 < 0)
{
hit = WALL;
}
else if (map[player[y] - 1][player[x]] != EMPTY)
{
hit = map[y - 1][x];
}
break;
case DOWN:
if (player[y] + 1 > MAP_SIZE - 1)
{
hit = WALL;
}
else if (map[player[y] + 1][player[x]] != EMPTY)
{
hit = map[player[y] + 1][player[x]];
}
break;
case LEFT:
if (player[x] - 1 < 0)
{
hit = WALL;
}
else if (map[player[y]][player[x] - 1] != EMPTY)
{
hit = map[player[y]][player[x] - 1];
}
break;
case RIGHT:
if (player[x] + 1 > MAP_SIZE - 1)
{
hit = WALL;
}
else if (map[player[y]][player[x] + 1] != EMPTY)
{
hit = map[player[y]][player[x] + 1];
}
break;
case QUIT:
hit = QUIT;
}
return hit;
}
|