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

Board shoot #5

Merged
merged 4 commits into from
Jan 10, 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
116 changes: 113 additions & 3 deletions source/board.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ void board_destroy(board* b)

void board_display(board* b_own, board* b_enemy)
{
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 i = 0; i < b_own->size_; i++)
Expand Down Expand Up @@ -151,7 +152,7 @@ void get_ship(char* position, int size, board* b)
{
int c;
while ((c = getchar() != '\n') && c != EOF);
if (fgets(position, sizeof(char) * 4, stdin) == NULL)
if (fgets(position, sizeof(char) * 3, stdin) == NULL)
{
printf("An error occured, try again\n");
continue;
Expand Down Expand Up @@ -229,7 +230,116 @@ void place_ships(board* b)
printf("All ships successfully placed");
}

int shoot(board* b, int x, int y)
bool check_destroyed(int x, int y, board* b)
{
// TODO: Waffle
int checking;
if (y > 0) {
checking = y - 1;
do
{
if (b->board_[x][checking] == SHIP) return false;
checking -= 1;
} while ((checking >= 0) && (b->board_[x][checking] == HIT_SHIP));
}
if (y < b->size_ - 1)
{
checking = y + 1;
do
{
if (b->board_[x][checking] == SHIP) return false;
checking += 1;
} while ((checking < b->size_) && (b->board_[x][checking] == HIT_SHIP));
}
if (x > 0) {
checking = x - 1;
do
{
if (b->board_[checking][y] == SHIP) return false;
checking -= 1;
} while ((checking >= 0) && (b->board_[checking][y] == HIT_SHIP));
}
if (x < b->size_ - 1)
{
checking = x + 1;
do
{
if (b->board_[checking][y] == SHIP) return false;
checking += 1;
} while ((checking < b->size_) && (b->board_[checking][y] == HIT_SHIP));
}
return true;
}
// Output will probably be sent to server
bool receive_shot(int x, int y, board* b)
{
if (b->board_[x][y] == SHIP)
{
b->board_[x][y] = HIT_SHIP;
if (check_destroyed(x, y, b))
{
b->destroyed_++;
if (b->destroyed_ == 5)
{
// Probably gonna want to send the server an "im dead" message
}
}
return true;
}
b->board_[x][y] = HIT_WATER;
return false;
}

void get_shot(char* shot, board* b_enemy)
{
bool accepted = false;
while (!accepted)
{
int c;
while ((c = getchar() != '\n') && c != EOF);
if (fgets(shot, sizeof(char) * 2, stdin) == NULL) {
printf("An error occured, try again\n");
continue;
}
if (strrchr(shot, '\n'))
{
printf("Not enough characters determine shot, try again\n");
continue;
}
if (!validate_coords(shot))
{
printf("Input not valid, try again\n");
continue;
}
int x = shot[0] - 'A';
int y = shot[1] - '0';
if(b_enemy->board_[x][y] != NOT_HIT)
{
printf("You have already shot there, try again\n");
}
}
printf("Shot accepted!\n");
}

// The output of this is probably just gonna get sent to server
char* shoot(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[3];
get_shot(shot, b_enemy);
return shot;
}
// Adam, dont forget to save the last shot before you send it to server, youll need it for this
ArkadySK marked this conversation as resolved.
Show resolved Hide resolved
void mark_hit(char* shot, bool destroyed, board* b_enemy)
{
int x = shot[0] - 'A';
int y = shot[1] - '0';
if (destroyed)
{
b_enemy->board_[x][y] = HIT_SHIP;
return;
}
b_enemy->board_[x][y] = HIT_WATER;
}
11 changes: 10 additions & 1 deletion source/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include "utils.h"

#define NOT_HIT '.'
#define HIT_WATER 'o'
Expand Down Expand Up @@ -36,4 +37,12 @@ void finalise_placement(char* position, int size, board* b);

void place_ships(board* b);

int shoot(board* b, int x, int y);
bool check_destroyed(int x, int y, board* b);

bool receive_shot(int x, int y, board* b);

void get_shot(char* shot, board* b_enemy);

char* shoot(board* b_enemy);

void mark_hit(char* shot, bool destroyed, board* b_enemy);
3 changes: 0 additions & 3 deletions source/menu.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
#include <stdio.h>
#include <stdlib.h>
#include "menu.h"
#include "utils.h"

char display_menu(void) {
clear_screen();
Expand Down
4 changes: 4 additions & 0 deletions source/menu.h
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
#include <stdio.h>
#include <stdlib.h>
#include "utils.h"

char display_menu(void);
int handle_menu(void);
2 changes: 1 addition & 1 deletion source/utils.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <stdio.h>
#include "utils.h"

// Clears the screen using ANSI escape sequence
void clear_screen() {
Expand Down
2 changes: 2 additions & 0 deletions source/utils.h
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
#include <stdio.h>

// Clears the screen using ANSI escape sequence
void clear_screen(void);