fixed printing and some logic

This commit is contained in:
David Husicka 2021-11-15 16:33:31 +01:00
parent 6a3085af4e
commit 2956f61c69
2 changed files with 47 additions and 17 deletions

45
2048.c
View File

@ -3,16 +3,14 @@
#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;
do {
x = rand() % game.field_size;
y = rand() % game.field_size;
value = (rand() % 2) + 1;
} while (game.field[x][y] != 0);
game.field[x][y] = value * 2;
}
Game game_init(size_t field_size) {
@ -27,6 +25,7 @@ Game game_init(size_t field_size) {
time_t t;
srand((unsigned) time(&t));
game_add_block(game);
return game;
}
@ -39,5 +38,35 @@ void game_destroy(Game game) {
}
uint8_t game_move(Game *game, Direction direction) {
// setting up stuff for my funny two-way for loop
// well technically speaking about 4-way because there
// are two 2-way for loops
/*
int8_t y_dir = 0;
int8_t x_dir = 0;
size_t start_y = 0;
size_t start_x = 0;
switch(direction) {
case NoDirection:
return 0;
case Up:
y_dir = -1;
start_y = game->field_size - 1;
break;
case Down:
y_dir = 1;
break;
case Left:
x_dir = -1;
start_x = game->field_size - 1;
break;
case Right:
x_dir = 1;
break;
}
*/
game_add_block(*game);
return 0;
}

19
tui.c
View File

@ -2,8 +2,8 @@
#include <stdio.h>
#include <curses.h>
const space_between_cells_x = 6;
const space_between_cells_y = 2;
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() {
@ -18,6 +18,14 @@ 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; 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]);
}
}
char input = getch();
Direction dir = NoDirection;
// these are not magic numbers
@ -46,12 +54,5 @@ void tui_loop(Game *game) {
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]);
}
}
}
}