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
|
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <string>
using namespace std;
const int ROWS = 10, COLS = 10;
void showMap(char [][COLS], int);
void setTraps(char [][COLS], int, int, int);
int move(char [][COLS], int, char);
int main(){
char map[ROWS][COLS];
int row, col, gameStatus;//gameStatus=(Won, hit trap, nothing happened)
char playerMove;
//Initializing all spots to '.' blank spaces
for (row = 0; row < ROWS; row ++){
for (col = 0; col < COLS; col ++){
map[row][col] = '.';
}
}
//Setting player start position
map[row = 0][col = 0] = 'P';
//Setting finish position
map [row = 9][col = 9] = 'F';
//Setting the traps
setTraps(map, ROWS, row, col);
//Displaying the map
showMap(map, ROWS);
do {
cin >> playerMove;
while (playerMove != 'a' && playerMove != 'A' && playerMove != 's' && playerMove != 'S' && playerMove != 'd' && playerMove != 'D' && playerMove != 'w' && playerMove != 'W'){
system("CLS");
cout << "Sorry that was not a choice. Please try again.\n";
showMap(map, ROWS);
cin >> playerMove;
}
gameStatus = move(map, ROWS, playerMove);
system("CLS");
//Displaying the map
showMap(map, ROWS);
}while(gameStatus == 1);
if (gameStatus == 2)
cout << "You hit a trap!\nGame over!\n";
else if (gameStatus == 3)
cout << "You Won!\n";
system ("pause");
return 0;
}
int move(char map[][COLS], int rows, char playerMove){
static int r = 0, c = 0;
//MOVE UP
if (playerMove == 'w' || playerMove == 'W'){
map[r][c] = '.';
r--;
if (map[r][c] == '.'){
map[r][c] = 'P';
return 1;
}
else if (map[r][c] == 'T')
return 2;
else if (map[r][c] == 'F')
return 3;
else {
map[++r][c] = 'P';
system("CLS");
showMap(map, ROWS);
cout << "There's a wall there!\n";
cin >> playerMove;
while(playerMove != 'a' && playerMove != 'A' && playerMove != 's' && playerMove != 'S' && playerMove != 'd' && playerMove != 'D' && playerMove != 'w' && playerMove != 'W'){
system("CLS");
cout << "Sorry that was not a choice. Please try again.\n";
showMap(map, ROWS);
cin >> playerMove;
}
move(map, ROWS, playerMove);
}
}
//MOVE Left
else if (playerMove == 'a' || playerMove == 'A'){
map[r][c] = '.';
c--;
if (map[r][c] == '.'){
map[r][c] = 'P';
return 1;
}
else if (map[r][c] == 'T')
return 2;
else if (map[r][c] == 'F')
return 3;
else {
map[r][++c] = 'P';
system("CLS");
showMap(map, ROWS);
cout << "There's a wall there!\n";
cin >> playerMove;
while(playerMove != 'a' && playerMove != 'A' && playerMove != 's' && playerMove != 'S' && playerMove != 'd' && playerMove != 'D' && playerMove != 'w' && playerMove != 'W')
{
system("CLS");
cout << "Sorry that was not a choice. Please try again.\n";
showMap(map, ROWS);
cin >> playerMove;
}
move(map, ROWS, playerMove);
}
}
}
|