1 /* Copyright 2017 Dan Amlund Thomsen
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "tetris_text.h"
23 static char empty_piece[7][7] = { { 0, 0, 0, 0, 0, 0, 0 },
24 { 0, 0, 0, 0, 0, 0, 0 },
25 { 0, 0, 0, 0, 0, 0, 0 },
26 { 0, 0, 0, 0, 0, 0, 0 },
27 { 0, 0, 0, 0, 0, 0, 0 },
28 { 0, 0, 0, 0, 0, 0, 0 },
29 { 0, 0, 0, 0, 0, 0, 0 } };
31 static char temp_piece[7][7];
36 static void clear_piece(char piece[7][7]) {
37 for (int y = 0; y < 7; y++) {
38 for (int x = 0; x < 7; x++) {
44 static void copy_piece_from_to(char from[7][7], char to[7][7]) {
45 for (int y = 0; y < 7; y++) {
46 for (int x = 0; x < 7; x++) {
47 to[x][y] = from[x][y];
52 static void rotate_piece(char piece[7][7]) {
54 for (int y = 0; y < 7; y++) {
55 for (int x = y + 1; x < 7; x++) {
56 char tmp = piece[y][x];
57 piece[y][x] = piece[x][y];
63 for (int y = 0; y < 7; y++) {
64 for (int x = 0; x < 3; x++) {
65 char tmp = piece[y][6 - x];
66 piece[y][6 - x] = piece[y][x];
72 static char get_shape_char(int shape) {
85 static void set_piece(char piece[7][7], int shape, int rotation) {
89 if (rotation % 2 == 0) {
113 for (int i = 0; i < rotation; i++) {
124 for (int i = 0; i < rotation; i++) {
137 if (rotation % 2 == 0) {
161 for (int i = 0; i < rotation; i++) {
166 if (rotation % 2 == 0) {
186 static void send_deletes(int deletes) {
187 for (int i = 0; i < deletes; i++) {
188 tetris_send_delete();
192 static void send_backspaces(int backspaces) {
193 for (int i = 0; i < backspaces; i++) {
194 tetris_send_backspace();
199 static void send_goto_xy(int x, int y) {
218 static void draw_row(char c, const char oldrow[7], const char newrow[7], int x, int y) {
219 char str[2] = { c, 0 };
220 char row_is_del[7] = { 0 };
223 for (int px = 0; px < 7; px++) {
224 if (oldrow[px] && !newrow[px]) {
227 if (newrow[px] || oldrow[px]) {
228 if (first == -1) first = px;
234 if (curx > x + last + 1) {
235 send_goto_xy(x + last + 1, cury);
237 if (curx < x + first) {
238 send_goto_xy(x + first, cury);
240 send_goto_xy(curx, y);
241 send_deletes((x + last + 1) - curx);
242 send_backspaces(curx - (x + first));
243 for (int i = first; i <= last; i++) {
245 tetris_send_string(".");
247 tetris_send_string(str);
254 static void move_piece_from_to(char from[7][7], char to[7][7], int xadd, int yadd) {
255 for (int y = 0; y < 7; y++) {
256 for (int x = 0; x < 7; x++) {
257 if (x + xadd >= 0 && x + xadd < 7 && y + yadd >= 0 && y + yadd < 7) {
258 to[y][x] = from[y + yadd][x + xadd];
266 static void draw_piece(char c, int x, int y, char oldpiece[7][7], char piece[7][7]) {
267 for (int py = 0; py < 7; py++) {
268 draw_row(c, oldpiece[py], piece[py], x, y + py);
272 static void draw_piece_moved(char c, int x, int y, char piece[7][7], int oldxadd, int oldyadd) {
273 move_piece_from_to(piece, temp_piece, oldxadd, oldyadd);
274 draw_piece(c, x, y, temp_piece, piece);
277 static int is_piece_hitting(char board[20][10], char piece[7][7], int x, int y) {
278 for (int py = 0; py < 7; py++) {
279 for (int px = 0; px < 7; px++) {
281 (px + x >= 10 || px + x < 0
282 || py + y >= 20 || py + y < 0
283 || board[py + y][px + x])) {
291 static void add_piece_to_board(char piece[7][7], char board[20][10], int x, int y) {
292 for (int py = 0; py < 7; py++) {
293 for (int px = 0; px < 7; px++) {
295 board[py + y][px + x] = piece[py][px];
301 static void draw_board_line(void) {
302 //send_string("l l");
303 tetris_send_string("l..........l");
304 tetris_send_newline();
306 static void init(void) {
307 for (int i = 0; i < 20; i++) {
310 tetris_send_string("doooooooooob");
315 static int get_piece_min_y(char piece[7][7]) {
316 for (int y = 0; y < 7; y++) {
317 for (int x = 0; x < 7; x++) {
325 static int clear_lines(char board[20][10]) {
326 int cleared_lines = 0;
327 for (int y = 19; y >= 0; y--) {
329 for (int x = 0; x < 10; x++) {
337 send_backspaces(12); // delete line contents
339 tetris_send_backspace();
344 if (cleared_lines > 0) {
345 // move cleared lines down on board
346 for (int x = 0; x < 10; x++) {
347 board[y + cleared_lines][x] = board[y][x];
352 // clear cleared top lines
353 for (int y = 0; y < cleared_lines; y++) {
354 for (int x = 0; x < 10; x++) {
358 if (cleared_lines > 0) {
360 for (int i = 0; i < cleared_lines; i++) {
366 return cleared_lines;
369 static uint8_t myrandom(uint8_t seed) {
370 uint8_t out = seed >> 1;
377 static char piece[7][7];
378 static char board[20][10];
386 static int next_down;
387 static int down_delay;
388 static int first_run;
389 static int game_over;
391 void tetris_start(uint8_t seed) {
392 for (int y = 0; y < 20; y++) {
393 for (int x = 0; x < 10; x++) {
407 copy_piece_from_to(empty_piece, piece);
418 int tetris_tick(int ms_since_previous_tick) {
423 time += ms_since_previous_tick;
425 if (first_run || time > next_down) {
426 if (first_run || is_piece_hitting(board, piece, x, y + 1)) {
428 add_piece_to_board(piece, board, x, y);
430 score += clear_lines(board);
432 down_delay = 500 - score * 10;
433 if (down_delay < 100) {
440 set_piece(piece, shape, rotation);
443 y = - get_piece_min_y(piece);
444 draw_piece_moved(get_shape_char(shape), 1 + x, y, piece, 0, 0);
446 if (is_piece_hitting(board, piece, x, y)) {
448 send_goto_xy(12, 10);
449 tetris_send_string(" game over");
451 tetris_send_string(" score ");
453 sprintf(tmp, "%d", score);
454 tetris_send_string(tmp);
459 draw_piece_moved(get_shape_char(shape), 1 + x, y, piece, 0, +1);
461 next_down = time + down_delay;
464 switch (tetris_get_keypress()) {
466 int oldrotation = rotation;
467 rotation = (rotation + 1) % 4;
468 copy_piece_from_to(piece, temp_piece);
469 set_piece(piece, shape, rotation);
470 if (is_piece_hitting(board, piece, x, y)) {
471 rotation = oldrotation;
472 set_piece(piece, shape, rotation);
474 draw_piece(get_shape_char(shape), 1 + x, y, temp_piece, piece);
479 if (!is_piece_hitting(board, piece, x - 1, y)) {
481 draw_piece_moved(get_shape_char(shape), 1 + x, y, piece, -1, 0);
486 while (!is_piece_hitting(board, piece, x, y + 1)) {
490 draw_piece(get_shape_char(shape), x + 1, starty, piece, empty_piece);
491 draw_piece(get_shape_char(shape), x + 1, y, empty_piece, piece);
493 next_down = time + down_delay;
497 if (!is_piece_hitting(board, piece, x + 1, y)) {
499 draw_piece_moved(get_shape_char(shape), 1 + x, y, piece, 1, 0);