-
Notifications
You must be signed in to change notification settings - Fork 0
/
board.cpp
213 lines (177 loc) · 4.27 KB
/
board.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
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
#include "board.h"
#include <iostream>
#include <filesystem>
#include <fstream>
#include <unistd.h>
#include <termios.h>
#include <ncurses.h>
#define LEFT 'a'
#define RIGHT 'd'
#define UP 'w'
#define DOWN 's'
#define PLAYER 'P'
#define AIR ' '
#define WALL '&'
#define DIRT ':'
#define GEM '*'
#define BOULDER 'O'
#define EXIT 'E'
// X
// Y0123
// 1
// 2
// 3
void Board::readBoard(string filename){
ifstream file;
try{
file.open(filename);
}catch(std::invalid_argument){
printw("File name incorrect.");
}
string tmp;
getline(file, tmp); //skip top line
for(int i = 0; i < hz; i++){
getline(file, tmp);
for(int j = 0; j < vt; j++){
brd[i][j] = tmp[j];
if(brd[i][j] == PLAYER){
px = i; py = j;
}
if(brd[i][j] == GEM){
gemCount++;
}
}
}
}
void Board::printBoard(){
move(0,0);
for(int i = 0; i < hz; i++){
for(int j = 0; j < vt; j++){
printw("%c", brd[i][j]);
}
printw("\n");
}
printw("X: %d Y: %d Gems left: %d\n", px, py, (gemCount - score));
refresh();
}
char Board::validMove(int x, int y, char direction){
if (brd[x][y] == WALL) return 0;
if (brd[x][y] == GEM){
score++;
}
if (direction == UP || direction == DOWN){
if (brd[x][y] == BOULDER) return 0;
}
if (direction == LEFT && brd[x][y] == BOULDER){
if (brd[x][y-1] == AIR){
brd[x][y-1] = BOULDER;
}else{
return 0;
}
}
if (direction == RIGHT && brd[x][y] == BOULDER){
if (brd[x][y+1] == AIR){
brd[x][y+1] = BOULDER;
}else{
return 0;
}
}
if (brd[x][y] == EXIT && score != gemCount) return 0;
else if(brd[x][y] == EXIT && score == gemCount) complete = 1;
return 1;
}
void Board::checkAround(){
bool moved = false;
for (int x = hz-1; x >= 0; x--){
for(int y = 0; y <= vt-1; y++){
if(gravityLogic(x, y)) moved = 1;
}
}
}
bool Board::gravityLogic(int x, int y){
if ((brd[x][y] != BOULDER) && (brd[x][y] != GEM)) return 0;
if(brd[x+1][y] == AIR){
brd[x+1][y] = brd[x][y];
brd[x][y] = AIR;
x++;
if(brd[x+1][y] == PLAYER) alive = 0;
return 1;
}
if((brd[x+1][y] == BOULDER || brd[x+1][y] == GEM) && brd[x][y-1] == AIR && brd[x+1][y-1] == AIR){
brd[x+1][y-1] = brd[x][y];
brd[x][y] = AIR;
x++; y--;
if(brd[x+1][y] == PLAYER) alive = 0;
return 1;
}
if((brd[x+1][y] == BOULDER || brd[x+1][y] == GEM) && brd[x][y+1] == AIR && brd[x+1][y+1] == AIR){
brd[x+1][y+1] = brd[x][y];
brd[x][y] = AIR;
x++; y++;
if(brd[x+1][y] == PLAYER) alive = 0;
return 1;
}
return 0;
}
void Board::explode(){
for(int x = px-1; x <= px+1; x++){
for(int y = py-1; y <= py+1; y++){
if(brd[x][y] != WALL) brd[x][y] = AIR;
}
}
}
int Board::playerMove(){
int npx, npy;
char input = getch();
if (!alive && !complete){
explode();
complete = 1;
}
if (!alive || complete) return input;
switch(input){
case LEFT:
npx = px;
npy = py - 1;
break;
case RIGHT:
npx = px;
npy = py + 1;
break;
case UP:
npx = px - 1;
npy = py;
break;
case DOWN:
npx = px + 1;
npy = py;
break;
default:
return input;
}
if (!validMove(npx, npy, input)) return input;// input has to be wasd
brd[npx][npy] = PLAYER;
brd[px][py] = AIR;
px = npx; py = npy;
return input;
}
Board::Board(string filename){
//get board length
string tmp;
ifstream file(filename);
getline(file, tmp, ' ');
vt = stoi(tmp);
getline(file, tmp);
hz = stoi(tmp);
file.close();
brd = (char**)malloc(sizeof(char**) * vt);
for(int i = 0; i < hz; i++){
brd[i] = (char*)malloc(sizeof(char*) * hz);
}
gemCount = 0;
if (filename[0] != '\0'){
readBoard(filename);
}
score = 0;
alive = 1;
complete = 0;
}