The server handles all of the game logic. It maintains all game states and sends messages to one or more clients. The server's main functionality is to maintain the Nuggets game's functionality and operation.
The server, implemented in server.c, defines player_t and game_t structures and exports the following functions:
int main(int argc, char* argv[]);
static bool parseArgs(int argc, char* argv[], char** mapFilename, int* seed);
static game_t* initializeGame(const char* mapFile);
static bool handleMessage(void* arg, const addr_t from, const char* message);
static void changeCord(grid_t* inputGrid, player_t* p, int changeX, int changeY);
static void broadcastDisplay();
static void mergeMaps(grid_t* currentMap, grid_t* newMap);
static void gameOver(void);The main function calls parseArgs, initializes necessary modules (initializeGame, message), and loops for messages from the client.
The parseArgs function checks for the correct number of command-line arguments and ensures they are valid.
The initializeGame function creates the game's grid from the given map file and randomly places gold piles throughout the grid.
The handleMessage function receives messages from the client and handles them according to the specs.
The changeCord function handles the actual movement of players and handles situations such as collisions and gold collection.
The broadcastDisplay function sends the display to the client for players and spectators to see.
The mergeMaps function combines a player's visible grid so previously explored map areas are still visible after the player moves away.
The gameOver function handles the end of the Nuggets game and sends messages as per the specs.
Makefile- compilation procedureserver.c- the implementation
To compile, simply make.
Test with the miniclient in the support library library.