diff --git a/.gitignore b/.gitignore index d92f3a3..c8e101d 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ client *.o *.json *.sh +.vimrc diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..4d23a95 --- /dev/null +++ b/Makefile @@ -0,0 +1,19 @@ +CC = gcc +CFLAGS = -std=c99 -pedantic -Wall -D_DEFAULT_SOURCE -D_BSD_SOURCE -D_SVID_SOURCE -D_POSIX_C_SOURCE=200809L -g -c + +.PHONY all: server client + +server: server.o common.h + $(CC) -o $@ $^ + +server.o: server.c + $(CC) $(CFLAGS) $< + +client: client.o common.h + $(CC) -o $@ $^ + +client.o: client.c + $(CC) $(CFLAGS) $< + +.PHONY clean: + rm -f client.o server.o client server diff --git a/battleship_td.pdf b/battleship_td.pdf new file mode 100644 index 0000000..8b93143 Binary files /dev/null and b/battleship_td.pdf differ diff --git a/client.c b/client.c new file mode 100644 index 0000000..f269b4e --- /dev/null +++ b/client.c @@ -0,0 +1,356 @@ +/** + * @file client.c + * @author Tobias Eidelpes + * @date 2018-03-21 + * + * @brief Client for OSUE exercise 1B `Battleship'. + */ + +// IO, C standard library, POSIX API, data types: +#include +#include +#include +#include +#include +#include +#include + +// Assertions, errors, signals: +#include +#include +#include + +// Time: +#include + +// Sockets, TCP, ... : +#include +#include +#include +#include + +// stuff shared by client and server: +#include "common.h" + +// Static variables for things you might want to access from several functions: +static const char *port = DEFAULT_PORT; // the port to bind to + +// Static variables for resources that should be freed before exiting: +static struct addrinfo *ai = NULL; // addrinfo struct +static char *hostname = NULL; +static int sockfd = -1; // socket file descriptor +static char* pname; + +static int field[MAP_SIZE][MAP_SIZE]; + +static uint8_t x; +static uint8_t y; + +volatile sig_atomic_t quit = 0; + +static void handle_signal(int signal) +{ + quit = 1; +} + +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); + exit(EXIT_FAILURE); +} + +static uint16_t setParity(uint16_t msg) +{ + uint16_t v = msg; + v &= 0x7FF; + uint8_t parity = 0; + + while (v) { + parity = !parity; + v = v & (v - 1); + } + + if (parity) + msg += 0x8000; + + return msg; +} + +static void parse(int argc, char *argv[]) +{ + if (argc == 1) { + hostname = NULL; + port = "1280"; + } else { + int opt_index = 0; + while ((opt_index = getopt(argc, argv, "h:p:")) != -1) { + switch (opt_index) { + case 'h': + hostname = optarg; + break; + case 'p': + port = optarg; + break; + case '?': + usage(); + default: + assert(0); + } + } + } +} + +static void cleanup(void) +{ + close(sockfd); + free(ai); +} + +static void markSunk(void) +{ + // check north + for (int i = y; i >= 0; i--) { + if (field[x][i] == 2) + field[x][i] = 3; + else + break; + } + + // check south + for (int i = y; i < MAP_SIZE; i++) { + if (field[x][i] == 2) + field[x][i] = 3; + else + break; + } + + // check east + for (int i = x; i >= 0; i--) { + if (field[i][y] == 2) + field[i][y] = 3; + else + break; + } + + // check west + for (int i = x; i < MAP_SIZE; i++) { + if (field[i][y] == 2) + field[i][y] = 3; + else + break; + } +} + +static int checkNorth(void) +{ + int j = y - 1; + + for (; j >= 0; --j) { + if (field[x][j] == 0) { + y = j; + return 1; + } else { + return 0; + } + } + + return 0; +} + +static int checkSouth(void) +{ + int j = y + 1; + + for (; j < MAP_SIZE; ++j) { + if (field[x][j] == 0) { + y = j; + return 1; + } else { + return 0; + } + } + + return 0; +} + +static int checkEast(void) +{ + int i = x - 1; + + for (; i >= 0; --i) { + if (field[i][y] == 0) { + x = i; + return 1; + } else { + return 0; + } + } + + return 0; +} + +static int checkWest(void) +{ + int i = x + 1; + + for (; i < MAP_SIZE; ++i) { + if (field[i][y] == 0) { + x = i; + return 1; + } else { + return 0; + } + } + + return 0; +} + +static void searchHit(void) +{ + for (int j = 0; j < MAP_SIZE; j++) { + for (int i = 0; i < MAP_SIZE; i++) { + if (field[i][j] == 2) { + x = i; + y = j; + if (checkNorth()) { + return; + } + if (checkSouth()) { + return; + } + if (checkEast()) { + return; + } + if (checkWest()) { + return; + } + } + } + } + + x = rand() % 10; + y = rand() % 10; + + while(field[x][y] != 0) { + x = rand() % 10; + y = rand() % 10; + } +} + +static void makeHit(void) +{ + uint8_t buffer[1]; + uint8_t status; + uint8_t hit; + uint16_t msg; + + msg = x; + msg <<= 6; + msg += y; + msg = setParity(msg); + uint16_t v = msg; + v &= 0x00FF; + buffer[0] = v; + send(sockfd, buffer, 1, 0); + + v = msg; + v &= 0xFF00; + v >>= 8; + buffer[0] = v; + send(sockfd, buffer, 1, 0); + + recv(sockfd, buffer, 1, 0); + + status = buffer[0]; + status &= 0x0C; + hit = buffer[0]; + hit &= 0x03; + + switch (status) { + case 2: + fprintf(stderr, "[%s] Parity error\n", pname); + cleanup(); + exit(2); + case 3: + fprintf(stderr, "[%s] Invalid coordinate\n", pname); + cleanup(); + exit(3); + default: + break; + } + + if (status == 1 && (hit == 0 || hit == 1 || hit == 2)) { + fprintf(stdout, "[%s] Game lost\n", pname); + cleanup(); + exit(EXIT_SUCCESS); + } else if (status == 1 && hit == 3) { + fprintf(stdout, "[%s] I win :)\n", pname); + cleanup(); + exit(EXIT_SUCCESS); + } + + if (hit == 0) + field[x][y] = 1; + if (hit == 1) + field[x][y] = 2; + if (hit == 2) + markSunk(); +} + +static void printMap(void) +{ + for (int y = 0; y < MAP_SIZE; y++) { + for (int x = 0; x < MAP_SIZE; x++) { + fprintf(stdout, "%d\t", field[x][y]); + } + fprintf(stdout, "\n"); + } + fprintf(stdout, "\n"); +} + +int main(int argc, char *argv[]) +{ + struct sigaction sa; + memset(&sa, 0, sizeof(sa)); + sa.sa_handler = handle_signal; + sigaction(SIGINT, &sa, NULL); + sigaction(SIGTERM, &sa, NULL); + + pname = argv[0]; + + parse(argc, argv); + + struct addrinfo hints; + memset(&hints, 0, sizeof(hints)); + hints.ai_family = AF_INET; + hints.ai_socktype = SOCK_STREAM; + hints.ai_flags = AI_PASSIVE; + + int res = getaddrinfo(hostname, port, &hints, &ai); + if (res < 0) { + fprintf(stderr, "[%s] ERROR: Failed to get address info: %s\n", pname, gai_strerror(res)); + exit(EXIT_FAILURE); + } + + sockfd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); + if (sockfd < 0) { + perror(pname); + exit(EXIT_FAILURE); + } + if (connect(sockfd, ai->ai_addr, ai->ai_addrlen) < 0) { + close(sockfd); + perror(pname); + exit(EXIT_FAILURE); + } + + srand(time(NULL)); + + while(quit == 0) { + searchHit(); + makeHit(); + // printMap(); + } + + cleanup(); + exit(EXIT_SUCCESS); +} diff --git a/common.h b/common.h new file mode 100644 index 0000000..6ff9fd1 --- /dev/null +++ b/common.h @@ -0,0 +1,79 @@ +/** + * @file common.h + * @author OSUE Team + * @date 2017-10-06 + * + * @brief Common definitions for OSUE exercise 1B `Battleship'. + */ + +// guard block: +#ifndef COMMON_H +#define COMMON_H + +// default hostname and port: +#define DEFAULT_HOST "localhost" +#define DEFAULT_PORT "1280" + +// Length of each side of the map: +#define MAP_SIZE 10 + +// Minimum and maximum length of the ships: +#define MIN_SHIP_LEN 2 +#define MAX_SHIP_LEN 4 + +// Number of ships of each length: +#define SHIP_CNT_LEN2 2 // 2 ships of length 2 +#define SHIP_CNT_LEN3 3 // 3 ships of length 3 +#define SHIP_CNT_LEN4 1 // 1 ship of length 4 + +// Maximum number of rounds after which the client loses the game: +#define MAX_ROUNDS 80 + +// Suggested values to save information about the squares of the map: +#define SQUARE_UNKNOWN 0 // the square has not been targeted yet +#define SQUARE_HIT 1 // a shot at the square hit a ship +#define SQUARE_EMPTY 2 // a shot at the square was a miss (thus it is empty) + +#define MIN(x,y) (x < y) ? x : y +#define MAX(x,y) (x > y) ? x : y + +#include +#include + +/** + * @brief Print a map showing the squares where ships have been hit. + * + * You might find this function useful for debugging. + * + * @param map A 2-dimensional array of unsigned 8-bit integers, where each + * element represents a square and its value indicates whether a + * shot has already been directed at this square and if a ship was + * hit by that shot, according to the values suggested above. + * + * Example usage: + * @code + * uint8_t map[MAP_SIZE][MAP_SIZE]; + * memset(&map, 0, sizeof(map)); // initialize each square as unknown + * map[1][5] = SQUARE_EMPTY; // a shot at B5 did not hit anything + * map[2][3] = SQUARE_HIT; // a shot at C3 hit a ship + * print_map(map); + * @endcode + */ +static inline void print_map(uint8_t map[MAP_SIZE][MAP_SIZE]) +{ + int x, y; + + printf(" "); + for (x = 0; x < MAP_SIZE; x++) + printf("%c ", 'A' + x); + printf("\n"); + + for (y = 0; y < MAP_SIZE; y++) { + printf("%c ", '0' + y); + for (x = 0; x < MAP_SIZE; x++) + printf("%c ", map[x][y] ? ((map[x][y] == 1) ? 'x' : 'o') : ' '); + printf("\n"); + } +} + +#endif // COMMON_H diff --git a/server.c b/server.c new file mode 100644 index 0000000..4b263ef --- /dev/null +++ b/server.c @@ -0,0 +1,385 @@ +/** + * @file server.c + * @author Tobias Eidelpes + * @date 2018-03-02 + * + * @brief Server for OSUE exercise 1B `Battleship'. + */ + +// IO, C standard library, POSIX API, data types: +#include +#include +#include +#include +#include +#include +#include + +// Assertions, errors, signals: +#include +#include +#include + +// Time: +#include + +// Sockets, TCP, ... : +#include +#include +#include +#include + +// stuff shared by client and server: +#include "common.h" + +// Static variables for things you might want to access from several functions: +static const char *port = DEFAULT_PORT; // the port to bind to + +// Static variables for resources that should be freed before exiting: +static struct addrinfo *ai = NULL; // stores address information +static int sockfd = -1; // socket file descriptor +static int connfd = -1; // connection file descriptor +static char* pname; + +/* TODO + * You might want to add further static variables here, for instance to save + * the program name (argv[0]) since you should include it in all messages. + * + * You should also have some kind of a list which saves information about your + * ships. For this purpose you might want to define a struct. Bear in mind that + * your server must keep record about which ships have been hit (and also where + * they have been hit) in order to know when a ship was sunk. + * + * You might also find it convenient to add a few functions, for instance: + * - a function which cleans up all resources and exits the program + * - a function which parses the arguments from the command line + * - a function which adds a new ship to your list of ships + * - a function which checks whether a client's shot hit any of your ships + */ + +struct Ship { + int shipsize; + int hitcount; + int isDestroyed; +}; + +struct Tile { + int isHit; + struct Ship *here; +}; + +static struct Tile field[MAP_SIZE][MAP_SIZE]; +static struct Ship *ships[6]; + +volatile sig_atomic_t quit = 0; + +/** + * @details Signal handler. + * @param signal The number of the received signal. + * @return This function has no return value. + */ +static void handle_signal(int signal) +{ + quit = 1; +} + +static void usage(void) +{ + fprintf(stderr, "[%s] Usage:\n\tSYNOPSIS\n\t\tserver [-p PORT] SHIP1...\n\tEXAMPLE\n\tserver -p 1280 C2E2 F0H0 B6A6 E8E6 I2I5 H8I8\n", pname); + exit(EXIT_FAILURE); +} + +static void cleanup(void) +{ + for (int i = 0; i < (int) sizeof(ships); i++) { + free(ships[i]); + } + + close(sockfd); + free(ai); +} + +static struct Ship * addShip(char *coords) +{ + if (strlen(coords) != 4) { + fprintf(stderr, "[%s] ERROR: wrong syntax for ship coordinates: %s\n", pname, coords); + cleanup(); + exit(EXIT_FAILURE); + } + + int x1 = coords[0] - 65; + int x2 = coords[1] - 48; + int y1 = coords[2] - 65; + int y2 = coords[3] - 48; + if ((x1 < 0 || x1 > 9) || (x2 < 0 || x2 > 9) + || (y1 < 0 || y1 > 9) || (y2 < 0 || y2 > 9)) { + fprintf(stderr, "[%s] ERROR: coordinates outside of map: %s.\n", pname, coords); + cleanup(); + exit(EXIT_FAILURE); + } + + int shipsize = 0; + if (x1 == y1) { + shipsize = (MAX(x2, y2)) - (MIN(x2, y2)) + 1; + } else if (x2 == y2) { + shipsize = (MAX(x1, y1)) - (MIN(x1, y1)) + 1; + } else { + fprintf(stderr, "[%s] ERROR: ships must be aligned either horizontally or vertically: %s\n", pname, coords); + cleanup(); + exit(EXIT_FAILURE); + } + + struct Ship *newShip = malloc(sizeof(struct Ship)); + newShip->shipsize = shipsize; + newShip->hitcount = 0; + newShip->isDestroyed = 0; + struct Tile newShipTile = {0, newShip}; + + for (int x = (MIN(x1, y1)); x <= (MAX(x1, y1)); x++) { + for (int y = (MIN(x2, y2)); y <= (MAX(x2, y2)); y++) { + field[x][y] = newShipTile; + } + } + + return newShip; +} + +static void parse(int argc, char *argv[]) +{ + if (argc == 1) + usage(); + + int opt_ind = 0; + int pflag = 0; + + while ((opt_ind = getopt(argc, argv, "p:")) != -1) { + switch (opt_ind) { + case 'p': + if (pflag != 0) { + usage(); + } else { + port = optarg; + pflag = 1; + } + break; + + case '?': + usage(); + + default: + assert(0); + } + } + + if ((argc - optind) != 6) + usage(); + + for (int i = optind, c = 0; i < argc; i++, c++) { + ships[c] = addShip(argv[i]); + } +} + +static int checkWin(void) +{ + int counter = 0; + + for (int i = 0; i < (int) (sizeof(ships) / sizeof(ships[0])); i++) { + if (ships[i]->isDestroyed) + counter++; + } + + if (counter == (int) (sizeof(ships) / sizeof(ships[0]))) + return 1; // all ships sunk + + return 0; +} + +static int makeHit(uint16_t msg) +{ + uint8_t y = msg & 0x003F; + uint8_t x = msg >> 6; + x &= 0x003F; + + if (field[x][y].isHit) { + if (field[x][y].here == NULL) + return 0; // no ship at position + if (field[x][y].here->isDestroyed == 1) + return 0; // ship already destroyed + return 1; // tile with ship hit again + } + + field[x][y].isHit = 1; + + if (field[x][y].here == NULL) { + return 0; // missed shot + } + + field[x][y].here->hitcount += 1; + + if (field[x][y].here->hitcount == field[x][y].here->shipsize) { + field[x][y].here->isDestroyed = 1; + if (checkWin()) // return == 1 (game won) + return 3; + return 2; // checkWin() == 0 (ship destroyed but still running) + } + + return 1; // ship hit but not destroyed +} + +static int checkParity(uint16_t msg) +{ + uint16_t v = msg; + v &= 0x7FF; + uint8_t parity = 0; + + while (v) { + parity = !parity; + v = v & (v - 1); + } + uint16_t msb = msg; + msb &= 0x8000; + msb >>= 15; + if (parity == msb) { + return 0; + } else { + return 1; + } +} + +static int checkCoords(uint16_t msg) +{ + uint8_t y = msg & 0x003F; + uint8_t x = msg >> 6; + x &= 0x003F; + + if ((x > 9) || (y > 9)) + return 1; + + return 0; +} + +static void printMap(void) +{ + for (int y = 0; y < MAP_SIZE; y++) { + for (int x = 0; x < MAP_SIZE; x++) { + fprintf(stdout, "%d\t", field[x][y].isHit); + } + fprintf(stdout, "\n"); + } +} + +int main(int argc, char *argv[]) +{ + struct sigaction sa; + memset(&sa, 0, sizeof(sa)); + sa.sa_handler = handle_signal; + sigaction(SIGINT, &sa, NULL); + sigaction(SIGTERM, &sa, NULL); + + pname = argv[0]; + + parse(argc, argv); + + struct addrinfo hints; + memset(&hints, 0, sizeof(hints)); + hints.ai_family = AF_INET; + hints.ai_socktype = SOCK_STREAM; + hints.ai_flags = AI_PASSIVE; + + int res = getaddrinfo(NULL, port, &hints, &ai); + if (res < 0) { + fprintf(stderr, "[%s] ERROR: Failed to get address info: %s\n", pname, gai_strerror(res)); + cleanup(); + exit(EXIT_FAILURE); + } + + sockfd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); + if (sockfd < 0) { + perror(pname); + cleanup(); + exit(EXIT_FAILURE); + } + + int val = 1; + res = setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)); + if (res < 0) { + cleanup(); + perror(pname); + exit(EXIT_FAILURE); + } + + res = bind(sockfd, ai->ai_addr, ai->ai_addrlen); + if (res < 0) { + cleanup(); + perror(pname); + exit(EXIT_FAILURE); + } + + res = listen(sockfd, 1); + if (res < 0) { + cleanup(); + perror(pname); + exit(EXIT_FAILURE); + } + + connfd = accept(sockfd, NULL, NULL); + if (connfd < 0) { + cleanup(); + perror(pname); + exit(EXIT_SUCCESS); + } + + int round = 0; + + while (quit == 0) { + uint8_t buffer[1]; + if (round == 80) { + fprintf(stdout, "[%s] Game lost\n", pname); + buffer[0] = 0x04; + send(connfd, buffer, 1, 0); + cleanup(); + exit(EXIT_SUCCESS); + } + round++; + + recv(connfd, buffer, 1, 0); + uint16_t msg = buffer[0]; + recv(connfd, buffer, 1, 0); + msg += 256U*buffer[0]; + if (checkParity(msg) == 1) { + fprintf(stderr, "[%s] Parity error\n", pname); + buffer[0] = 0x08; + send(connfd, buffer, 1, 0); + cleanup(); + exit(2); + } + if (checkCoords(msg) == 1) { + fprintf(stderr, "[%s] Invalid coordinate\n", pname); + buffer[0] = 0x0C; + send(connfd, buffer, 1, 0); + cleanup(); + exit(3); + } + + int hit = makeHit(msg); + if (hit == 0) { + buffer[0] = 0x00; + send(connfd, buffer, 1, 0); + } else if (hit == 1) { + buffer[0] = 0x01; + send(connfd, buffer, 1, 0); + } else if (hit == 2) { + buffer[0] = 0x02; + send(connfd, buffer, 1, 0); + } else { + fprintf(stdout, "[%s] Client wins in %d rounds\n", pname, round); + buffer[0] = 0x07; + send(connfd, buffer, 1, 0); + cleanup(); + exit(EXIT_SUCCESS); + } + } + + cleanup(); + exit(EXIT_SUCCESS); +}