-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEngine2048.cc
More file actions
111 lines (96 loc) · 2.42 KB
/
Engine2048.cc
File metadata and controls
111 lines (96 loc) · 2.42 KB
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
#include "Engine2048.h"
Engine2048::Engine2048() {
// Make the board
brd = new Board();
// Game state variables
game_state = false;
// Setup the board
// First starting piece
brd->generateEmptySquaresList();
brd->generateNewPiece();
gcycle = game_cycle;
}
Engine2048::~Engine2048() {
delete brd;
}
bool Engine2048::beginningPhase() {
// Generate list of empty squares
brd->generateEmptySquaresList();
// Generate random piece for random empty square
brd->generateNewPiece();
// Check for a gameover
game_state = !(brd->legalMoveState());
return game_state;
}
bool Engine2048::mainPhase(int direction) {
bool temp = false;
bool dir_success = false;
// Try to slide the pieces
switch(direction) {
case UP:
for (int iter=2; iter>=0; iter--)
for (int y=iter; y<3; y++)
for (int x=0; x<4; x++) {
temp = brd->tryDirection(x,y,direction);
if (temp) dir_success = temp;
}
break;
case DOWN:
for (int iter=1; iter<4; iter++)
for (int y=iter; y>0; y--)
for (int x=0; x<4; x++) {
temp = brd->tryDirection(x,y,direction);
if (temp) dir_success = temp;
}
break;
case LEFT:
for (int iter=1; iter<4; iter++)
for (int x=iter; x>0; x--)
for (int y=0; y<4; y++) {
temp = brd->tryDirection(x,y,direction);
if (temp) dir_success = temp;
}
break;
case RIGHT:
for (int iter=2; iter>=0; iter--)
for (int x=iter; x<3; x++)
for (int y=0; y<4; y++) {
temp = brd->tryDirection(x,y,direction);
if (temp) dir_success = temp;
}
break;
}
return dir_success;
}
void Engine2048::endPhase() {
// Reset change states
brd->resetChangeStates();
}
void Engine2048::printBoard() {
std::cout << "------------------------------------\n";
for (int y=3; y>=0; y--) {
std::cout << "| | | | |\n";
std::cout << "|";
for (int x=0; x<4; x++) {
int value = brd->getSquarePieceValue(x,y);
if (value) std::cout << std::setw(6) << value << " ";
else std::cout << std::setw(6) << " ";
if (x<3) std::cout << "|";
else std::cout << "|\n";
}
std::cout << "| | | | |\n";
std::cout << "------------------------------------\n";
}
}
void Engine2048::holdBoardState() {
int count=0;
for (int y=3; y>=0; y--) {
for (int x=0; x<4; x++) {
game_cycle[count]=brd->getSquarePieceValue(x,y);
count++;
}
}
}
int *Engine2048::getHeldBoardState() {
return gcycle;
}