-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgame.h
More file actions
118 lines (90 loc) · 3.14 KB
/
game.h
File metadata and controls
118 lines (90 loc) · 3.14 KB
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
#ifndef _GAME_H
#define _GAME_H
// this is where we handle the game
#include <string.h> // strcpy
#include <stdlib.h> // free
#include <time.h> // time_t
#include "list.h" // LIST, list_*
typedef enum {
P_1=0, // de sorte que !P_2 == P_1 et !P_1 == P_2
P_2=1
} ePlayer;
typedef enum {
T_OK,
T_INVALID,
T_WIN
} eTurnTypes;
typedef struct { // define a turn of the game
ePlayer player;
eTurnTypes type;
time_t t_remaining;
} sGameTurn;
// stocke la configuration de la partie => statique
typedef struct {
char gamename[64]; // game name
char playername[2][9]; // players names
time_t t_total;
time_t t_turn;
ePlayer phost, pjoin;
} sGameConf;
typedef enum {
GS_INIT, // initial state
GS_TURN, // turn in progress (see ePlayer to know which player turn it is)
GS_WIN // a player won (see ePlayer to know which one won)
} eGameState;
// stocke le statut du jeu => évolue au cours de la partie
typedef struct {
eGameState state;
ePlayer pcurr;
time_t t_remaining;
} sGameState;
typedef struct { // initial state of the game, sent to the new user
sGameConf conf;
sGameState st;
} sGameInit;
typedef struct {
// shared data
sGameConf conf; // configuration of the game
sGameState st; // real-time status of the game
// local data
ePlayer pme;
LIST *turns; // list of game turns
} sGame;
// new game from scratch
sGame * game_new (sGame *g, const char *name);
char * game_get_filepath (const sGame *g);
// game from/to .histo file
LIST * game_histo_getlist (); // get list of histo files in /tmp
// LIST * game_histo_destroylist (LIST *l);
int game_histo_check (char *filepath);
int game_histo_load (sGame *g, const char *name);
int game_histo_save (const sGame *g);
// manage a game
int game_playturn (sGame *g, const sGameTurn *t);
// destroy game
// void game_destroy (sGame *g)
// static functions declarations:
// game configuration
static inline
sGameConf * game_get_conf (sGame *g, sGameConf *c) { if(c) { memcpy(c, &g->conf, sizeof(*c)); return c; } else return &g->conf; }
static inline
void game_set_conf (sGame *g, sGameConf *c) { memcpy(&g->conf, c, sizeof(*c)); }
// game state
static inline
sGameState * game_get_state (sGame *g, sGameState *s) { if(s) { memcpy(s, &g->st, sizeof(*s)); return s; } else return &g->st; }
static inline
void game_set_state (sGame *g, sGameState *s) { memcpy(&g->st, s, sizeof(*s)); }
static inline // Is it my turn?
int game_isit_myturn (sGame *g) { return g->pme==g->st.pcurr; }
static inline // Am I host?
int game_ami_host (sGame *g) { return g->pme==g->conf.phost; }
static inline
LIST * game_histo_destroylist (LIST *l) {
return list_destroy_full(l, (free_handler)free);
}
static inline
void game_destroy (sGame *g) {
g->turns=list_destroy_full(g->turns, (free_handler)free); // free the turns stack
g->st.state=GS_INIT;
}
#endif