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
|
#include<iostream>
#include<cstdlib>
#include<string>
using namespace std;
int main()
{
char play;
char move;
cout << "play?" << endl;
cin >> play;
bool validinput = true;
bool gameon = false;
char grid[10][10] = {
{ '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', },
{ '*', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '*', },
{ '*', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '*', },
{ '*', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '*', },
{ '*', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '*', },
{ '*', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '*', },
{ '*', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '*', },
{ '*', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '*', },
{ '*', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '*', },
{ '*', '*', '*', '*', '*', '*', '*', '*', '*', '*' }
};
char player = 'P', trap = 'X', goal = 'G';
int x = 1, y = 1;
grid[x][y] = player;
grid[5][4] = trap;
grid[8][8] = goal;
// Above initializes variables
do
{
if (play == 'y')
{
for (int row = 0; row < 10; row++)
{
for (int column = 0; column < 10; column++)
{
cout << grid[row][column];
}
cout << endl;
}
cout << "move" << endl;
cin >> move;
move = tolower(move);
if (move == 'r') // Right
{
grid[x][y] = ' ';// Remove player from grid location
y++; // Changes locations
if(grid[x][y] == 'X') // Check if trap in new location
{
cout << "You died in the trap" << endl; // If yes, player dies
// and game ends
gameon = true;
}
if (grid[x][y] == 'G') // Check if location is exit
{
cout << "You found the exit!!" << endl; // If yes, player wins
// and game ends
gameon = true;
}
grid[x][y] = player; // If not, move player to new location
}
if (move == 'd') // Down
{
grid[x][y] = ' ';
x++;
if (grid[x][y] == 'X')
{
cout << "You died in the trap" << endl;
gameon = true;
}
if (grid[x][y] == 'G')
{
cout << "You found the exit!!" << endl;
gameon = true;
}
grid[x][y] = player;
}
if (move == 'l') // Left
{
grid[x][y] = ' ';
y--;
if (grid[x][y] == 'X')
{
cout << "You died in the trap" << endl;
gameon = true;
}
if (grid[x][y] == 'G')
{
cout << "You found the exit!!" << endl;
gameon = true;
}
grid[x][y] = player;
}
if (move == 'u') // Up
{
grid[x][y] = ' ';
x--;
if (grid[x][y] == 'X')
{
cout << "You died in the trap" << endl;
gameon = true;
}
if (grid[x][y] == 'G')
{
cout << "You found the exit!!" << endl;
gameon = true;
}
grid[x][y] = player;
}
}
} while (!gameon);
system("pause");
return 0;
}
|