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
|
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
const int MaxRow = 8, MaxCol = 10;
const char Space = '.', Trap = '#', Play = '@', Cash = '$';
const char Up = 'U', Down = 'D', Left = 'L', Right = 'R';
const int NumCash = 5, NumTraps = 10;
enum State { Playing, Win, Lose };
struct Player {
int row, col;
int cash;
Player() : row{0}, col{0}, cash{0} {}
};
bool repeat();
void displayInstructions();
void initDungeon(char dungeon[][MaxCol], const Player& player);
void displayDungeon(const char dungeon[][MaxCol]);
void setItems(char dungeon[][MaxCol], char sym, int num);
char getMove();
State updateDungeon(char dungeon[][MaxCol], Player& player, char move);
int main()
{
srand(time(nullptr));
do
{
displayInstructions();
char dungeon[MaxRow][MaxCol];
Player player;
initDungeon(dungeon, player);
State state = Playing;
while (state == Playing)
{
displayDungeon(dungeon);
char move = getMove();
state = updateDungeon(dungeon, player, move);
}
displayDungeon(dungeon);
if (state == Win)
cout << "You win.\n";
else
cout << "You lose.\n";
} while (repeat());
}
void displayInstructions()
{
cout << "\t----- WELCOME TO THE DUNGEON -----\n";
cout << "\tTo choose your move please enter:\n";
cout << "\tTO MOVE DOWN -------------------- " << Down << '\n';
cout << "\tTO MOVE UP ---------------------- " << Up << '\n';
cout << "\tTO MOVE LEFT -------------------- " << Left << '\n';
cout << "\tTO MOVE RIGHT ------------------- " << Right << '\n';
cout << "\t--Lowercase or uppercase accepted--\n";
cout << "\tThe object is to reach treasures: " << Cash << '\n';
cout << "\tAnd to avoid the traps: " << Trap << "\n\n\n";
}
void initDungeon(char dungeon[][MaxCol], const Player& player)
{
for (int row = 0; row < MaxRow; row++)
for (int col = 0; col < MaxCol; col++)
dungeon[row][col] = Space;
setItems(dungeon, Trap, NumTraps);
setItems(dungeon, Cash, NumCash);
dungeon[player.row][player.col] = Play;
}
void displayDungeon(const char dungeon[][MaxCol])
{
cout << '\n';
for (int row = 0; row < MaxRow; ++row)
{
cout << '\t';
for (int col = 0; col < MaxCol; ++col)
cout << dungeon[row][col] << ' ';
cout << '\n';
}
}
void setItems(char dungeon[][MaxCol], char sym, int num)
{
for (int i = 0; i < num; )
{
int row = rand() % MaxRow, col = rand() % MaxCol;
if (dungeon[row][col] == Space)
{
dungeon[row][col] = sym;
++i;
}
}
}
char getMove()
{
char move;
while (true)
{
cout << "\t(U)p, (D)own, (L)eft, (R)ight: ";
cin >> move;
move = toupper(move);
if (move == Up || move == Down || move == Left || move == Right)
break;
cout << "\tTry again.";
}
return move;
}
State updateDungeon(char dungeon[][MaxCol], Player& player, char move)
{
int drow = 0, dcol = 0;
if (move == Up && player.row != 0) --drow;
else if (move == Down && player.row != MaxRow - 1) ++drow;
else if (move == Left && player.col != 0) --dcol;
else if (move == Right && player.col != MaxCol - 1) ++dcol;
State state = Playing;
if (drow != 0 || dcol != 0)
{
dungeon[player.row][player.col] = Space;
player.row += drow;
player.col += dcol;
if (dungeon[player.row][player.col] == Cash)
{
if (++player.cash == NumCash)
state = Win;
}
else if (dungeon[player.row][player.col] == Trap)
state = Lose;
dungeon[player.row][player.col] = Play;
}
return state;
}
bool repeat()
{
cout << "Checking for play again, return false for debug\n";
return false;
}
|