From b17e60d0d57a1487babcf9268e92c5c4dcac0b2d Mon Sep 17 00:00:00 2001 From: WaffleMaker9000 Date: Thu, 2 Jan 2025 20:37:54 +0100 Subject: [PATCH 01/12] Display and Board Started work on the board and display functionality --- .vscode/settings.json | 5 +++++ README.md | 18 ++++++++++-------- player.c | 34 ++++++++++++++++++++++++++++++++++ player.h | 17 +++++++++++++++++ 4 files changed, 66 insertions(+), 8 deletions(-) create mode 100644 .vscode/settings.json create mode 100644 player.c create mode 100644 player.h diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..76ea51d --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "editor.rulers": [ + 80 + ] +} \ No newline at end of file diff --git a/README.md b/README.md index c3fe45e..5105900 100644 --- a/README.md +++ b/README.md @@ -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 (alt + 219) + o - Hit water -x - Hit BattleShip +X - Hit BattleShip . - Not yet hit diff --git a/player.c b/player.c new file mode 100644 index 0000000..5d3d6bb --- /dev/null +++ b/player.c @@ -0,0 +1,34 @@ +#include "player.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] = '.'; + } + } + + b->destroyed_ = 0; +} + +void board_display(board b, bool own) +{ + printf(" A B C D E F G H I J\n"); + // TODO: Waffle +} + +void place_ships(board b) +{ + board_display(b, true); + printf("Where do you want to place the 5 tile ship?"); + // TODO: Waffle +} + +void shoot(board b) +{ + // TODO: Waffle +} \ No newline at end of file diff --git a/player.h b/player.h new file mode 100644 index 0000000..f690744 --- /dev/null +++ b/player.h @@ -0,0 +1,17 @@ +#include +#include + +typedef struct board board; + +struct board { + char** board_; + int destroyed_; +}; + +void board_init(board b, int size); + +void place_ships(board b); + +void board_display(board b, bool own); + +int shoot(board b, int x, int y); \ No newline at end of file From a8bfad8c946a85d012b8e360404cd89766c2a890 Mon Sep 17 00:00:00 2001 From: WaffleMaker9000 Date: Thu, 2 Jan 2025 21:07:58 +0100 Subject: [PATCH 02/12] Implemented display Display implemented, macros for symbols created --- player.c | 24 ++++++++++++++++++++---- player.h | 6 ++++++ 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/player.c b/player.c index 5d3d6bb..63655ba 100644 --- a/player.c +++ b/player.c @@ -8,20 +8,36 @@ void board_init(board* b, int size) b->board_[i] = calloc(size, sizeof(char)); for (int j = 0; j < size; j++) { - b->board_[i][j] = '.'; + b->board_[i][j] = NOT_HIT; } } + b->size_ = size; b->destroyed_ = 0; } -void board_display(board b, bool own) +void board_display(board* b, bool own) { printf(" A B C D E F G H I J\n"); - // TODO: Waffle + for (int i = 0; i < b->size_; i++) + { + printf("%2d ", i); + for (int j = 0; j < b->size_; j++) + { + if (!own && b->board_[i][j] == SHIP) + { + printf("%c ", NOT_HIT); + } + else + { + printf("%c ", b->board_[i][j]); + } + } + printf("\n"); + } } -void place_ships(board b) +void place_ships(board* b) { board_display(b, true); printf("Where do you want to place the 5 tile ship?"); diff --git a/player.h b/player.h index f690744..48a635a 100644 --- a/player.h +++ b/player.h @@ -1,10 +1,16 @@ #include #include +#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_; }; From 0584d6d21c794431cba1466dae2b78d2c0948817 Mon Sep 17 00:00:00 2001 From: WaffleMaker9000 Date: Thu, 2 Jan 2025 22:25:40 +0100 Subject: [PATCH 03/12] Display changed changed name of files from player to board, created a testing main file for the drawing function --- player.c => board.c | 22 ++++++++++++++-------- player.h => board.h | 8 ++++---- main.c | 11 +++++++++++ 3 files changed, 29 insertions(+), 12 deletions(-) rename player.c => board.c (55%) rename player.h => board.h (60%) create mode 100644 main.c diff --git a/player.c b/board.c similarity index 55% rename from player.c rename to board.c index 63655ba..ea5ed10 100644 --- a/player.c +++ b/board.c @@ -1,4 +1,4 @@ -#include "player.h" +#include "board.h" void board_init(board* b, int size) { @@ -16,21 +16,27 @@ void board_init(board* b, int size) b->destroyed_ = 0; } -void board_display(board* b, bool own) +void board_display(board* b_own, board* b_enemy) { - printf(" A B C D E F G H I J\n"); - for (int i = 0; i < b->size_; i++) + 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); - for (int j = 0; j < b->size_; j++) + for (int j = 0; j < b_own->size_; j++) { - if (!own && b->board_[i][j] == SHIP) + printf("%c ", b_own->board_[i][j]); + } + printf("|%2d ", i); + for (int j = 0; j < b_enemy->size_; j++) + { + if (b_enemy->board_[i][j] = SHIP) { printf("%c ", NOT_HIT); } else { - printf("%c ", b->board_[i][j]); + printf("%c ", b_enemy->board_[i][j]); } } printf("\n"); @@ -44,7 +50,7 @@ void place_ships(board* b) // TODO: Waffle } -void shoot(board b) +void shoot(board* b) { // TODO: Waffle } \ No newline at end of file diff --git a/player.h b/board.h similarity index 60% rename from player.h rename to board.h index 48a635a..5c187f8 100644 --- a/player.h +++ b/board.h @@ -14,10 +14,10 @@ struct board { int destroyed_; }; -void board_init(board b, int size); +void board_init(board* b, int size); -void place_ships(board b); +void place_ships(board* b); -void board_display(board b, bool own); +void board_display(board* b_own, board* b_enemy); -int shoot(board b, int x, int y); \ No newline at end of file +int shoot(board* b, int x, int y); \ No newline at end of file diff --git a/main.c b/main.c new file mode 100644 index 0000000..863ca74 --- /dev/null +++ b/main.c @@ -0,0 +1,11 @@ +#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); + return 0; +} From 611adedd3777c669cf478aa7af98239998ab8a75 Mon Sep 17 00:00:00 2001 From: WaffleMaker9000 Date: Thu, 2 Jan 2025 22:36:34 +0100 Subject: [PATCH 04/12] Fixed compile errors Errors in compilation fixed, included stdlib.h --- board.c | 5 ++--- board.h | 1 + 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/board.c b/board.c index ea5ed10..b3f62d9 100644 --- a/board.c +++ b/board.c @@ -2,7 +2,7 @@ void board_init(board* b, int size) { - b->board_ = malloc(size, sizeof(char*)); + b->board_ = malloc(size * sizeof(char*)); for (int i = 0; i < size; i++) { b->board_[i] = calloc(size, sizeof(char)); @@ -45,12 +45,11 @@ void board_display(board* b_own, board* b_enemy) void place_ships(board* b) { - board_display(b, true); printf("Where do you want to place the 5 tile ship?"); // TODO: Waffle } -void shoot(board* b) +void shoot(board* b, int x, int y) { // TODO: Waffle } \ No newline at end of file diff --git a/board.h b/board.h index 5c187f8..75c0cba 100644 --- a/board.h +++ b/board.h @@ -1,5 +1,6 @@ #include #include +#include #define NOT_HIT '.' #define HIT_WATER 'o' From 23aa299bafcfe3d15177a0022d4b69bc22351d49 Mon Sep 17 00:00:00 2001 From: WaffleMaker9000 Date: Thu, 2 Jan 2025 22:39:01 +0100 Subject: [PATCH 05/12] Fixed dumb type error changed definition to match declaration --- board.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/board.c b/board.c index b3f62d9..9871132 100644 --- a/board.c +++ b/board.c @@ -49,7 +49,7 @@ void place_ships(board* b) // TODO: Waffle } -void shoot(board* b, int x, int y) +int shoot(board* b, int x, int y) { // TODO: Waffle } \ No newline at end of file From d4720f1a0db47357316df86f37d7a9de404684f0 Mon Sep 17 00:00:00 2001 From: WaffleMaker9000 Date: Thu, 2 Jan 2025 22:44:21 +0100 Subject: [PATCH 06/12] =?UTF-8?q?Changed=20=E2=96=88=20to=20S?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit causing problems in compiling, i need to test, so ill figure this out later, for now its S --- board.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/board.h b/board.h index 75c0cba..3a67c02 100644 --- a/board.h +++ b/board.h @@ -5,7 +5,7 @@ #define NOT_HIT '.' #define HIT_WATER 'o' #define HIT_SHIP 'X' -#define SHIP '█' +#define SHIP 'S' typedef struct board board; From ef414ad9648fcfbc82809024531cfe85e2f8e488 Mon Sep 17 00:00:00 2001 From: WaffleMaker9000 Date: Fri, 3 Jan 2025 00:13:54 +0100 Subject: [PATCH 07/12] Fixed display formatting Minor tweak to board printing output formatting --- board.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/board.c b/board.c index 9871132..d31885c 100644 --- a/board.c +++ b/board.c @@ -18,16 +18,16 @@ void board_init(board* b, int size) void board_display(board* b_own, board* b_enemy) { - printf(" Your board | Enemy board\n"); + 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); + printf("%2d ", i + 1); for (int j = 0; j < b_own->size_; j++) { printf("%c ", b_own->board_[i][j]); } - printf("|%2d ", i); + printf("|%2d ", i + 1); for (int j = 0; j < b_enemy->size_; j++) { if (b_enemy->board_[i][j] = SHIP) From fc024eba908cb46593f06202fee8c12488f9c582 Mon Sep 17 00:00:00 2001 From: WaffleMaker9000 Date: Tue, 7 Jan 2025 13:32:16 +0100 Subject: [PATCH 08/12] Moved files into source folder to reflect architecture and avoid conflicts, all files were moved into the source folder --- board.c => source/board.c | 0 board.h => source/board.h | 0 main.c => source/main.c | 0 3 files changed, 0 insertions(+), 0 deletions(-) rename board.c => source/board.c (100%) rename board.h => source/board.h (100%) rename main.c => source/main.c (100%) diff --git a/board.c b/source/board.c similarity index 100% rename from board.c rename to source/board.c diff --git a/board.h b/source/board.h similarity index 100% rename from board.h rename to source/board.h diff --git a/main.c b/source/main.c similarity index 100% rename from main.c rename to source/main.c From f8e805111c7f38784946226e6b5e1a03cd0fe26f Mon Sep 17 00:00:00 2001 From: WaffleMaker9000 Date: Tue, 7 Jan 2025 14:20:12 +0100 Subject: [PATCH 09/12] Board display finalised Finalised board display functionality --- source/board.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/source/board.c b/source/board.c index d31885c..6be9d2a 100644 --- a/source/board.c +++ b/source/board.c @@ -45,6 +45,9 @@ void board_display(board* b_own, board* b_enemy) 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 } From d375c4c25b3362c6c0cc5ea24346bec746348a02 Mon Sep 17 00:00:00 2001 From: WaffleMaker9000 Date: Tue, 7 Jan 2025 14:37:09 +0100 Subject: [PATCH 10/12] Resolved issues Added destructor, changed player ship tile to '#' --- README.md | 10 +++++----- source/board.c | 9 +++++++++ source/board.h | 4 +++- source/main.c | 2 ++ 4 files changed, 19 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 5105900..f8596d7 100644 --- a/README.md +++ b/README.md @@ -6,17 +6,17 @@ 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 █ . . . . . . . . . + 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 . . . . . █ █ █ █ █ + 8 X . o . o . o # # # + 9 # . . . . . . . . . +10 . . . . . # # # # # ``` -█ - Players ship tile (alt + 219) +\# - Players ship tile o - Hit water diff --git a/source/board.c b/source/board.c index 6be9d2a..7198a43 100644 --- a/source/board.c +++ b/source/board.c @@ -16,6 +16,15 @@ void board_init(board* b, int 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"); diff --git a/source/board.h b/source/board.h index 3a67c02..34329da 100644 --- a/source/board.h +++ b/source/board.h @@ -5,7 +5,7 @@ #define NOT_HIT '.' #define HIT_WATER 'o' #define HIT_SHIP 'X' -#define SHIP 'S' +#define SHIP '#' typedef struct board board; @@ -17,6 +17,8 @@ struct board { 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); diff --git a/source/main.c b/source/main.c index 863ca74..8337b82 100644 --- a/source/main.c +++ b/source/main.c @@ -7,5 +7,7 @@ int main(int argc, char const *argv[]) board b_enemy; board_init(&b_enemy, 10); board_display(&b_own, &b_enemy); + board_destroy(&b_own); + board_destroy(&b_enemy); return 0; } From f69dcf2347f5f5cc043d31f40c786cb7941f225c Mon Sep 17 00:00:00 2001 From: WaffleMaker9000 Date: Tue, 7 Jan 2025 14:40:38 +0100 Subject: [PATCH 11/12] Created Gitignore and added .vscode folder to it --- .gitignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..dbe9c82 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.vscode/ \ No newline at end of file From ffed6e7532735844c7b22b85a0cea9a3bbcb845e Mon Sep 17 00:00:00 2001 From: WaffleMaker9000 Date: Tue, 7 Jan 2025 14:46:30 +0100 Subject: [PATCH 12/12] Removed vscode folder --- .vscode/settings.json | 5 ----- 1 file changed, 5 deletions(-) delete mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index 76ea51d..0000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "editor.rulers": [ - 80 - ] -} \ No newline at end of file