forked from Ray5mz/2048
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.c
More file actions
40 lines (37 loc) · 973 Bytes
/
utils.c
File metadata and controls
40 lines (37 loc) · 973 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <stdlib.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
#include "include/cgame.h"
#define GRID_SIZE 4
int add_random_tile(Game* game) {
int tiles = empty_tiles(game);
int tile = ((rand() / 16) * tiles) / (RAND_MAX / 16);
int n = 0;
for (int i = 0; i < 16; i++) {
if (game->board[i] == 0) {
if (n == tile) {
game->board[i] = rand() > RAND_MAX / 3 * 2 ? 2 : 4;
return i;
} else {
n++;
}
}
}
return -1;
}
int add_random_tileM(Game* game) {
int tiles = empty_tiles(game);
int tile = ((rand() / 16) * tiles) / (RAND_MAX / 16);
int n = 0;
for (int i = 0; i < 16; i++) {
if (game->ai_board[i] == 0) {
if (n == tile) {
game->ai_board[i] = rand() > RAND_MAX / 3 * 2 ? 2 : 4;
return i;
} else {
n++;
}
}
}
return -1;
}