Skip to content

Commit

Permalink
Merge pull request #2 from ArkadySK/board_display
Browse files Browse the repository at this point in the history
Board display
  • Loading branch information
ArkadySK authored Jan 7, 2025
2 parents a56d1c7 + be76410 commit 0f32966
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.vscode/
build/
18 changes: 10 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,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;
}

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;
}

0 comments on commit 0f32966

Please sign in to comment.