-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjuego1D.cpp
83 lines (70 loc) · 1.53 KB
/
juego1D.cpp
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
/*
* Basic 1D game thingy
*
* @author J. Alvarez
*/
#include <iostream>
#define LEFT 'A'
#define RIGHT 'D'
#define DIE 'X'
#define N 10
#define CELL '_'
#define ICON 'I'
/*
* Obtains user input and performs movement with it
* @param map The current map
* @param pos The pointer to the current character position
* @param action The action character entered by user
*/
void inputAction(const char map[], int * pos, char action, bool * gameOver);
/*
* Prints the current status of the specified map
* @param map The map
* @param pos Current character position
*/
void printMap(const char map[], int pos);
/*
* Main program
*/
int main() {
int charPos = 0;
char action = '0';
bool gameOver = false;
char map[N];
for(int i=0; i<N; i++)
map[i] = CELL;
printMap(map, charPos);
while(!gameOver) {
std::cout << "Enter an action:\nLeft (A) - Right (D) - Die (X)" << std::endl;
std::cin >> action;
inputAction(map, & charPos, action, & gameOver);
}
return 0;
}
void inputAction(const char map[], int * pos, char action, bool * gameOver) {
switch(std::toupper(action)) {
case LEFT:
if(* pos > 0)
* pos = * pos - 1;
break;
case RIGHT:
if(* pos < N-1)
* pos = * pos + 1;
break;
case DIE:
* gameOver = true;
break;
default:
std::cout << "INVALID ACTION!" << std::endl;
break;
}
printMap(map, * pos);
}
void printMap(const char map[], int pos) {
for(int i=0; i<N; i++)
if(i!=pos)
std::cout << map[i];
else
std::cout << ICON;
std::cout << " -- Now at: " << pos << std::endl;
}