73 lines
1.7 KiB
C
73 lines
1.7 KiB
C
#include "2048.h"
|
|
#include <stdlib.h>
|
|
#include <time.h>
|
|
|
|
void game_add_block(Game game) {
|
|
size_t x, y;
|
|
int value;
|
|
do {
|
|
x = rand() % game.field_size_x;
|
|
y = rand() % game.field_size_y;
|
|
value = (rand() % 2) + 1;
|
|
} while (game.field[x][y] != 0);
|
|
game.field[x][y] = value * 2;
|
|
}
|
|
|
|
Game game_init(size_t field_size_x, size_t field_size_y) {
|
|
Game game;
|
|
game.field_size_x = field_size_x;
|
|
game.field_size_y = field_size_y;
|
|
game.score = 0;
|
|
game.field = malloc(sizeof(uint16_t *) * field_size_y);
|
|
for (size_t i = 0; i < field_size_y; i++) {
|
|
game.field[i] = calloc(field_size_x, sizeof(uint16_t) * field_size_x);
|
|
}
|
|
|
|
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_y; i++) {
|
|
free(game.field[i]);
|
|
}
|
|
free(game.field);
|
|
game.field = NULL;
|
|
}
|
|
|
|
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;
|
|
} |