2048-homework/src/tui.c

58 lines
1.4 KiB
C

#include "tui.h"
#include <stdio.h>
#include <curses.h>
const uint32_t space_between_cells_x = 6;
const uint32_t space_between_cells_y = 2;
// entering and leaving screen using ANSI escape sequence
void tui_init() {
initscr();
cbreak();
noecho();
nonl();
intrflush(stdscr, FALSE);
keypad(stdscr, TRUE);
}
void tui_destroy() {}
void tui_loop(Game *game) {
while(true){
// printing the game state is fun
clear();
for (size_t i = 0; i < game->field_size_y; i++) {
for (size_t j = 0; j < game->field_size_x; j++) {
mvprintw(i * space_between_cells_y, j * space_between_cells_x, "%d", game->field[i][j]);
}
}
char input = getch();
Direction dir = NoDirection;
// these are not magic numbers
// these are just codes for arrows,
// numpad and Vim bindings
switch (input) {
case 3:
case 107:
case 56:
dir = Up;
break;
case 2:
case 106:
case 50:
dir = Down;
break;
case 4:
case 104:
case 52:
dir = Left;
break;
case 5:
case 108:
case 54:
dir = Right;
break;
}
game_move(game, dir);
}
}