Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Game over #10

Merged
merged 2 commits into from
Jan 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions client/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,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 @@ -103,6 +111,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();
ArkadySK marked this conversation as resolved.
Show resolved Hide resolved
while (getchar() != '\n');
board_display(b, b);
char* position = calloc(4, sizeof(char));
Expand Down