2048-homework/2048.c

73 lines
1.7 KiB
C
Raw Normal View History

2021-11-15 08:57:27 +00:00
#include "2048.h"
2021-11-15 10:00:37 +00:00
#include <stdlib.h>
#include <time.h>
void game_add_block(Game game) {
size_t x, y;
int value;
2021-11-15 15:33:31 +00:00
do {
2021-11-15 21:20:16 +00:00
x = rand() % game.field_size_x;
y = rand() % game.field_size_y;
2021-11-15 15:33:31 +00:00
value = (rand() % 2) + 1;
} while (game.field[x][y] != 0);
game.field[x][y] = value * 2;
2021-11-15 10:00:37 +00:00
}
2021-11-15 08:57:27 +00:00
2021-11-15 21:20:16 +00:00
Game game_init(size_t field_size_x, size_t field_size_y) {
2021-11-15 08:57:27 +00:00
Game game;
2021-11-15 21:20:16 +00:00
game.field_size_x = field_size_x;
game.field_size_y = field_size_y;
2021-11-15 08:57:27 +00:00
game.score = 0;
2021-11-15 21:20:16 +00:00
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);
2021-11-15 08:57:27 +00:00
}
2021-11-15 10:00:37 +00:00
time_t t;
srand((unsigned) time(&t));
game_add_block(game);
2021-11-15 15:33:31 +00:00
2021-11-15 08:57:27 +00:00
return game;
}
void game_destroy(Game game) {
2021-11-15 21:20:16 +00:00
for (size_t i = 0; i < game.field_size_y; i++) {
2021-11-15 08:57:27 +00:00
free(game.field[i]);
}
free(game.field);
game.field = NULL;
}
uint8_t game_move(Game *game, Direction direction) {
2021-11-15 15:33:31 +00:00
// 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);
2021-11-15 08:57:27 +00:00
return 0;
}