Skip to content

Commit

Permalink
Merge pull request #10 from ArkadySK/game_over
Browse files Browse the repository at this point in the history
Game over functionality
  • Loading branch information
ArkadySK authored Jan 12, 2025
2 parents a2a1194 + 90ccbd6 commit 90c82f2
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
9 changes: 9 additions & 0 deletions client/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,14 @@ void play_game(int sock)
case MSG_SHOT:
// Received shot from opponent
int hit =receive_shot(msg.x, msg.y, &b_own);

if (hit == -1) {
printf("You lost!\n");
Message game_over_msg = {.type = MSG_GAME_OVER};
send(sock, &game_over_msg, sizeof(Message), 0);
game_over = true;
break;
}

// Send result back
Message result = {
Expand All @@ -102,6 +110,7 @@ void play_game(int sock)
break;

case MSG_GAME_OVER:
printf("You win!\n");
game_over = true;
break;
}
Expand Down
4 changes: 4 additions & 0 deletions server/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,10 @@ void *handle_client(void *arg)
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);
break;
}
}

Expand Down
3 changes: 1 addition & 2 deletions source/board.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ void board_destroy(board* b)

void board_display(board* b_own, board* b_enemy)
{
//clear_screen();
clear_screen();
printf(" Your board | Enemy board\n");
printf(" A B C D E F G H I J | A B C D E F G H I J\n");
for (int j = 0; j < b_own->size_; j++)
Expand Down Expand Up @@ -166,7 +166,6 @@ void place_ships(board* b)
printf("Ships cannot be adjacent to other ships\n");
printf("-------------------------------\n");
printf("Press any key to continue...\n");
getchar();
while (getchar() != '\n');
board_display(b, b);
char* position = calloc(4, sizeof(char));
Expand Down

0 comments on commit 90c82f2

Please sign in to comment.