-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathboard.rs
295 lines (265 loc) · 9.06 KB
/
board.rs
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
use piston::input::*;
use rand::Rng;
use super::BOARD_WIDTH;
use super::BOARD_HEIGHT;
use super::block::Shape;
use super::block::Tetromino;
use super::block::TETROMINOS;
// 1st entry is # of points for clearing 1 line, etc.
const SCORES_PER_LINE: [u64; 4] = [80, 200, 600, 2400];
const GHOST_PIECE_OPACITY: f32 = 0.35;
#[derive(PartialEq, Debug, Clone, Copy)]
pub enum GameState {
Playing,
Paused,
Over // donezo. player lost or quit
}
pub struct Board {
pub cells: [[u8; BOARD_WIDTH as usize]; BOARD_HEIGHT as usize],
pub current_piece: Tetromino, // current active Tetromino
pub ghost_piece: Tetromino, // ghost piece to display at bottom
pub next_piece: Tetromino,
pub state: GameState,
pub score: u64,
pub token: i32, // token identifier for use by the game server
pub new_block: bool, // true for 1 iteration if new block, false otherwise
// line_counts[i] = # of filled blocks in row i
line_counts: [i64; BOARD_HEIGHT as usize],
// for "random bag" generation of the next tetromino
tetrominos_bag: Vec<Tetromino>,
}
impl Board {
pub fn init_board() -> Board {
let mut bag = TETROMINOS.to_vec();
::rand::thread_rng().shuffle(&mut bag);
let first_piece: Tetromino = bag.remove(0);
let next_piece: Tetromino = bag.remove(0);
let mut ghost_piece: Tetromino = first_piece.clone();
ghost_piece.y_offset = BOARD_HEIGHT as f64 - ghost_piece.block_height();
// some shapes are drawn starting from 2nd, not 1st row in block
if ghost_piece.name == Shape::T || ghost_piece.name == Shape::S || ghost_piece.name == Shape::Z {
ghost_piece.y_offset -= 1.0;
}
ghost_piece.color[3] = GHOST_PIECE_OPACITY; // transparent
Board {
cells: [[0; BOARD_WIDTH as usize]; BOARD_HEIGHT as usize],
current_piece: first_piece,
ghost_piece: ghost_piece,
next_piece: next_piece,
state: GameState::Playing,
score: 0,
token: 0,
new_block: true,
line_counts: [0; BOARD_HEIGHT as usize],
tetrominos_bag: bag
}
}
pub fn handle_key_press(&mut self, inp: &Input) {
match *inp {
Input::Press(but) => {
match but {
Button::Keyboard(Key::Up) => {
self.current_piece.rotate_right();
self.ghost_piece.rotate_right();
// undo rotation if it was invalid...lol
if self.current_piece_out_of_bounds() {
self.current_piece.rotate_left();
self.ghost_piece.rotate_left();
}
self.update_ghost_piece();
}
Button::Keyboard(Key::Left) => {
if self.can_move_current_piece_left() {
self.current_piece.move_left();
self.update_ghost_piece();
}
}
Button::Keyboard(Key::Right) => {
if self.can_move_current_piece_right() {
self.current_piece.move_right();
self.update_ghost_piece();
}
}
Button::Keyboard(Key::Down) => {
if self.can_move_piece_down(&self.current_piece) {
self.current_piece.move_down();
}
}
Button::Keyboard(Key::Space) => {
// pressing spacebar drops piece to bottom
while self.can_move_piece_down(&self.current_piece) {
self.current_piece.move_down();
}
}
_ => {}
}
}
_ => {}
}
}
// iterate thru board and clear the specified row
fn clear_row(&mut self, row: usize) {
for col in 0..BOARD_WIDTH {
self.cells[row][col as usize] = 0;
}
self.line_counts[row] = 0;
// move all cells above (and including) the cleared row down by one
for r in (1..(row+1)).rev() {
for col in 0..BOARD_WIDTH {
self.cells[r as usize][col as usize] = self.cells[(r-1) as usize][col as usize];
}
self.line_counts[r as usize] = self.line_counts[(r-1) as usize];
}
}
pub fn clear_line_if_needed(&mut self) {
// iterate through rows affected by current piece
let mut rows_affected = Vec::new();
let current_y = self.current_piece.y_offset;
for r in 0..self.current_piece.blocks.len() {
for c in 0..self.current_piece.blocks[0].len() {
if self.current_piece.blocks[r][c] == 1 {
let board_y: usize = ((r as f64) + current_y) as usize;
rows_affected.push(board_y);
}
}
}
let mut rows_cleared = 0;
for row in rows_affected.iter() {
if self.line_counts[*row] == BOARD_WIDTH {
self.clear_row(*row);
rows_cleared += 1;
}
}
if rows_cleared > 0 {
self.score += SCORES_PER_LINE[rows_cleared - 1];
}
}
pub fn advance_board(&mut self) {
if self.state != GameState::Playing {
return
}
// check if game is over or not
// check top row of board and see if any of them are filled?
for col in 0..self.cells[0].len() {
if self.cells[0][col] == 1{
self.state = GameState::Over;
return
}
}
// make the existing piece fall
if self.can_move_piece_down(&self.current_piece) {
self.new_block = false;
self.current_piece.move_down();
} else {
// add piece to board cells
self.set_piece_on_board();
self.score += 10;
// clear line if necessary
self.clear_line_if_needed();
// get new piece
self.current_piece = self.next_piece;
self.next_piece = self.get_next_piece();
self.new_block = true;
self.update_ghost_piece();
}
}
fn can_move_piece_down(&self, piece: &Tetromino) -> bool {
if piece.bottommost() + 1.0 >= BOARD_HEIGHT as f64 {
return false
}
// check if piece will intersect with a piece already on the board
let current_x = piece.x_offset;
let current_y = piece.y_offset;
for row in 0..piece.blocks.len() {
for col in 0..piece.blocks[0].len() {
if piece.blocks[row][col] == 1 {
let board_x: i64 = (col as i64) + (current_x as i64);
let board_y: i64 = (row as i64) + (current_y as i64) + 1;
if self.cells[board_y as usize][board_x as usize] == 1 {
return false;
}
}
}
}
true
}
fn can_move_current_piece_left(&self) -> bool {
if self.current_piece.leftmost() - 1.0 < 0.0 {
return false;
}
// check if will intersect with a piece already on the board
let current_x = self.current_piece.x_offset;
let current_y = self.current_piece.y_offset;
for row in 0..self.current_piece.blocks.len() {
for col in 0..self.current_piece.blocks[0].len() {
if self.current_piece.blocks[row][col] == 1 {
let board_x: i64 = (col as i64) + (current_x as i64) - 1;
let board_y: i64 = (row as i64) + (current_y as i64);
if self.cells[board_y as usize][board_x as usize] == 1 {
return false;
}
}
}
}
true
}
fn can_move_current_piece_right(&self) -> bool {
if self.current_piece.rightmost() + 1.0 >= BOARD_WIDTH as f64 {
return false;
}
// check if will intersect with a piece already on the board
let current_x = self.current_piece.x_offset;
let current_y = self.current_piece.y_offset;
for row in 0..self.current_piece.blocks.len() {
for col in 0..self.current_piece.blocks[0].len() {
if self.current_piece.blocks[row][col] == 1 {
let board_x: i64 = (col as i64) + (current_x as i64) + 1;
let board_y: i64 = (row as i64) + (current_y as i64);
if self.cells[board_y as usize][board_x as usize] == 1 {
return false;
}
}
}
}
true
}
fn current_piece_out_of_bounds(&self) -> bool {
if self.current_piece.leftmost() < 0.0 || self.current_piece.rightmost() >= BOARD_WIDTH as f64 || self.current_piece.bottommost() >= BOARD_HEIGHT as f64 {
return true
}
false
}
pub fn set_piece_on_board(&mut self) {
let current_x = self.current_piece.x_offset;
let current_y = self.current_piece.y_offset;
for row in 0..self.current_piece.blocks.len() {
for col in 0..self.current_piece.blocks[0].len() {
if self.current_piece.blocks[row][col] == 1 {
let x: i64 = (col as i64) + (current_x as i64);
let y: i64 = (row as i64) + (current_y as i64);
self.cells[y as usize][x as usize] = 1;
self.line_counts[y as usize] += 1;
}
}
}
}
pub fn get_next_piece(&mut self) -> Tetromino {
if self.tetrominos_bag.len() == 0 {
self.tetrominos_bag = TETROMINOS.to_vec();
::rand::thread_rng().shuffle(&mut self.tetrominos_bag);
}
self.tetrominos_bag.remove(0)
}
// helper function to update position of ghost piece w/ current piece
pub fn update_ghost_piece(&mut self) {
if self.current_piece.name != self.ghost_piece.name {
self.ghost_piece = self.current_piece.clone();
self.ghost_piece.color[3] = GHOST_PIECE_OPACITY;
}
self.ghost_piece.y_offset = 0.0;
self.ghost_piece.x_offset = self.current_piece.x_offset;
while self.can_move_piece_down(&self.ghost_piece) {
self.ghost_piece.move_down();
}
}
}