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
|
#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
#define FLOOR 0
#define HERO 1
#define MONSTER 2
#define GOLD 3
#define BOARD_H 12
#define BOARD_W 10
int getkey(void);
void print_board(int board[BOARD_H][BOARD_W],
int man[2], int gold[2],
int mon_one[2], int mon_two[2]);
char floor(int f);
void clear_screen(void);
char move_dir(int d);
void player_move(char input, int man[2], int play);
void monster_move(int move, int monster[2]);
int victory(int man[2], int gold[2], int mon_one[2],
int mon_two[2], int play);
int main() {
srand(time(0));
int x1 = rand() % 9, x2 = rand() % 9,
x3 = rand() % 9, x4 = rand() % 9;
int y1 = rand() % 3, y2 = rand() % 3,
y3 = rand() % 3, y4 = rand() % 3;
int mon_one_move, mon_two_move;
char key;
int play = 1;
int man[2] = {y1, x1}, gold[2] = {9+y2, x2};
int mon_one[2] = {3+y3, x3}, mon_two[2] = {6+y4, x4};
int board [BOARD_H][BOARD_W];
for (int r = 0; r < BOARD_H; r++) {
for (int c = 0; c < BOARD_W; c++) {
board[r][c] = 0;
}
}
do {
clear_screen();
print_board(board, man, gold, mon_one, mon_two);
key = getkey();
player_move(key, man, play);
if ((man[0] == gold[0]) && (man[1] == gold[1])) {
play = 2;
}
if (((man[0] == mon_one[0]) && (man[1] == mon_one[1])) ||
((man[0] == mon_two[0]) && (man[1] == mon_two[1]))) {
play = 0;
}
if (key == 'q')
play = 3;
monster_move(rand() % 5, mon_one);
monster_move(rand() % 5, mon_two);
clear_screen();
} while (play == 1);
switch (play) {
case 0:
cout << "You got eaten by a monster!" << endl;
break;
case 2:
cout << "Yay you got the gold!" << endl;
break;
case 3:
cout << "Thanks for playing, come again" << endl;
break;
}
cin.get();
return 0;
}
void print_board(int board[BOARD_H][BOARD_W],
int man[2], int gold[2],
int mon_one[2], int mon_two[2]) {
for (int r = 0; r < BOARD_H; r++) {
for (int c = 0; c < BOARD_W; c++) {
if ((r == man[0]) && (c == man[1])) {
cout << floor(HERO);
} else if ((r == gold[0]) && (c == gold[1])) {
cout << floor(GOLD);
} else if ((r == mon_one[0]) && (c == mon_one[1])) {
cout << floor(MONSTER);
} else if ((r == mon_two[0]) && (c == mon_two[1])) {
cout << floor(MONSTER);
} else
cout << floor(board[r][c]);
}
cout << endl;
}
}
char floor(int f) {
if (f == 0)
return '.';
if (f == 1)
return 'H';
if (f == 2)
return 'M';
if (f == 3)
return 'G';
}
void clear_screen(void) {
for (int i = 0; i < 25; i++) {
cout << "\n";
}
}
int getkey ( void ) {
int ch;
struct termios oldt, newt;
tcgetattr ( STDIN_FILENO, &oldt );
newt = oldt;
newt.c_lflag &= ~( ICANON | ECHO );
tcsetattr ( STDIN_FILENO, TCSANOW, &newt );
ch = getchar();
tcsetattr ( STDIN_FILENO, TCSANOW, &oldt );
return ch;
}
char move_dir(int d) {
switch (d) {
case 0:
return 'u';
case 1:
return 'l';
case 2:
return 'd';
case 3:
return 'r';
case 4:
return 'z';
}
}
void player_move(char input, int man[2], int play) {
if ((input == 'w') && (man[0] > 0))
man[0]--;
if ((input == 'a') && (man[1] > 0))
man[1]--;
if ((input == 's') && (man[0] < BOARD_H - 1))
man[0]++;
if ((input == 'd') && (man[1] < BOARD_W - 1))
man[1]++;
}
void monster_move(int move, int monster[2]) {
switch (move) {
case 0:
if (monster[0] > 0)
monster[0]--;
break;
case 1:
if (monster[1] > 0)
monster[1]--;
break;
case 2:
if (monster[0] < BOARD_H - 1)
monster[0]++;
break;
case 3:
if (monster[1] < BOARD_W - 1)
monster[1]++;
break;
case 4:
break;
}
}
int victory(int man[2], int gold[2], int mon_one[2],
int mon_two[2], int play) {
if ((man[0] == gold[0]) && (man[1] == gold[1])) {
cout << "Yay you got the gold!" << endl;
play = 2;
}
if (((man[0] == mon_one[0]) && (man[1] == mon_one[1])) ||
((man[0] == mon_two[0]) && (man[1] == mon_two[1]))) {
cout << "You got eaten by a monster!" << endl;
play = 0;
}
}
| |