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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
|
extern crate termios;
use rand::Rng;
use nonblock::NonBlockingReader;
const FIELD_COLS: usize = 10;
const FIELD_ROWS_VISIBLE: usize = 20;
const FIELD_ROWS: usize = FIELD_ROWS_VISIBLE + 20;
const FIELD_SIZE: usize = FIELD_COLS * FIELD_ROWS;
const FIGURE_SIZE_COLS: usize = 4;
const FIGURE_SIZE_ROWS: usize = 4;
const FIGURE_SIZE: usize = FIGURE_SIZE_COLS*FIGURE_SIZE_ROWS;
const CELLS_I: [u8; FIGURE_SIZE] = [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0];
const CELLS_L2:[u8; FIGURE_SIZE] = [0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0];
const CELLS_L: [u8; FIGURE_SIZE] = [0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0];
const CELLS_B: [u8; FIGURE_SIZE] = [0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0];
const CELLS_S: [u8; FIGURE_SIZE] = [0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0];
const CELLS_T: [u8; FIGURE_SIZE] = [0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0];
const CELLS_Z: [u8; FIGURE_SIZE] = [0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0];
enum StepResult {
StateChanged,
Quit,
}
#[derive(Clone)]
#[derive(Debug)]
enum FigureKind {
I,
L2,
L,
T,
B,
S,
Z,
}
struct Timeout {
beginning: nix::sys::time::TimeSpec,
duration: std::time::Duration,
}
impl Default for Timeout {
#[inline]
fn default() -> Self {
Self {
beginning: nix::sys::time::TimeSpec::new(0, 0),
duration: std::time::Duration::default(),
}
}
}
impl Timeout {
fn from_duration(d: std::time::Duration) -> Self {
Timeout{
beginning: nix::time::clock_gettime(nix::time::ClockId::CLOCK_MONOTONIC).unwrap(),
duration: d,
}
}
fn is_elapsed(&self) -> bool {
std::time::Duration::from(self.beginning) + self.duration <= std::time::Duration::from(nix::time::clock_gettime(nix::time::ClockId::CLOCK_MONOTONIC).unwrap())
}
}
#[derive(Clone)]
struct Figure {
kind: FigureKind,
cells: [u8; FIGURE_SIZE],
}
impl Figure {
fn kind_random() -> Self {
return match rand::thread_rng().gen_range(0..7) {
0 => Self::kind_i(),
1 => Self::kind_l2(),
2 => Self::kind_l(),
3 => Self::kind_b(),
4 => Self::kind_s(),
5 => Self::kind_t(),
6 => Self::kind_z(),
_ => unreachable!(),
};
}
const fn kind_i() -> Self { Self{ cells: CELLS_I, kind: FigureKind::I } }
const fn kind_l2() -> Self { Self{ cells: CELLS_L2, kind: FigureKind::L2 } }
const fn kind_l() -> Self { Self{ cells: CELLS_L, kind: FigureKind::L } }
const fn kind_b() -> Self { Self{ cells: CELLS_B, kind: FigureKind::B } }
const fn kind_s() -> Self { Self{ cells: CELLS_S, kind: FigureKind::S } }
const fn kind_t() -> Self { Self{ cells: CELLS_T, kind: FigureKind::T } }
const fn kind_z() -> Self { Self{ cells: CELLS_Z, kind: FigureKind::Z } }
fn rotate_cw_4x4(&mut self) {
let mut tmp: [u8; 4 * 4] = Default::default();
for y in 0..4 {
for x in 0..4 {
tmp[y + (3 - x) * 4] = self.cells[x + y * 4];
}
}
self.cells = tmp;
}
fn rotate_cw_3x3(&mut self) {
let mut tmp: [u8; 3 * 3] = Default::default();
for y in 0..3 {
for x in 0..3 {
tmp[y + (2 - x) * 3] = self.cells[x + y * 4];
}
}
for y in 0..3 {
for x in 0..3 {
self.cells[x + y * 4] = tmp[x + y * 3];
}
}
}
fn rotate(&mut self) {
match self.kind {
FigureKind::I => self.rotate_cw_4x4(),
FigureKind::L2 => self.rotate_cw_3x3(),
FigureKind::L => self.rotate_cw_3x3(),
FigureKind::B => {},
FigureKind::S => self.rotate_cw_3x3(),
FigureKind::T => self.rotate_cw_3x3(),
FigureKind::Z => self.rotate_cw_3x3(),
}
}
}
#[derive(Clone)]
struct Field {
cells: [u8; FIELD_SIZE],
}
impl Default for Field {
#[inline]
fn default() -> Self {
Self { cells: [0; FIELD_SIZE] }
}
}
impl Field {
fn render(&mut self, figure: &Figure, x: isize, y: isize) {
for i in 0..FIGURE_SIZE {
let f_x = (i % FIGURE_SIZE_COLS) as isize + x;
let f_y = (i / FIGURE_SIZE_ROWS) as isize + y;
if f_x < 0 {
continue;
} else if f_x >= FIELD_COLS as isize {
continue;
} else if f_y < 0 {
continue;
} else if f_y >= FIELD_ROWS as isize {
continue;
}
if figure.cells[i] != 0 {
self.cells[f_x as usize + f_y as usize * FIELD_COLS] = figure.cells[i];
}
}
}
fn has_collision(&self, figure: &Figure, x: isize, y: isize) -> bool {
for i in 0..FIGURE_SIZE {
let f_x = (i % FIGURE_SIZE_COLS) as isize + x;
let f_y = (i / FIGURE_SIZE_ROWS) as isize + y;
if figure.cells[i] != 0 {
if f_x < 0 {
return true;
} else if f_x >= FIELD_COLS as isize {
return true;
} else if f_y < 0 {
return true;
} else if f_y >= FIELD_ROWS as isize {
return true;
} else if self.cells[f_x as usize + f_y as usize * FIELD_COLS] != 0 {
return true;
}
}
}
false
}
fn is_line_complete(&self, line: usize) -> bool {
assert!(line < FIELD_ROWS);
self.cells[FIELD_COLS * line..][..FIELD_COLS].iter().fold(1, |a, e| a & e) != 0
}
fn kill_line(&mut self, line: usize) {
for row in line..FIELD_ROWS - 1 {
for col in 0..FIELD_COLS {
self.cells[col + row * FIELD_COLS] = self.cells[col + (row + 1) * FIELD_COLS];
}
}
for col in 0..FIELD_COLS {
self.cells[col + (FIELD_ROWS - 1) * FIELD_COLS] = 0;
}
}
}
struct Game {
field: Field,
figure: Figure,
x: isize,
y: isize,
fall_timeout: Timeout,
place_timeout: Option<Timeout>,
}
impl Default for Game {
#[inline]
fn default() -> Self {
Self{
field: Field::default(),
figure: Figure::kind_random(),
x: 3,
y: 18,
fall_timeout: Timeout::from_duration(std::time::Duration::from_millis(500)),
place_timeout: None,
}
}
}
impl Game {
fn place_figure(&mut self) {
self.field.render(&self.figure, self.x, self.y);
self.figure = Figure::kind_random();
(self.x, self.y) = (3, 18);
self.stop_place_timeout();
for line in 0..FIELD_ROWS - 1 {
while self.field.is_line_complete(line) {
self.field.kill_line(line);
}
}
}
fn reset_fall_timeout(&mut self) {
self.fall_timeout = Timeout::from_duration(std::time::Duration::from_millis(500));
}
fn start_place_timeout(&mut self) {
self.place_timeout = Some(Timeout::from_duration(std::time::Duration::from_millis(500)));
}
fn stop_place_timeout(&mut self) {
self.place_timeout = None
}
fn reset_place_timeout(&mut self) {
if let Some(_) = self.place_timeout {
self.start_place_timeout();
}
}
fn handle_input(&mut self, c: char) -> Option<StepResult> {
match c {
'q' => return Some(StepResult::Quit),
' ' => {
while !self.field.has_collision(&self.figure, self.x, self.y - 1) {
self.y -= 1;
}
self.place_figure()
},
'h' => {
if self.field.has_collision(&self.figure, self.x, self.y) {
self.x -= 1
} else if !self.field.has_collision(&self.figure, self.x - 1, self.y) {
self.x -= 1
}
}
'j' => {
if self.field.has_collision(&self.figure, self.x, self.y) {
self.y -= 1
} else if !self.field.has_collision(&self.figure, self.x, self.y - 1) {
self.y -= 1
}
}
';' | 'k' => {
let mut figure = self.figure.clone();
figure.rotate();
if !self.field.has_collision(&figure, self.x, self.y) {
self.figure.rotate()
}
}
'l' => {
if self.field.has_collision(&self.figure, self.x, self.y) {
self.x += 1
} else if !self.field.has_collision(&self.figure, self.x + 1, self.y) {
self.x += 1
}
}
_ => {},
}
self.reset_place_timeout();
Some(StepResult::StateChanged)
}
fn advance_figure(&mut self) {
if self.field.has_collision(&self.figure, self.x, self.y - 1) {
self.place_figure();
} else {
self.y -= 1;
}
if self.field.has_collision(&self.figure, self.x, self.y - 1) {
self.start_place_timeout();
}
self.reset_fall_timeout();
}
fn step(&mut self, c_optional: Option<char>) -> Option<StepResult> {
let handle_input_result = if let Some(c) = c_optional {
self.handle_input(c)
} else {
None
};
let advance_figure_result = if let Some(t) = &self.place_timeout {
if t.is_elapsed() {
self.advance_figure();
Some(StepResult::StateChanged)
} else {
None
}
} else if self.fall_timeout.is_elapsed() {
self.advance_figure();
Some(StepResult::StateChanged)
} else {
None
};
if let None = handle_input_result {
advance_figure_result
} else {
handle_input_result
}
}
}
fn print_field(field: &Field) {
cursor_to_home();
for y in 0..FIELD_ROWS_VISIBLE {
for x in 0..FIELD_COLS {
let c = field.cells[(FIELD_ROWS_VISIBLE - 1 - y) * FIELD_COLS + x];
if c == 0 {
print!(". ");
} else {
print!("██");
}
}
print!("\r\x1b[1B");
}
}
fn display_game(game: &Game) {
cursor_to_home();
let mut field = game.field.clone();
field.render(&game.figure, game.x, game.y);
print_field(&field);
if game.field.has_collision(&game.figure, game.x, game.y) {
println!("Collision!");
} else {
clear_line();
}
}
fn clear_screen() {
print!("\x1B[2J");
}
fn cursor_to_home() {
print!("\x1B[H");
}
fn clear_line() {
println!("\x1B[2K");
}
fn main() {
let termios_state_initial = termios::Termios::from_fd(0).unwrap();
let mut termios_state = termios_state_initial.clone();
let c_lflag = termios_state.c_lflag;
termios_state.c_lflag &= !(termios::ICANON | termios::ECHO);
termios::tcsetattr(0, termios::TCSADRAIN, &termios_state).unwrap();
termios_state.c_lflag = c_lflag;
clear_screen();
let mut game = Game::default();
display_game(&game);
let mut noblock_stdin = NonBlockingReader::from_fd(std::io::stdin()).unwrap();
'outer: while !noblock_stdin.is_eof() {
let mut buf = String::new();
noblock_stdin.read_available_to_string(&mut buf).unwrap();
let mut changed = false;
if buf.len() > 0 {
for c in buf.bytes() {
if let Some(result) = game.step(Some(c as char)) {
match result {
StepResult::StateChanged => changed = true,
StepResult::Quit => break 'outer,
}
}
}
} else {
if let Some(result) = game.step(None) {
match result {
StepResult::StateChanged => changed = true,
StepResult::Quit => break 'outer,
}
}
}
if changed {
display_game(&game);
}
std::thread::sleep(std::time::Duration::from_millis(1));
}
termios::tcsetattr(0, termios::TCSADRAIN, &termios_state_initial).unwrap();
}
|