-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathboard.h
178 lines (142 loc) · 3.77 KB
/
board.h
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
* Copyright 2014-2015 by Wade T. Cline (Frostsnow)
*
* "2048" is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* "2048" is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with "2048". If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef board_H
#define board_H
#include "include.h"
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#define BOARD_COLUMNS 4
#define BOARD_ROWS 4
// Likelihood of a '4' appearing on the board represented as a percentage
// multiplied by 100.
#define BOARD_4SPAWN_CHANCE 15
/**
* Represents the in-game board.
*/
struct board {
// Total score for the current game.
unsigned score_current;
// File handle for the top score.
int score_file;
// Top score.
unsigned score_top;
// Holds the value of each tile.
unsigned tiles[BOARD_ROWS][BOARD_COLUMNS];
};
/**
* Returns 0 if the game is not over; >0 if the game is over and the user
* has won, or <0 if the user has lost.
*/
int board_done(struct board* board);
/**
* Clean up any resources associated with the speicifed board.
*/
void board_free(struct board* board);
/**
* Returns the number of empty tiles in the board.
*/
unsigned board_get_tiles_empty(struct board* board);
/**
* Initializes the specified board.
*/
void board_init(struct board* board);
/**
* Initialize the specified board's top score and score file.
*/
void board_init_score(struct board* board);
/**
* Merge tiles in the board downwards.
*/
int board_merge_down(struct board* board);
/**
* Merge tiles in the board leftwards.
*/
int board_merge_left(struct board* board);
/**
* Merge tiles in the board rightwards.
*/
int board_merge_right(struct board* board);
/**
* Merge tiles in the board upwards.
*/
int board_merge_up(struct board* board);
/**
* Processes user move-down request.
*/
int board_move_down(struct board* board);
/**
* Process user move-left request.
*/
int board_move_left(struct board* board);
/**
* Process user move-right request.
*/
int board_move_right(struct board* board);
/**
* Process user move-up request.
*/
int board_move_up(struct board* board);
/**
* Spawn a new tile on the board.
*
* I didn't know what to call this, so "plopping" a value on the board sounds
* legit enough.
*/
void board_plop(struct board* board);
/**
* Print the board to 'stdout'.
*/
void board_print(struct board* board);
/**
* Start a new game.
*/
void board_reset(struct board* board);
/**
* Call whenever the current score is updated in order to propogate changes
* to the top score and score file.
*/
void board_score_updated(struct board* board);
/**
* Shift all the elements in the board down.
*/
int board_shift_down(struct board* board);
/**
* Shift all the elements in the board left.
*/
int board_shift_left(struct board* board);
/**
* Shift all the elements in the board right.
*/
int board_shift_right(struct board* board);
/**
* Shift all the elements in the board up.
*/
int board_shift_up(struct board* board);
/**
* Sets the values of all tiles on the specified board to '0'.
*/
void board_tiles_clear(struct board* board);
/**
* Merges the specified tile 'b' into the specified tile 'a' and updates the
* total score.
*/
void board_tiles_merge(struct board* board, unsigned* a, unsigned* b);
#endif // board_H