-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStacker.cpp
163 lines (134 loc) · 4.25 KB
/
Stacker.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
#include "Stacker.h"
int levelSizes[16] = {4,4,4,4,3,3,3,3,2,2,2,2,1,1,1,1};
int levelSpeeds[16] = {1,1,1,2,2,2,2,3,3,3,4,4,4,5,5,5};
void (*displayFunc)(int[][16]); // Stored function for displaying board externally
GameState gameState = Playing; // Current game state
Timer timer; // Timer object for calculating movements
int currState[8][16]= {}; // Current game board
int currY = 0; // Current playing level (0-15)
int currDirection = 1; // Current movement direction (1 = left, -1 = right);
int currX = 0; // The x-position of leftmost pixel of current moving bar
int currWidth = 4; // Width of current moving bar
void nextState() {
// If moving bar at edge of board, change direction
if (currX + currWidth == 8)
currDirection = -1;
else if (currX == 0)
currDirection = 1;
currX += currDirection; // Move bar 1 px in current direction
// Mash currState with the level bar to print state
int newCurrState[8][16] = {};
// Current state
for (int y = 0; y < 16; y++) {
for (int x = 0; x < 8; x++) {
newCurrState[x][y] = currState[x][y];
}
}
// Level bar
for (int x = 0; x < currWidth; x++) {
newCurrState[currX + x][currY] = 1;
}
displayFunc(newCurrState); // Display board to leds
}
void Stacker::keepPlaying() {
// Check for victory
if (currY > 15) {
displayWin();
}
// Check timer to see if next frame yet
if (timer.read_ms() > (250-40*(levelSpeeds[currY]))) {
nextState(); // Go to next state
timer.reset(); // Reset timer to 0
}
}
void Stacker::restart() {
// Reset game variables
gameState = Playing;
currY = 0;
currDirection = 1;
currX = 0;
currWidth = 4;
timer.reset();
// Initialize to empty board
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 16; j++) {
currState[i][j] = 0;
}
}
}
/**
* The constructor for the stacker game.
*/
Stacker::Stacker() {
timer.start();
}
/**
* Gives the stacker object a function that it can use to display the board state
*/
void Stacker::attachDisplay(void (*display)(int[][16])) {
displayFunc = display;
}
/**
Signals to the game that the user pressed the button.
*/
void Stacker::buttonPressed() {
int oldWidth = currWidth; // Save current width for loop condition
for (int i = 0; i < oldWidth; i++) {
// Landed on stack or bottom row
if (currY == 0 || currState[currX + i][currY-1] == 1) {
currState[currX + i][currY] = 1;
} else { // Misaligned
currWidth -= 1;
}
}
// No more pixels in moving bar left / you lose
if (currWidth == 0) {
displayLoss();
return;
}
currY += 1; // Increase level
currX = 0; // Move bar to left side of board
currDirection = 1; // Start bar moving right
if (currWidth > levelSizes[currY])
currWidth = levelSizes[currY];
}
void Stacker::printBoard() {
for (int i = 15; i >= 0; i--) {
for (int j = 0; j < 8; j++) {
printf("%d ", currState[j][i]);
}
printf("\r\n");
}
printf("\r\n");
}
void Stacker::displayWin() {
gameState = Won;
int winImage[8][16] = {
{ 0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,1,0,0,0,0,1,1,0,0,0,0,0 },
{ 0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,1,0,0,0,0,1,1,0,0,0,0,0 },
{ 0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0 }
};
displayFunc(winImage);
}
void Stacker::displayLoss() {
gameState = Lost;
int lossImage[8][16] = {
{ 0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,1,0,0,1,1,0,0,0,0,0 },
{ 0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,1,0,0,1,1,0,0,0,0,0 },
{ 0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0 }
};
displayFunc(lossImage);
}
GameState Stacker::getGameState() {
return gameState;
}