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
|
// Keiboard keis \ |
#include <iostream>
#include <vector>
#include <cstdlib>
#include <time.h>
#include "conio.h" // getche() get char input
using namespace::std;
/*
NOTE: This will only compile on LINUX due to the following line 'cout << "\033[2J\033[1;1H"'
*/
int main()
{
char board[7][19] = {{'-','-','-','-','e','o','-','o','-','-','-','o','-','o','-','e','-','-','e'}, // our board
{'-','-','e','-','-','-','-','-','-','-','-','-','-','-','-','-','o','-','-'},
{'e','-','-','-','-','e','o','e','o','e','-','-','-','-','o','X','-','-','o'},
{'-','o','-','-','-','-','-','-','-','-','-','-','-','o','-','e','-','o','-'},
{'o','-','-','o','-','-','e','o','-','-','-','-','-','-','-','o','-','e','-'},
{'-','-','-','-','-','o','-','-','o','-','-','e','-','e','-','-','o','-','e'},
{'-','P','-','e','o','-','o','-','-','o','-','o','-','-','-','o','-','-','-'}};
cout << "\033[2J\033[1;1H" << "Get P to X using WASD. \n\n" << endl; // clear screen
for (int i = 0; i != 7; ++i) { // print board
for (int j = 0; j != 19; ++j)
cout << board[i][j];
cout << endl;
}
char move = 'q'; // getting next move from user
int y = 6, x = 1; // where player spawns
while (move = getche()) { // get next move
board[y][x] = '-'; // reset previous position
cout << "\033[2J\033[1;1H" << "Get P to X using WASD. \n\n" << endl; // clear screen TERMINAL ONLY
switch (move) { // move our player position
case 'w':
case 'W':
if (y > 0)
y--;
break;
case 's':
case 'S':
if (y < 6)
y++;
break;
case 'a':
case 'A':
if (x > 0)
x--;
break;
case 'd':
case 'D':
if (x < 18)
x++;
break;
default:
cout << "Invalid input, try again." << endl;
break;
}
if (board[y][x] == 'o') { // if player fell into a hole
cout << "Game over, you fell into a whole." << endl;
return 0;
} else if (board[y][x] == 'e') {
} else if (board[y][x] == 'X') { // if player found the treasure
cout << "You found the treasure. HURRAY!!!" << endl;
return 0;
}
board[y][x] = 'P'; // move player
// move traps
srand (time(NULL));
unsigned nextMove = rand() % 4 + 1;
cout << nextMove << endl;
for (int y = 0; y != 7; ++y) {
for (int x = 0; x != 19; ++x){
if (board[y][x] == 'e') {
if (nextMove == 1 && y > 0) {
if (board[y-1][x] == '-') {
board[y][x] = '-';
board[y-1][x] = 'e';
} else if (board[y-1][x] == 'P') {
cout << "Game over, an enemy has killed you!" << endl;
}
} else if (nextMove == 2 && y < 6) {
if (board[y+1][x] == '-') {
board[y][x] = '-';
board[y+1][x] = 'e';
} else if (board[y+1][x] == 'P') {
cout << "Game over, an enemy has killed you!" << endl;
}
} else if (nextMove == 3 && x > 0) {
if (board[y][x-1] == '-') {
board[y][x] = '-';
board[y][x-1] = 'e';
} else if (board[y][x-1] == 'P') {
cout << "Game over, an enemy has killed you!" << endl;
}
} else if (nextMove == 4 && x < 18) {
if (board[y][x+1] == '-') {
board[y][x] = '-';
board[y][x+1] = 'e';
} else if (board[y][x+1] == 'P') {
cout << "Game over, an enemy has killed you!" << endl;
}
}
}
}
}
for (int i = 0; i != 7; ++i) { // print out our board
for (int j = 0; j != 19; ++j)
cout << board[i][j];
cout << endl;
}
}
return 0;
}
|