43 lines
948 B
C
43 lines
948 B
C
#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;
|
|
game.field_size = field_size;
|
|
game.score = 0;
|
|
game.field = malloc(sizeof(uint16_t *) * field_size);
|
|
for (size_t i = 0; i < field_size; i++) {
|
|
game.field[i] = calloc(field_size, sizeof(uint16_t) * field_size);
|
|
}
|
|
|
|
time_t t;
|
|
srand((unsigned) time(&t));
|
|
game_add_block(game);
|
|
return game;
|
|
}
|
|
|
|
void game_destroy(Game game) {
|
|
for (size_t i = 0; i < game.field_size; i++) {
|
|
free(game.field[i]);
|
|
}
|
|
free(game.field);
|
|
game.field = NULL;
|
|
}
|
|
|
|
uint8_t game_move(Game *game, Direction direction) {
|
|
return 0;
|
|
} |