From 610de68430cd101e917d8870e289ff770f455dbc Mon Sep 17 00:00:00 2001 From: WaffleMaker9000 Date: Sun, 12 Jan 2025 04:47:45 +0100 Subject: [PATCH 1/5] Refactoring on the wrong branch --- client/client.c | 2 ++ server/bot_board.c | 10 ++++++++++ source/board.c | 31 +++++++++++++++++++++---------- 3 files changed, 33 insertions(+), 10 deletions(-) create mode 100644 server/bot_board.c diff --git a/client/client.c b/client/client.c index 068efff..6720d47 100644 --- a/client/client.c +++ b/client/client.c @@ -45,6 +45,8 @@ int initialize_client(int port) return sock; } +void play_game_ + void play_game(int sock) { board b_own; diff --git a/server/bot_board.c b/server/bot_board.c new file mode 100644 index 0000000..c2a533c --- /dev/null +++ b/server/bot_board.c @@ -0,0 +1,10 @@ +#include "board.h" + +void generate_board(board* b) +{ + int placed = 0; + while(placed < 5) + { + + } +} \ No newline at end of file diff --git a/source/board.c b/source/board.c index 50990be..e1beb2f 100644 --- a/source/board.c +++ b/source/board.c @@ -53,6 +53,16 @@ void board_display(board* b_own, board* b_enemy) } } +void parse_input(char* input, int* x, int* y, bool* down) +{ + *x = input[0] - 'A'; + *y = input[1] - '0'; + if (down != NULL) + { + *down = input[2] == DOWN; + } +} + bool validate_coords(char* coords) { return((coords[0] >= 'A') && (coords[0] <= 'J') && (coords[1] >= '0') && (coords[1] <= '9')); @@ -74,10 +84,10 @@ bool validate_tile(int x, int y, board* b) bool validate_position(char* position, int size, board* b) { - int x = position[0] - 'A'; - int y = position[1] - '0'; - bool down = true; - if (position[2] == 'r') down = false; + int x; + int y; + bool down; + parse_input(position, &x, &y, &down); if (down) { if (y + size > b->size_) return false; @@ -136,10 +146,10 @@ void get_ship(char* position, int size, board* b) void finalise_placement(char* position, int size, board* b) { - int x = position[0] - 'A'; - int y = position[1] - '0'; - bool down = true; - if (position[2] == 'r') down = false; + int x; + int y; + bool down; + parse_input(position, &x, &y, &down); if (down) { for (int i = y; i < y + size ; i++) @@ -273,8 +283,9 @@ void get_shot(char* shot, board* b_enemy) printf("Input not valid, try again\n"); continue; } - int x = shot[0] - 'A'; - int y = shot[1] - '0'; + int x; + int y; + parse_input(shot, &x, &y, NULL); if(b_enemy->board_[x][y] != NOT_HIT) { printf("You have already shot there, try again\n"); From b819a9dc924a471c3cd39744d9d8017f7e756df6 Mon Sep 17 00:00:00 2001 From: WaffleMaker9000 Date: Sun, 12 Jan 2025 04:56:46 +0100 Subject: [PATCH 2/5] Random generation of board --- server/bot_board.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/server/bot_board.c b/server/bot_board.c index c2a533c..cc25487 100644 --- a/server/bot_board.c +++ b/server/bot_board.c @@ -3,8 +3,21 @@ void generate_board(board* b) { int placed = 0; + int size = 5; + char* position = malloc(3 * sizeof(char)); while(placed < 5) { - + position[0] = 'A' + rand() % b->size_; + position[1] = '0' + rand() % b->size_; + position[2] = rand() % 2 ? 'd' : 'r'; + if (validate_position(position, size, b)) + { + finalise_placement(position, size, b); + placed++; + if (placed != 3) + { + size--; + } + } } } \ No newline at end of file From b797d7ec1e15ce9058f62580937fed0054a7c0b0 Mon Sep 17 00:00:00 2001 From: WaffleMaker9000 Date: Sun, 12 Jan 2025 17:30:52 +0100 Subject: [PATCH 3/5] Fully implemented computer game mode, created h files for client and server --- client/client.c | 57 ++++++++---------- client/client.h | 17 ++++++ server/CMakeLists.txt | 5 +- server/bot_board.c | 23 ++++++- server/bot_board.h | 5 ++ server/server.c | 136 +++++++++++++++++++++++++++++++----------- server/server.h | 36 +++++++++++ source/board.c | 43 ++++++------- source/board.h | 4 +- source/message.h | 2 + 10 files changed, 232 insertions(+), 96 deletions(-) create mode 100644 client/client.h create mode 100644 server/bot_board.h create mode 100644 server/server.h diff --git a/client/client.c b/client/client.c index 6720d47..84cd6ba 100644 --- a/client/client.c +++ b/client/client.c @@ -1,16 +1,4 @@ -#include -#include -#include -#include -#include -#include -#include -#include "../source/board.h" -#include "../source/menu.h" -#include "../source/utils.h" -#include "../source/message.h" - -#define SERVER_IP "127.0.0.1" // localhost +#include "client.h" int initialize_client(int port) { @@ -45,8 +33,6 @@ int initialize_client(int port) return sock; } -void play_game_ - void play_game(int sock) { board b_own; @@ -58,8 +44,6 @@ void play_game(int sock) // Initialize game boards board_init(&b_own, 10); board_init(&b_enemy, 10); - - // Place ships place_ships(&b_own); // Game loop @@ -120,11 +104,12 @@ void play_game(int sock) if (my_turn) { board_display(&b_own, &b_enemy); - char* shot = shoot(&b_enemy); - // TODO refactor? - int x = shot[0] - 'A'; - int y = shot[1] - '0'; - free(shot); // Add this to prevent memory leak + char* shot = calloc(3, sizeof(char)); + shoot(shot, &b_enemy); + int x; + int y; + parse_input(shot, &x, &y, NULL); + free(shot); // Send shot Message msg = { @@ -145,6 +130,9 @@ void play_game(int sock) int main(int argc, char **argv) { int mode; + int port = 8536; + if (argc >= 2) + port = atoi(argv[1]); // Handle menu mode = handle_menu(); @@ -153,24 +141,29 @@ int main(int argc, char **argv) clear_screen(); - if (mode == 1) // Computer game mode + if (mode == 1) // Computer singleplayer game mode { - show_message("Computer mode not yet implemented"); - sleep(3); - clear_screen(); - // TODO Waffle: implement + show_message("Connecting to computer opponent..."); + sleep(1); + int sock = initialize_client(port); + + Message mode_msg = {.type = MSG_SINGLE_PLAYER}; + send(sock, &mode_msg, sizeof(Message), 0); + + play_game(sock); + close(sock); return 0; } - int port = 8536; - if (argc >= 2) - port = atoi(argv[1]); - - if (mode == 2) // Human vs Human (network) mode + if (mode == 2) // Human vs Human multiplayer mode { show_message("Please wait, connecting..."); sleep(1); int sock = initialize_client(port); + + Message mode_msg = {.type = MSG_MULTI_PLAYER}; + send(sock, &mode_msg, sizeof(Message), 0); + play_game(sock); close(sock); } diff --git a/client/client.h b/client/client.h new file mode 100644 index 0000000..11d31a4 --- /dev/null +++ b/client/client.h @@ -0,0 +1,17 @@ +#include +#include +#include +#include +#include +#include +#include +#include "../source/board.h" +#include "../source/menu.h" +#include "../source/utils.h" +#include "../source/message.h" + +#define SERVER_IP "127.0.0.1" // localhost + +int initialize_client(int port); + +void play_game(int sock); \ No newline at end of file diff --git a/server/CMakeLists.txt b/server/CMakeLists.txt index b42b4be..c570c98 100644 --- a/server/CMakeLists.txt +++ b/server/CMakeLists.txt @@ -1,2 +1,5 @@ # Project: Server -add_executable(server server.c) \ No newline at end of file +add_executable(server server.c bot_board.c) +target_include_directories(server PRIVATE ../source) +target_link_libraries(server PRIVATE board) +target_link_libraries(server PRIVATE utils) \ No newline at end of file diff --git a/server/bot_board.c b/server/bot_board.c index cc25487..5d98ea4 100644 --- a/server/bot_board.c +++ b/server/bot_board.c @@ -1,10 +1,10 @@ -#include "board.h" +#include "bot_board.h" void generate_board(board* b) { int placed = 0; int size = 5; - char* position = malloc(3 * sizeof(char)); + char* position = calloc(3, sizeof(char)); while(placed < 5) { position[0] = 'A' + rand() % b->size_; @@ -20,4 +20,23 @@ void generate_board(board* b) } } } + free(position); +} + +void generate_shot(char* shot, board* b_enemy) +{ + while (true) + { + shot[0] = 'A' + rand() % b_enemy->size_; + shot[1] = '0' + rand() % b_enemy->size_; + shot[2] = '\0'; + int x; + int y; + parse_input(shot, &x, &y, NULL); + if (b_enemy->board_[x][y] != NOT_HIT) + { + continue; + } + break; + } } \ No newline at end of file diff --git a/server/bot_board.h b/server/bot_board.h new file mode 100644 index 0000000..098b0ce --- /dev/null +++ b/server/bot_board.h @@ -0,0 +1,5 @@ +#include "../source/board.h" + +void generate_board(board* b); + +void generate_shot(char* shot, board* b_enemy); \ No newline at end of file diff --git a/server/server.c b/server/server.c index d59fad9..0d7d9a0 100644 --- a/server/server.c +++ b/server/server.c @@ -1,24 +1,7 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "../source/message.h" - -#define MAX_PLAYERS 2 +#include "server.h" atomic_int player_count = 0; // Atomic variable to track connected players -typedef struct { - int socket; - int opponent_socket; - bool has_opponent; -} Player; - Player players[MAX_PLAYERS]; pthread_mutex_t players_mutex = PTHREAD_MUTEX_INITIALIZER; @@ -79,7 +62,13 @@ void *handle_client(void *arg) //Dereference the pointer to get the socket int client_socket = *(int *)arg; - // Register player + // Receive game mode from client + Message mode_msg; + if (recv(client_socket, &mode_msg, sizeof(Message), 0) <= 0) { + close(client_socket); + return NULL; + } + pthread_mutex_lock(&players_mutex); int player_slot = find_empty_slot(); if(player_slot == -1) { @@ -90,19 +79,39 @@ void *handle_client(void *arg) players[player_slot].socket = client_socket; players[player_slot].has_opponent = false; - - //Try to find opponent, assign opponent socket and set has_opponent to true - for(int i = 0; i < MAX_PLAYERS; i++) { - if(i != player_slot && players[i].socket != 0 && !players[i].has_opponent) { - players[player_slot].opponent_socket = players[i].socket; + players[player_slot].player_type = PLAYER_TYPE_HUMAN; + + if (mode_msg.type == MSG_SINGLE_PLAYER) { + // Create bot player + int bot_slot = find_empty_slot(); + if(bot_slot != -1) { + players[bot_slot].socket = -1; + players[bot_slot].player_type = PLAYER_TYPE_BOT; + players[bot_slot].bot_state = malloc(sizeof(BotState)); + board_init(&players[bot_slot].bot_state->b_own, 10); + board_init(&players[bot_slot].bot_state->b_enemy, 10); + generate_board(&players[bot_slot].bot_state->b_own); + + // Connect bot and human player + players[player_slot].opponent_socket = -1; players[player_slot].has_opponent = true; - players[i].opponent_socket = client_socket; - players[i].has_opponent = true; - break; + players[bot_slot].opponent_socket = client_socket; + players[bot_slot].has_opponent = true; + } + } else if (mode_msg.type == MSG_MULTI_PLAYER) { + //Try to find opponent, assign opponent socket and set has_opponent to true + for(int i = 0; i < MAX_PLAYERS; i++) { + if(i != player_slot && players[i].socket != 0 && !players[i].has_opponent) { + players[player_slot].opponent_socket = players[i].socket; + players[player_slot].has_opponent = true; + players[i].opponent_socket = client_socket; + players[i].has_opponent = true; + break; + } } } pthread_mutex_unlock(&players_mutex); - + // Wait for opponent while(!players[player_slot].has_opponent) { Message msg = {.type = MSG_WAIT_PLAYER}; @@ -121,10 +130,10 @@ void *handle_client(void *arg) Message start_msg = {.type = MSG_START_GAME}; send(client_socket, &start_msg, sizeof(Message), 0); - // Wait a moment to ensure both players have received START_GAME + // Wait to ensure both players have received START_GAME usleep(100000); // 100ms - // First player gets first turn, but only after both are ready + // First player gets first turn after both are ready if(player_slot == 0) { Message turn_msg = {.type = MSG_YOUR_TURN}; send(client_socket, &turn_msg, sizeof(Message), 0); @@ -146,23 +155,72 @@ void *handle_client(void *arg) switch(msg.type) { case MSG_SHOT: - Message wait_msg = {.type = MSG_WAIT_PLAYER}; - send(client_socket, &wait_msg, sizeof(Message), 0); + if (players[find_player_slot(opponent_socket)].player_type == PLAYER_TYPE_BOT) + { + // Handle bot response + int bot_slot = find_player_slot(opponent_socket); + BotState* bot_state = players[bot_slot].bot_state; + + // Process player's shot + int hit = receive_shot(msg.x, msg.y, &bot_state->b_own); + + // Send result back to player + Message result = { + .type = MSG_RESULT, + .x = msg.x, + .y = msg.y, + .hit = hit + }; + send(client_socket, &result, sizeof(Message), 0); - // Forward shot to opponent - send(opponent_socket, &msg, sizeof(Message), 0); + if (hit == -1) + { + Message game_over = {.type = MSG_GAME_OVER}; + send(client_socket, &game_over, sizeof(Message), 0); + break; + } + char shot[3]; + generate_shot(shot, &bot_state->b_enemy); + int x = shot[0] - 'A'; + int y = shot[1] - '0'; + + Message bot_shot = { + .type = MSG_SHOT, + .x = x, + .y = y + }; + send(client_socket, &bot_shot, sizeof(Message), 0); + } + else + { + Message wait_msg = {.type = MSG_WAIT_PLAYER}; + send(client_socket, &wait_msg, sizeof(Message), 0); + send(opponent_socket, &msg, sizeof(Message), 0); + } break; case MSG_RESULT: // Forward result to shooter - send(opponent_socket, &msg, sizeof(Message), 0); + if (players[find_player_slot(opponent_socket)].player_type == PLAYER_TYPE_BOT) + { + int bot_slot = find_player_slot(opponent_socket); + BotState* bot_state = players[bot_slot].bot_state; + mark_hit(msg.x, msg.y, msg.hit, &bot_state->b_enemy); + } + else + { + send(opponent_socket, &msg, sizeof(Message), 0); + } // Send turn message to the player who just got shot at Message turn_msg = {.type = MSG_YOUR_TURN}; send(client_socket, &turn_msg, sizeof(Message), 0); break; case MSG_GAME_OVER: - send(opponent_socket, &msg, sizeof(Message), 0); + if (players[find_player_slot(opponent_socket)].player_type != PLAYER_TYPE_BOT) + { + send(opponent_socket, &msg, sizeof(Message), 0); + } break; } } @@ -185,6 +243,12 @@ void *handle_client(void *arg) // destroy opponent's data for(int i = 0; i < MAX_PLAYERS; i++) { if(players[i].socket == opponent_socket) { + if (players[i].player_type == PLAYER_TYPE_BOT && players[i].bot_state != NULL) { + board_destroy(&players[i].bot_state->b_own); + board_destroy(&players[i].bot_state->b_enemy); + free(players[i].bot_state); + players[i].bot_state = NULL; + } players[i].opponent_socket = 0; players[i].has_opponent = false; break; diff --git a/server/server.h b/server/server.h new file mode 100644 index 0000000..7f27161 --- /dev/null +++ b/server/server.h @@ -0,0 +1,36 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "../source/message.h" +#include "bot_board.h" + +#define MAX_PLAYERS 2 +#define PLAYER_TYPE_HUMAN 0 +#define PLAYER_TYPE_BOT 1 + +typedef struct { + board b_own; + board b_enemy; +} BotState; + +typedef struct { + int socket; + int opponent_socket; + bool has_opponent; + int player_type; + BotState* bot_state; +} Player; + +int find_player_slot(int socket); + +int find_empty_slot(); + +int initialize_server(int port); + +void* handle_client(void* arg); \ No newline at end of file diff --git a/source/board.c b/source/board.c index e1beb2f..5a7de0b 100644 --- a/source/board.c +++ b/source/board.c @@ -207,43 +207,42 @@ bool check_destroyed(int x, int y, board* b) { int checking; if (y > 0) { - checking = y - 1; - do + checking = y; + while ((checking >= 0) && (b->board_[x][checking] == HIT_SHIP)) { - if (b->board_[x][checking] == SHIP) return false; - checking -= 1; - } while ((checking >= 0) && (b->board_[x][checking] == HIT_SHIP)); + if (b->board_[checking][y] == SHIP) return false; + checking--; + } } if (y < b->size_ - 1) { - checking = y + 1; - do + checking = y; + while ((checking < b->size_) && (b->board_[x][checking] == HIT_SHIP)) { - if (b->board_[x][checking] == SHIP) return false; - checking += 1; - } while ((checking < b->size_) && (b->board_[x][checking] == HIT_SHIP)); + if (b->board_[checking][y] == SHIP) return false; + checking++; + } } if (x > 0) { - checking = x - 1; - do + checking = x; + while ((checking >= 0) && (b->board_[checking][y] == HIT_SHIP)) { if (b->board_[checking][y] == SHIP) return false; - checking -= 1; - } while ((checking >= 0) && (b->board_[checking][y] == HIT_SHIP)); + checking--; + } } if (x < b->size_ - 1) { - checking = x + 1; - do + checking = x; + while ((checking < b->size_) && (b->board_[checking][y] == HIT_SHIP)) { if (b->board_[checking][y] == SHIP) return false; - checking += 1; - } while ((checking < b->size_) && (b->board_[checking][y] == HIT_SHIP)); + checking++; + } } return true; } -// Output will probably be sent to server int receive_shot(int x, int y, board* b) { if (b->board_[x][y] == SHIP) @@ -296,19 +295,15 @@ void get_shot(char* shot, board* b_enemy) printf("Shot accepted!\n"); } -// The output of this is probably just gonna get sent to server -char* shoot(board* b_enemy) +void shoot(char* shot, board* b_enemy) { printf("Shots are given as \"A2\"\n"); printf("X coordinate: A-J\n"); printf("Y coordinate: 0-9\n"); printf("Where do you want to shoot?\n"); - char* shot = calloc(3, sizeof(char)); get_shot(shot, b_enemy); - return shot; } -// Adam, dont forget to save the last shot before you send it to server, you'll need it for this void mark_hit(int x, int y, int hit, board* b_enemy) { if (hit == 1) diff --git a/source/board.h b/source/board.h index 6cf119f..786679d 100644 --- a/source/board.h +++ b/source/board.h @@ -25,6 +25,8 @@ void board_destroy(board* b); void board_display(board* b_own, board* b_enemy); +void parse_input(char* input, int* x, int* y, bool* down); + bool validate_coords(char* coords); bool validate_rotation(char rotation); @@ -43,6 +45,6 @@ int receive_shot(int x, int y, board* b); void get_shot(char* shot, board* b_enemy); -char* shoot(board* b_enemy); +void shoot(char* shot, board* b_enemy); void mark_hit(int x, int y, int hit, board* b_enemy); \ No newline at end of file diff --git a/source/message.h b/source/message.h index e10b62d..89b098f 100644 --- a/source/message.h +++ b/source/message.h @@ -4,6 +4,8 @@ #define MSG_SHOT 'H' // Shot coordinates #define MSG_RESULT 'R' // Shot result #define MSG_GAME_OVER 'G' // Game over +#define MSG_SINGLE_PLAYER 'B' // Single player mode +#define MSG_MULTI_PLAYER 'M' // Multi player mode typedef struct { char type; // Message type From 1d7889370b2c6b7136d4c330b2c4c1c431fb5862 Mon Sep 17 00:00:00 2001 From: WaffleMaker9000 Date: Sun, 12 Jan 2025 17:31:46 +0100 Subject: [PATCH 4/5] Oops --- source/board | Bin 27816 -> 0 bytes 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100755 source/board diff --git a/source/board b/source/board deleted file mode 100755 index afe226fb46ca808109ddc2709d308ee5eca5dddc..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 27816 zcmeHwdvsIRneRT5w=BP3*gOM=S0D*~@Ce~q%tJ)Hf|EdEXc8eyvMnM@9_fhKSwL7g zY4LUFHMchz+98kT&P+2cNtt^)S;=$_aoU8C+uN3OWo6Mx=MG6BwF{HDz|44B8qM$9 z4_P|cAv0_Kn(<~y-~OKa+u#27Ir|)KZyxh(Uhgmrri+t(i4nG|Oj7Cq zG}Q5=n9yg5il-gg+-7#YR;6c$>tK_LhSUa?doNhQe@}Z2O7GG_X~zzaOES|?w|6)6 zC@x>LsNrPysP;x1heIm<8mjQJ+DN!*@uJ#DusRZsCOfJ-mMpGbyr?D-t63miAiqp} zD5vhcYdeFPKBB2GGRFalYkR~Sr}!k_dt}Fz@}uv6uk+KZzbJmEpkn~i<#Yx;KTT!qOa{F$gFaRri@;a}#v(8lfw2gT zMc_YA1g?}!{hhb#lH1$Ef3XV7+$Z`hN9v5X>rMBML?=_X?*utD`vH8HOsi)k-%6Z; zk5Z{rbFaW@p)>IFVVo8?13w(bX<;_-zlU*JkPW;rjMKtr;C~F`wBQ*yIE>RmXCOX| z(*kFpDUGLQ*TB2xl4WhW@!!#qx4>5x^4 zp;=6I3{76@T*x$fmj8?tN#wlkfF--t!*yzH!d!?S8xeBbf1a|IpjxIqU5%ecRJd-MAQ% zGCkYRdV9)yw)A@g?|9$%(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>*5^(rH<0O&mL&Me&8iN-4eOv#r0Q zVYTo`ujjNOkigw(=>cq^WOSWU1qiY6q)!-e4N!Czj`6Hg}M z11}zm_(?(4+Oc@85nZW4wWbBDSW&^&dsbtGI)Kv5gmg@#Voj)3&BfLrtZ7Wzkdsns z=sKG6w}oL$s$&3I4Xy_CR~ev%nvyM$^haB8=}>4m+8on$e^XODv`1q&)IX%_h@h?# z8Cj9W(HpqVzRXx5u;! zUfySw#lSN4?f=iitSo6-LuMCRw`S_=b+d+Mw)l1W_k)i!UTBMmD0<(ShI18NzfiaP z?+2fj`C9+1>c57x9-~4&QdR5YHyBaE_$C#tulKoWNbl@3i#`5-1Sahn)B3ZNqZ*n@ zS^KZ$G~5q9?LpJH-iM~49zVPP$}FRw&uL7wzTOw7A^pBUMYnJ7{~v=#`fErCMenoI zFjJpmX4n4~XcQA|KYia<_kXod01~p;^M`)2%+xoPo`%mWJq;8)-ORUb`h7}3LoGj= z{t27@8Kt0MuU1r0+tctho4#HbYG~08v5cbiH0gCzGW)OB`x+K$K?UvcdrRr-@z?8N z4S%ENgEphbPvbwa>Feh-4UcO<1?}yBVADUkND6BBWZE%Fx3_-*Wc*Sq<{(MveXIKa zWwFa^T*Kc&heE8osrR)WSV`7~^HpNQxYqvz7?itOUq8R<|7S;UaH07}MeFHt{4QuT zep+AeYwcJ?(%2p+LHb+kYv_Vt)?;SLtv;=NM-xE>gZrsJmV>>Ne^kLkvUGEoak^g8|B zN?)}vxP_qo>uzLV_O%MS@_WNv(7)d7f{K=$JRwbZ+%Dr=CLvD V?bBnlO++ZZ Date: Sun, 12 Jan 2025 17:57:07 +0100 Subject: [PATCH 5/5] Made port a macro --- client/client.c | 2 +- server/server.c | 8 ++++---- server/server.h | 1 + source/utils.h | 2 ++ 4 files changed, 8 insertions(+), 5 deletions(-) diff --git a/client/client.c b/client/client.c index 84cd6ba..8db3d7c 100644 --- a/client/client.c +++ b/client/client.c @@ -130,7 +130,7 @@ void play_game(int sock) int main(int argc, char **argv) { int mode; - int port = 8536; + int port = DEFAULT_PORT; if (argc >= 2) port = atoi(argv[1]); diff --git a/server/server.c b/server/server.c index 0d7d9a0..634dccb 100644 --- a/server/server.c +++ b/server/server.c @@ -181,9 +181,9 @@ void *handle_client(void *arg) } char shot[3]; generate_shot(shot, &bot_state->b_enemy); - int x = shot[0] - 'A'; - int y = shot[1] - '0'; - + int x; + int y; + parse_input(shot, &x, &y, NULL); Message bot_shot = { .type = MSG_SHOT, .x = x, @@ -266,7 +266,7 @@ int main(int argc, char** argv) // Initialize players array with 0s memset(players, 0, sizeof(players)); - int port = 8536; + int port = DEFAULT_PORT; if(argc >= 2) port = atoi(argv[1]); diff --git a/server/server.h b/server/server.h index 7f27161..e3d173b 100644 --- a/server/server.h +++ b/server/server.h @@ -9,6 +9,7 @@ #include #include "../source/message.h" #include "bot_board.h" +#include "../source/utils.h" #define MAX_PLAYERS 2 #define PLAYER_TYPE_HUMAN 0 diff --git a/source/utils.h b/source/utils.h index d503728..5a2a4c2 100644 --- a/source/utils.h +++ b/source/utils.h @@ -1,4 +1,6 @@ #include +#define DEFAULT_PORT 8536 + // Clears the screen using ANSI escape sequence void clear_screen(); \ No newline at end of file