basic tui rendering
This commit is contained in:
parent
0db9339346
commit
6a3085af4e
19
2048.c
19
2048.c
|
@ -1,4 +1,19 @@
|
||||||
#include "2048.h"
|
#include "2048.h"
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
|
void game_add_block(Game game) {
|
||||||
|
start:
|
||||||
|
size_t x, y;
|
||||||
|
int value;
|
||||||
|
x = rand() % game.field_size;
|
||||||
|
y = rand() % game.field_size;
|
||||||
|
value = (rand() % 2) + 1;
|
||||||
|
if(game.field[x][y] != 0)
|
||||||
|
game.field[x][y] = value * 2;
|
||||||
|
else
|
||||||
|
goto start;
|
||||||
|
}
|
||||||
|
|
||||||
Game game_init(size_t field_size) {
|
Game game_init(size_t field_size) {
|
||||||
Game game;
|
Game game;
|
||||||
|
@ -8,6 +23,10 @@ Game game_init(size_t field_size) {
|
||||||
for (size_t i = 0; i < field_size; i++) {
|
for (size_t i = 0; i < field_size; i++) {
|
||||||
game.field[i] = calloc(field_size, sizeof(uint16_t) * field_size);
|
game.field[i] = calloc(field_size, sizeof(uint16_t) * field_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
time_t t;
|
||||||
|
srand((unsigned) time(&t));
|
||||||
|
game_add_block(game);
|
||||||
return game;
|
return game;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
9
2048.h
9
2048.h
|
@ -5,10 +5,11 @@
|
||||||
#define HEADER_2048
|
#define HEADER_2048
|
||||||
|
|
||||||
typedef enum Direction {
|
typedef enum Direction {
|
||||||
up,
|
NoDirection,
|
||||||
down,
|
Up,
|
||||||
right,
|
Down,
|
||||||
left
|
Right,
|
||||||
|
Left
|
||||||
} Direction;
|
} Direction;
|
||||||
|
|
||||||
typedef struct Game
|
typedef struct Game
|
||||||
|
|
42
tui.c
42
tui.c
|
@ -2,6 +2,9 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <curses.h>
|
#include <curses.h>
|
||||||
|
|
||||||
|
const space_between_cells_x = 6;
|
||||||
|
const space_between_cells_y = 2;
|
||||||
|
|
||||||
// entering and leaving screen using ANSI escape sequence
|
// entering and leaving screen using ANSI escape sequence
|
||||||
void tui_init() {
|
void tui_init() {
|
||||||
initscr();
|
initscr();
|
||||||
|
@ -16,10 +19,39 @@ void tui_destroy() {}
|
||||||
void tui_loop(Game *game) {
|
void tui_loop(Game *game) {
|
||||||
while(true){
|
while(true){
|
||||||
char input = getch();
|
char input = getch();
|
||||||
// up - 3, 107, 56
|
Direction dir = NoDirection;
|
||||||
// down - 2, 106, 50
|
// these are not magic numbers
|
||||||
// left - 4, 104, 52
|
// these are just codes for arrows,
|
||||||
// right - 5, 108, 54
|
// numpad and Vim bindings
|
||||||
// fprintf(stderr, "%d", input);
|
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);
|
||||||
|
|
||||||
|
// printing the game state is fun
|
||||||
|
for (size_t i = 0; i < game->field_size; i++) {
|
||||||
|
for (size_t j = 0; j < game->field_size; j++) {
|
||||||
|
mvprintw(i * space_between_cells_y, j * space_between_cells_x, "%d", game->field[i][j]);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue