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 display #2

Merged
merged 13 commits into from
Jan 7, 2025
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.vscode/
18 changes: 10 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,22 @@ The game board is displayed in the following way for each player:
```
A B C D E F G G I J
1 o o o o o o o o o o
2 x o o o o o o o o o
3 . . . . . . . . . .
2 X o o o o o o o o o
3 # . . . . . . . . .
4 o o o o o o o o o o
5 o o . . . . . . o o
6 . . . x x x x o o o
7 x . o . o . o o o .
8 x . o . o . o . . .
9 . . . . . . . . . .
10 . . . . . . . . . .
6 . . . X X X X o o o
7 X . o . o . o o o .
8 X . o . o . o # # #
9 # . . . . . . . . .
10 . . . . . # # # # #
```

\# - Players ship tile

o - Hit water

x - Hit BattleShip
X - Hit BattleShip

. - Not yet hit

Expand Down
67 changes: 67 additions & 0 deletions source/board.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#include "board.h"

void board_init(board* b, int size)
{
b->board_ = malloc(size * sizeof(char*));
for (int i = 0; i < size; i++)
{
b->board_[i] = calloc(size, sizeof(char));
for (int j = 0; j < size; j++)
{
b->board_[i][j] = NOT_HIT;
}
}

b->size_ = size;
b->destroyed_ = 0;
WaffleMaker9000 marked this conversation as resolved.
Show resolved Hide resolved
}

void board_destroy(board* b)
{
for (int i = 0; i < b->size_; i++)
{
free(b->board_[i]);
}
free(b->board_);
}

void board_display(board* b_own, board* b_enemy)
{
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++)
{
printf("%2d ", i + 1);
for (int j = 0; j < b_own->size_; j++)
{
printf("%c ", b_own->board_[i][j]);
}
printf("|%2d ", i + 1);
for (int j = 0; j < b_enemy->size_; j++)
{
if (b_enemy->board_[i][j] = SHIP)
{
printf("%c ", NOT_HIT);
}
else
{
printf("%c ", b_enemy->board_[i][j]);
}
}
printf("\n");
}
}

void place_ships(board* b)
{
printf("Ship coordinates are given as: \"2Ad\" ");
printf("where 2A are the coordinates of the ship, ");
printf("and the last letter is either \"d\" or \"r\" for down or right\n");
printf("Where do you want to place the 5 tile ship?");
// TODO: Waffle
}

int shoot(board* b, int x, int y)
{
// TODO: Waffle
}
26 changes: 26 additions & 0 deletions source/board.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>

#define NOT_HIT '.'
#define HIT_WATER 'o'
#define HIT_SHIP 'X'
#define SHIP '#'

typedef struct board board;

struct board {
char** board_;
int size_;
int destroyed_;
};

void board_init(board* b, int size);

void board_destroy(board* b);

void place_ships(board* b);

void board_display(board* b_own, board* b_enemy);

int shoot(board* b, int x, int y);
13 changes: 13 additions & 0 deletions source/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include "board.h"

int main(int argc, char const *argv[])
{
board b_own;
board_init(&b_own, 10);
board b_enemy;
board_init(&b_enemy, 10);
board_display(&b_own, &b_enemy);
board_destroy(&b_own);
board_destroy(&b_enemy);
return 0;
}