Added comments for functions.
This commit is contained in:
parent
f5edadebb6
commit
7aa822d623
110
client.c
110
client.c
@ -8,31 +8,77 @@
|
|||||||
|
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @details Constant which holds the port number to connect to.
|
||||||
|
*/
|
||||||
static const char *port = DEFAULT_PORT;
|
static const char *port = DEFAULT_PORT;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @details Struct which holds info for the socket.
|
||||||
|
*/
|
||||||
static struct addrinfo *ai = NULL;
|
static struct addrinfo *ai = NULL;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @details Global variable for the hostname to connect to.
|
||||||
|
*/
|
||||||
static char *hostname = NULL;
|
static char *hostname = NULL;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @details Global variable for the socket file descriptor (easy cleanup).
|
||||||
|
*/
|
||||||
static int sockfd = -1;
|
static int sockfd = -1;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @details Global variable for the program name for error messages.
|
||||||
|
*/
|
||||||
static char* pname;
|
static char* pname;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @details Global variable for tracking where we have already fired a shot.
|
||||||
|
*/
|
||||||
static int field[MAP_SIZE][MAP_SIZE];
|
static int field[MAP_SIZE][MAP_SIZE];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @details Global variable for the x coordinate.
|
||||||
|
*/
|
||||||
static uint8_t x;
|
static uint8_t x;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @details Global variable for the y coordinate.
|
||||||
|
*/
|
||||||
static uint8_t y;
|
static uint8_t y;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @details Variable which get set on signal received.
|
||||||
|
*/
|
||||||
volatile sig_atomic_t quit = 0;
|
volatile sig_atomic_t quit = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @details Signal handler.
|
||||||
|
* @param signal The number of the received signal.
|
||||||
|
* @return Function has no return value.
|
||||||
|
*/
|
||||||
static void handle_signal(int signal)
|
static void handle_signal(int signal)
|
||||||
{
|
{
|
||||||
quit = 1;
|
quit = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @details Prints the program usage.
|
||||||
|
* @param void Function takes no arguments.
|
||||||
|
* @return Always non-zero.
|
||||||
|
*/
|
||||||
static void usage(void)
|
static void usage(void)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "[%s] Usage:\n\tSYNOPSIS\n\t\t [-h HOSTNAME] [-p PORT]\n\tEXAMPLE\n\tclient -h localhost -p 1280\n", pname);
|
fprintf(stderr, "[%s] Usage:\n\tSYNOPSIS\n\t\t [-h HOSTNAME] [-p PORT]\n\tEXAMPLE\n\tclient -h localhost -p 1280\n", pname);
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @details Sets even parity for the supplied message.
|
||||||
|
* @param msg The encoded coordinates in 2 bytes.
|
||||||
|
* @return Returns the message with even parity set.
|
||||||
|
*/
|
||||||
static uint16_t setParity(uint16_t msg)
|
static uint16_t setParity(uint16_t msg)
|
||||||
{
|
{
|
||||||
uint16_t v = msg;
|
uint16_t v = msg;
|
||||||
@ -50,6 +96,12 @@ static uint16_t setParity(uint16_t msg)
|
|||||||
return msg;
|
return msg;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @details Parses the commandline arguments.
|
||||||
|
* @param argc Count of commandline arguments.
|
||||||
|
* @param argv Array of commandline arguments.
|
||||||
|
* @return Function has no return value.
|
||||||
|
*/
|
||||||
static void parse(int argc, char *argv[])
|
static void parse(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
if (argc == 1) {
|
if (argc == 1) {
|
||||||
@ -74,16 +126,28 @@ static void parse(int argc, char *argv[])
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void cleanup(void)
|
/**
|
||||||
|
* @details Function for convenient cleanup of resources.
|
||||||
|
* @param void Function takes no arguments.
|
||||||
|
* @return 0 on success. Non-zero on failure.
|
||||||
|
*/
|
||||||
|
static int cleanup(void)
|
||||||
{
|
{
|
||||||
if (close(sockfd) < 1) {
|
if (close(sockfd) < 0) {
|
||||||
perror(pname);
|
perror(pname);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
free(ai);
|
free(ai);
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @details Calls target mode by testing adjacent fields.
|
||||||
|
* @param void Function takes no arguments.
|
||||||
|
* @return Function has no return value.
|
||||||
|
*/
|
||||||
static void markSunk(void)
|
static void markSunk(void)
|
||||||
{
|
{
|
||||||
// check north
|
// check north
|
||||||
@ -119,6 +183,11 @@ static void markSunk(void)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @details Function checks the north tile and sets x and y if not already hit.
|
||||||
|
* @param void Function takes no arguments.
|
||||||
|
* @return 1 if field north not hit. 0 otherwise.
|
||||||
|
*/
|
||||||
static int checkNorth(void)
|
static int checkNorth(void)
|
||||||
{
|
{
|
||||||
int j = y - 1;
|
int j = y - 1;
|
||||||
@ -135,6 +204,11 @@ static int checkNorth(void)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @details Function checks the south tile and sets x and y if not already hit.
|
||||||
|
* @param void Function takes no arguments.
|
||||||
|
* @return 1 if field north not hit. 0 otherwise.
|
||||||
|
*/
|
||||||
static int checkSouth(void)
|
static int checkSouth(void)
|
||||||
{
|
{
|
||||||
int j = y + 1;
|
int j = y + 1;
|
||||||
@ -151,6 +225,11 @@ static int checkSouth(void)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @details Function checks the eastern tile and sets x and y if not already hit.
|
||||||
|
* @param void Function takes no arguments.
|
||||||
|
* @return 1 if field north not hit. 0 otherwise.
|
||||||
|
*/
|
||||||
static int checkEast(void)
|
static int checkEast(void)
|
||||||
{
|
{
|
||||||
int i = x - 1;
|
int i = x - 1;
|
||||||
@ -167,6 +246,11 @@ static int checkEast(void)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @details Function checks the western tile and sets x and y if not already hit.
|
||||||
|
* @param void Function takes no arguments.
|
||||||
|
* @return 1 if field north not hit. 0 otherwise.
|
||||||
|
*/
|
||||||
static int checkWest(void)
|
static int checkWest(void)
|
||||||
{
|
{
|
||||||
int i = x + 1;
|
int i = x + 1;
|
||||||
@ -183,6 +267,12 @@ static int checkWest(void)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @details Searches for a tile not already hit and calls adjacent hit functions
|
||||||
|
* when a ship has been hit but not sunk.
|
||||||
|
* @param void Functions takes no arguments.
|
||||||
|
* @return Function has no return value.
|
||||||
|
*/
|
||||||
static void searchHit(void)
|
static void searchHit(void)
|
||||||
{
|
{
|
||||||
for (int j = 0; j < MAP_SIZE; j++) {
|
for (int j = 0; j < MAP_SIZE; j++) {
|
||||||
@ -215,6 +305,13 @@ static void searchHit(void)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @details Reads x and y and contructs the encoded message in 2 bytes. Then
|
||||||
|
* sends the bytes on by one to the server and waits for response. Marks
|
||||||
|
* the tile hit accordingly.
|
||||||
|
* @param void Function takes no arguments.
|
||||||
|
* @return Function has no return value.
|
||||||
|
*/
|
||||||
static void makeHit(void)
|
static void makeHit(void)
|
||||||
{
|
{
|
||||||
uint8_t buffer[1];
|
uint8_t buffer[1];
|
||||||
@ -275,6 +372,14 @@ static void makeHit(void)
|
|||||||
markSunk();
|
markSunk();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The main entry point of the program.
|
||||||
|
* @deails Sets up signal handler. Parses the commandline arguments. Connects to
|
||||||
|
* the server and starts the game.
|
||||||
|
* @param argc Count of commandline arguments.
|
||||||
|
* @param argv Array of commandline arguments.
|
||||||
|
* @return 0 on success. Non-zero on failure.
|
||||||
|
*/
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
struct sigaction sa;
|
struct sigaction sa;
|
||||||
@ -315,7 +420,6 @@ int main(int argc, char *argv[])
|
|||||||
while(quit == 0) {
|
while(quit == 0) {
|
||||||
searchHit();
|
searchHit();
|
||||||
makeHit();
|
makeHit();
|
||||||
// printMap();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
cleanup();
|
cleanup();
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user