-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGameState.h
52 lines (47 loc) · 1.87 KB
/
GameState.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
#ifndef __GAME_STATE
#define __GAME_STATE
/*
Data structure containing the variable necessary to retain game state
*/
struct sessionData { // Store the game state
int sock; // The client socket that is connected
char* word; // Store the current full word
char partWord[LINESIZE]; // Store the current part word
int lives; // Store the number of guesses left for each client
int gameState; // Store the game state for the connection
char bufIn[LINESIZE]; // Input Buffer
char bufOut[LINESIZE]; // Output Buffer
char ip[INET_ADDRSTRLEN]; // client IP
int port; // client port
};
/*
Reset or clear sessionData variables when a Client has finished playing
*/
void closeSocketConnection(struct sessionData* cli) {
(*cli).sock = -1; // Clear client from client index
(*cli).word = ""; // Whole word for each client
(*cli).partWord[0] = '\0'; // Part word for each client
(*cli).lives = MAX_LIVES; // Lives for each client
(*cli).gameState = 'I'; // Game state for each client
(*cli).bufIn[0] = '\0'; // Clear client from client index
}
/*
Display the current game state
*/
void displayState(int i, char* word, char* part, int lives, char state) {
printf("\nState %d Word: %s partWord: %s ", // Display Clients:
i,word,part); // Position in Clients array, word to guess, part word
printf("lives %d gameState %c\n\n",
lives,state); // Number of guesses/lives left, current game state
}
/*
Initialise the game state for a client
*/
void initClient(struct sessionData* cli) {
// printf("Init the game state\n");
(*cli).word = selectRandomWord((*cli).ip, (*cli).port); // Whole word for each client
initPartWord((*cli).partWord, strlen((*cli).word)); // Part word for each client
(*cli).lives = MAX_LIVES; // Lives for each client
(*cli).gameState = 'I'; // Game state for each client
}
#endif /* __GAME_STATE */