-
Notifications
You must be signed in to change notification settings - Fork 2
/
juego2D.cpp
179 lines (154 loc) · 4.04 KB
/
juego2D.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
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
/*
* Basic CLI 2D game thingy. A character moving around a 2D map, according to
* user input. Saves state in a file.
*
* For Platzi's C++ course challenge.
*
* @author J. Alvarez
*/
#include <iostream>
#include <fstream>
#define LEFT 'A'
#define RIGHT 'D'
#define UP 'W'
#define DOWN 'S'
#define DIE 'X'
#define N 8
#define M 10
#define CELL '_'
#define ICON 'I'
#define SAVE_FILE "mapState.dat"
/*
* Obtains user input and performs movement with it
* @param map The current map
* @param pos The pointer to the current horizontal character position
* @param pos The pointer to the current vertical character position
* @param action The action character entered by user
*/
void inputAction(const char map[][M], int * posI, int * posJ, 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[][M], int posI, int posJ);
/*
* Updates the character's position according to the save file's map
* @param posI Pointer to vertical position to be updated
* @param posJ Pointer to horizontal position to be updated
*/
void setPositionFromFile(int * posI, int * posJ);
/*
* Saves the finished game's map status on the defined SAVE_FILE file, with the
* character's last position marked as a *
* @param map The map of the game
* @param posI The last horizontal position of the character
* @param posJ The last vertical position of the character
*/
void saveGame(char map[][M], int posI, int posJ);
/*
* Main program
*/
int main() {
int charPosI = 0, charPosJ = 0;
char action = '0';
bool gameOver = false;
char map[N][M];
setPositionFromFile(& charPosI, & charPosJ);
for(int i = 0; i < N; i++)
for(int j = 0; j < M; j++)
map[i][j] = CELL;
printMap(map, charPosI, charPosJ);
while(!gameOver) {
std::cout << "Enter a key for the desired action:\n Up (W) - Down (S) -"
<< "Left (A) - Right (D) - Die (X)" << std::endl;
std::cin >> action;
inputAction(map, & charPosI, & charPosJ, action, & gameOver);
}
saveGame(map, charPosI, charPosJ);
std::cout << "-- Game Over! --" << std::endl;
return 0;
}
void inputAction(const char map[][M], int * posI, int * posJ, char action,
bool * gameOver) {
switch(std::toupper(action)) {
case LEFT:
if(* posJ > 0)
* posJ = * posJ - 1;
break;
case RIGHT:
if(* posJ < M-1)
* posJ = * posJ + 1;
break;
case UP:
if(* posI > 0)
* posI = * posI - 1;
break;
case DOWN:
if(* posI < N-1)
* posI = * posI + 1;
break;
case DIE:
* gameOver = true;
break;
default:
std::cout << "INVALID ACTION!" << std::endl;
break;
}
printMap(map, * posI, * posJ);
}
void printMap(const char map[][M], int posI, int posJ) {
for(int i = 0; i < N; i++) {
for(int j = 0; j < M; j++)
if(i == posI && j == posJ)
std::cout << ICON;
else
std::cout << map[i][j];
std::cout << std::endl;
}
std::cout << " -- Now at: " << posI << ", " << posJ << std::endl;
}
void setPositionFromFile(int * posI, int * posJ) {
std::ifstream inputFile(SAVE_FILE);
if(inputFile.is_open()) {
bool found = false;
int currentLine = 0;
std::string readBuffer;
while(getline(inputFile, readBuffer) && !found) {
for(int k = 0; k < readBuffer.length(); k++) {
if(readBuffer[k] == '*') {
* posI = currentLine;
* posJ = k;
found = true;
}
}
currentLine++;
}
if(!found) {
std::cout << "Could not find saved position in file. File is not"
<< " valid. Using origin position" << std::endl;
}
}
else {
std::cout << "Could not open save file " << SAVE_FILE << ", using "
<< "origin position" << std::endl;
}
inputFile.close();
}
void saveGame(char map[][M], int posI, int posJ) {
std::ofstream saveFile(SAVE_FILE);
if(saveFile.is_open()) {
for(int i = 0; i < N; i++) {
for(int j = 0; j < M; j++)
if (i == posI && j == posJ)
saveFile << '*';
else
saveFile << map[i][j];
saveFile << std::endl;
}
}
else
std::cout << "Couldn't open " << SAVE_FILE << "OS problem" << std::endl;
saveFile.close();
}