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 224 225 226 227 228 229 230 231
|
#include <stdlib.h>
#include <iostream>
#include <windows.h>
#include <thread>
#include <mutex>
#include <cctype>
using namespace std;
mutex mu;
char gridWorld[9][9] = { // Hard coded map. gridWorld[y][x]
{ '#', '#', '#', '#', '#', '#', '#', '#', '#' },
{ '#', ' ', '#', ' ', 'D', ' ', 'G', ' ', '#' },
{ '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#' },
{ '#', ' ', '#', 'D', '#', ' ', '#', 'D', '#' },
{ '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#' },
{ '#', '#', ' ', ' ', '#', '#', '#', ' ', '#' },
{ '#', '#', ' ', '#', '#', ' ', ' ', ' ', '#' },
{ '#', 'D', ' ', ' ', ' ', ' ', ' ', ' ', '#' },
{ '#', '#', '#', '#', '#', '#', '#', '#', '#' }
};
bool gameOver = false;
int playerX = 3;
int playerY = 7;
bool playerDead = false;
bool playerWon = false;
bool wall = false;
char* directions = "NESW";
void start()
{
for (int y = 0; y < 9; y++)
{
for (int x = 0; x < 9; x++)
{
if (x == playerX && y == playerY)
{
cout << 'P';
}
else
{
cout << gridWorld[y][x];
}
}
cout << endl;
}
}
void moveTheEnemy()
{
for (int y = 0; y < 9; y++)
{
for (int x = 0; x < 9; x++)
{
for (int i = 1; i < 8; i++)
{
if (x == i && y == 4)
{
cout << 'D';
system("CLS");
}
}
for (int i = 8; i > 9; i--)
{
if (x == i && y == 4)
{
cout << 'D';
system("CLS");
}
}
system("CLS");
}
}
}
void runBothFunctions()
{
thread t1(start);
thread t2(moveTheEnemy);
t2.join();
t1.join();
}
bool canThePlayerMove(char aChar)
{
if (aChar == 'N' && (gridWorld[playerY - 1][playerX] == ' ' | 'G' | 'D'))
return true;
else if (aChar == 'E' && (gridWorld[playerY][playerX + 1] == ' ' | 'G' | 'D'))
return true;
else if (aChar == 'S' && playerY != 7 && (gridWorld[playerY + 1][playerX] == ' ' | 'G' | 'D'))
return true;
else if (aChar == 'W' && (gridWorld[playerY][playerX - 1] == ' ' | 'G' | 'D'))
return true;
else
return false;
}
void moveThePlayer(char aChar)
{
switch (aChar)
{
case 'N':
playerY--;
break;
case 'E':
playerX++;
break;
case 'S':
playerY++;
break;
case 'W':
playerX--;
break;
}
if (gridWorld[playerY][playerX] == 'D')
{
gameOver = true;
playerDead = true;
}
else if (gridWorld[playerY][playerX] == 'G')
{
gameOver = true;
playerWon = true;
}
}
void undoThePlayerMove(char aChar)
{
switch (aChar)
{
case 'N':
playerY++;
break;
case 'E':
playerX--;
break;
case 'S':
playerY--;
break;
case 'W':
playerX++;
break;
}
}
bool game_Over()
{
return gameOver;
}
bool player_Dead()
{
return playerDead;
}
bool player_Won()
{
return playerWon;
}
void there_is_a_wall(char aChar)
{
if (gridWorld[playerY][playerX] == '#')
{
wall = true;
cout << "Oops! That's a wall! Please choose a different path...." << endl;
cout << endl;
undoThePlayerMove(aChar);
}
}
int main()
{
char InputChar = ' ';
cout << "\n\tWelcome to GridWorld: Quantised Excitement. Fate is waiting for You!" << endl;
cout << "\t____________________________________________________________________" << endl;
cout << "\nRules: To move your player use N, S, E or W"
<< "\n\tN - North "
<< "\n\tS - South "
<< "\n\tE - East "
<< "\n\tW - West " << endl;
cout << "\nQ to quit the game." << endl;
cout << "\n" << endl;
do
{
//moveTheEnemy();
runBothFunctions();
if (player_Dead())
{
cout << "\nYou are dead :( !" << endl;
cout << "\n\t-GAME OVER-" << endl;
cout << "Thanks for playing. Maybe next time." << endl;
cout << "\n" << endl;
break;
}
else if (player_Won())
{
cout << "\nYou found the treasure :) !" << endl;
cout << "\n\t-YOU WIN-!" << endl;
cout << "Thanks for playing. There probably won't be a next time." << endl;
cout << "\n" << endl;
break;
}
cout << endl;
cin >> InputChar;
if (islower(InputChar))
{
InputChar = toupper(InputChar);
}
for (int i = 0; i < 4; i++)
{
if (InputChar == directions[i] && canThePlayerMove(directions[i]))
{
moveThePlayer(InputChar);
there_is_a_wall(InputChar);
}
}
//system("CLS");
} while (InputChar != 'Q');
}
|