From 8b88c2fbc974b4564ca0977ee294a7979f941a2b Mon Sep 17 00:00:00 2001 From: Ariana Ginju Date: Wed, 24 Jan 2024 12:36:32 +0000 Subject: [PATCH 01/40] Add new directory Author: Ariana Ginju --- src/main/c/GameTic_Tac_Toe/.gitkeep | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 src/main/c/GameTic_Tac_Toe/.gitkeep diff --git a/src/main/c/GameTic_Tac_Toe/.gitkeep b/src/main/c/GameTic_Tac_Toe/.gitkeep new file mode 100644 index 0000000..e69de29 From 2c08c49fdf287f7dcb61a0f36b8d25a2472da8a7 Mon Sep 17 00:00:00 2001 From: Ariana Ginju Date: Wed, 24 Jan 2024 12:45:27 +0000 Subject: [PATCH 02/40] Add new file --- src/main/c/GameTic_Tac_Toe/temp | 1 + 1 file changed, 1 insertion(+) create mode 100644 src/main/c/GameTic_Tac_Toe/temp diff --git a/src/main/c/GameTic_Tac_Toe/temp b/src/main/c/GameTic_Tac_Toe/temp new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/src/main/c/GameTic_Tac_Toe/temp @@ -0,0 +1 @@ + From eca9fef7a13af1ef19f0765d5feeebc03878c48b Mon Sep 17 00:00:00 2001 From: Ariana Ginju Date: Wed, 24 Jan 2024 13:04:28 +0000 Subject: [PATCH 03/40] Delete temp --- src/main/c/GameTic_Tac_Toe/temp | 1 - 1 file changed, 1 deletion(-) delete mode 100644 src/main/c/GameTic_Tac_Toe/temp diff --git a/src/main/c/GameTic_Tac_Toe/temp b/src/main/c/GameTic_Tac_Toe/temp deleted file mode 100644 index 8b13789..0000000 --- a/src/main/c/GameTic_Tac_Toe/temp +++ /dev/null @@ -1 +0,0 @@ - From a8c427c5b2c361bb6739f9188bfc24184eb4802d Mon Sep 17 00:00:00 2001 From: Ariana Ginju Date: Wed, 24 Jan 2024 14:21:33 +0100 Subject: [PATCH 04/40] added global board --- src/main/c/GameTic_Tac_Toe/game.c | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 src/main/c/GameTic_Tac_Toe/game.c diff --git a/src/main/c/GameTic_Tac_Toe/game.c b/src/main/c/GameTic_Tac_Toe/game.c new file mode 100644 index 0000000..7d5c061 --- /dev/null +++ b/src/main/c/GameTic_Tac_Toe/game.c @@ -0,0 +1,6 @@ +#include + +char board[3][3] = {{'1', '2', '3'}, + {'4', '5', '6'}, + {'7', '8', '9'}}; +char currentPlayer = 'X'; From 794f052fa746757d14dba5d6de325061cb229aca Mon Sep 17 00:00:00 2001 From: Ariana Ginju Date: Tue, 30 Jan 2024 11:44:47 +0000 Subject: [PATCH 05/40] display the game board --- src/main/c/GameTic_Tac_Toe/game.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/main/c/GameTic_Tac_Toe/game.c b/src/main/c/GameTic_Tac_Toe/game.c index 7d5c061..aeec7e0 100644 --- a/src/main/c/GameTic_Tac_Toe/game.c +++ b/src/main/c/GameTic_Tac_Toe/game.c @@ -4,3 +4,16 @@ char board[3][3] = {{'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'}}; char currentPlayer = 'X'; + +// Funktionen zur Anzeige des Spielbretts +void displayBoard() { + printf("Tic-Tac-Toe\n"); + for (int i = 0; i < 3; i++) { + for (int j = 0; j < 3; j++) { + printf("%c", board[i][j]); + if (j < 2) printf(" | "); + } + printf("\n"); + if (i < 2) printf("---------\n"); + } +} From 7999eb79fe7155da4fc5b3b8d94f54fdd2d395c4 Mon Sep 17 00:00:00 2001 From: Ariana Ginju Date: Tue, 30 Jan 2024 11:45:48 +0000 Subject: [PATCH 06/40] Verification of the winner --- src/main/c/GameTic_Tac_Toe/game.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/main/c/GameTic_Tac_Toe/game.c b/src/main/c/GameTic_Tac_Toe/game.c index aeec7e0..2937166 100644 --- a/src/main/c/GameTic_Tac_Toe/game.c +++ b/src/main/c/GameTic_Tac_Toe/game.c @@ -17,3 +17,6 @@ void displayBoard() { if (i < 2) printf("---------\n"); } } + +// Funktion zur Überprüfung des Gewinners +char checkWinner() { From a9fb21881e83ecb0337d2e60b5ff9bca5bb78e33 Mon Sep 17 00:00:00 2001 From: Ariana Ginju Date: Tue, 30 Jan 2024 11:46:46 +0000 Subject: [PATCH 07/40] Check horizontal and vertical lines --- src/main/c/GameTic_Tac_Toe/game.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/main/c/GameTic_Tac_Toe/game.c b/src/main/c/GameTic_Tac_Toe/game.c index 2937166..a08e501 100644 --- a/src/main/c/GameTic_Tac_Toe/game.c +++ b/src/main/c/GameTic_Tac_Toe/game.c @@ -20,3 +20,8 @@ void displayBoard() { // Funktion zur Überprüfung des Gewinners char checkWinner() { + // Überprüfe horizontale und vertikale Linien + for (int i = 0; i < 3; i++) { + if (board[i][0] == board[i][1] && board[i][1] == board[i][2]) return board[i][0]; + if (board[0][i] == board[1][i] && board[1][i] == board[2][i]) return board[0][i]; + } From 30f7147c18b85d24caa91288a8d7e6cd7f45f4d5 Mon Sep 17 00:00:00 2001 From: Ariana Ginju Date: Tue, 30 Jan 2024 11:47:58 +0000 Subject: [PATCH 08/40] Check diagonal lines --- src/main/c/GameTic_Tac_Toe/game.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/c/GameTic_Tac_Toe/game.c b/src/main/c/GameTic_Tac_Toe/game.c index a08e501..3f1dc5a 100644 --- a/src/main/c/GameTic_Tac_Toe/game.c +++ b/src/main/c/GameTic_Tac_Toe/game.c @@ -25,3 +25,7 @@ char checkWinner() { if (board[i][0] == board[i][1] && board[i][1] == board[i][2]) return board[i][0]; if (board[0][i] == board[1][i] && board[1][i] == board[2][i]) return board[0][i]; } + // Überprüfe diagonale Linien + if (board[0][0] == board[1][1] && board[1][1] == board[2][2]) return board[0][0]; + if (board[0][2] == board[1][1] && board[1][1] == board[2][0]) return board[0][2]; + From 970382ae3c968897fb7925f33e52b2f3ca643152 Mon Sep 17 00:00:00 2001 From: Ariana Ginju Date: Tue, 30 Jan 2024 11:52:41 +0000 Subject: [PATCH 09/40] No winner --- src/main/c/GameTic_Tac_Toe/game.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/main/c/GameTic_Tac_Toe/game.c b/src/main/c/GameTic_Tac_Toe/game.c index 3f1dc5a..a91ba51 100644 --- a/src/main/c/GameTic_Tac_Toe/game.c +++ b/src/main/c/GameTic_Tac_Toe/game.c @@ -29,3 +29,6 @@ char checkWinner() { if (board[0][0] == board[1][1] && board[1][1] == board[2][2]) return board[0][0]; if (board[0][2] == board[1][1] && board[1][1] == board[2][0]) return board[0][2]; + // Kein Gewinner + return ' '; +} From 7f8de3f06bb9fa26aca2ef129c4b6041b24dc4f1 Mon Sep 17 00:00:00 2001 From: Ariana Ginju Date: Tue, 30 Jan 2024 11:58:28 +0000 Subject: [PATCH 10/40] Verification function --- src/main/c/GameTic_Tac_Toe/game.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/main/c/GameTic_Tac_Toe/game.c b/src/main/c/GameTic_Tac_Toe/game.c index a91ba51..6432f7f 100644 --- a/src/main/c/GameTic_Tac_Toe/game.c +++ b/src/main/c/GameTic_Tac_Toe/game.c @@ -32,3 +32,15 @@ char checkWinner() { // Kein Gewinner return ' '; } + +// Funktion zur Überprüfung, ob das Spiel unentschieden ist +int isBoardFull() { + for (int i = 0; i < 3; i++) { + for (int j = 0; j < 3; j++) { + if (board[i][j] != 'X' && board[i][j] != 'O') { + return 0; + } + } + } + return 1; +} From 1ec3169a5b6edffa9c36a7a30e3d2df7a3943381 Mon Sep 17 00:00:00 2001 From: Ariana Ginju Date: Tue, 30 Jan 2024 11:59:42 +0000 Subject: [PATCH 11/40] Function: A player's turn --- src/main/c/GameTic_Tac_Toe/game.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/main/c/GameTic_Tac_Toe/game.c b/src/main/c/GameTic_Tac_Toe/game.c index 6432f7f..089012b 100644 --- a/src/main/c/GameTic_Tac_Toe/game.c +++ b/src/main/c/GameTic_Tac_Toe/game.c @@ -44,3 +44,11 @@ int isBoardFull() { } return 1; } + + +// Funktion zum Zug eines Spielers +void makeMove() { + int choice; + printf("Spieler %c, wähle eine Zahl (1-9): ", currentPlayer); + scanf("%d", &choice); + From 22d08f9a4b27e6ed72339a872ad860226106cd85 Mon Sep 17 00:00:00 2001 From: Ariana Ginju Date: Tue, 30 Jan 2024 12:00:33 +0000 Subject: [PATCH 12/40] Convert the number --- src/main/c/GameTic_Tac_Toe/game.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/c/GameTic_Tac_Toe/game.c b/src/main/c/GameTic_Tac_Toe/game.c index 089012b..48035ae 100644 --- a/src/main/c/GameTic_Tac_Toe/game.c +++ b/src/main/c/GameTic_Tac_Toe/game.c @@ -52,3 +52,7 @@ void makeMove() { printf("Spieler %c, wähle eine Zahl (1-9): ", currentPlayer); scanf("%d", &choice); +// Konvertiere die Zahl in Zeilen- und Spaltenindex + int row = (choice - 1) / 3; + int col = (choice - 1) % 3; + From f1b61336a5a5beb5d08874fb901e9657932f9b1e Mon Sep 17 00:00:00 2001 From: Ariana Ginju Date: Tue, 30 Jan 2024 12:01:52 +0000 Subject: [PATCH 13/40] Check whether the selected field is valid --- src/main/c/GameTic_Tac_Toe/game.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/c/GameTic_Tac_Toe/game.c b/src/main/c/GameTic_Tac_Toe/game.c index 48035ae..f0ddb37 100644 --- a/src/main/c/GameTic_Tac_Toe/game.c +++ b/src/main/c/GameTic_Tac_Toe/game.c @@ -56,3 +56,7 @@ void makeMove() { int row = (choice - 1) / 3; int col = (choice - 1) % 3; + // Überprüfe, ob das gewählte Feld gültig ist + if (choice >= 1 && choice <= 9 && board[row][col] != 'X' && board[row][col] != 'O') { + + From 9ef4f2f96690b5e7cdf6af931d9ffcbac1abaec5 Mon Sep 17 00:00:00 2001 From: Ariana Ginju Date: Tue, 30 Jan 2024 12:02:18 +0000 Subject: [PATCH 14/40] Update game.c --- src/main/c/GameTic_Tac_Toe/game.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/c/GameTic_Tac_Toe/game.c b/src/main/c/GameTic_Tac_Toe/game.c index f0ddb37..5129548 100644 --- a/src/main/c/GameTic_Tac_Toe/game.c +++ b/src/main/c/GameTic_Tac_Toe/game.c @@ -58,5 +58,6 @@ void makeMove() { // Überprüfe, ob das gewählte Feld gültig ist if (choice >= 1 && choice <= 9 && board[row][col] != 'X' && board[row][col] != 'O') { - + board[row][col] = currentPlayer; + } else { From d47cb68db8380e185c9556709a41c366ccd0a30d Mon Sep 17 00:00:00 2001 From: Ariana Ginju Date: Tue, 30 Jan 2024 12:03:21 +0000 Subject: [PATCH 15/40] Invalid move --- src/main/c/GameTic_Tac_Toe/game.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main/c/GameTic_Tac_Toe/game.c b/src/main/c/GameTic_Tac_Toe/game.c index 5129548..51f5c7c 100644 --- a/src/main/c/GameTic_Tac_Toe/game.c +++ b/src/main/c/GameTic_Tac_Toe/game.c @@ -60,4 +60,7 @@ void makeMove() { if (choice >= 1 && choice <= 9 && board[row][col] != 'X' && board[row][col] != 'O') { board[row][col] = currentPlayer; } else { - + printf("Ungültiger Zug! Bitte wähle erneut.\n"); + makeMove(); // Rekursiver Aufruf, bis ein gültiger Zug gemacht wird + } +} From 87f5a6c5d8267494d57cd5302e9e5517ef909f61 Mon Sep 17 00:00:00 2001 From: Ariana Ginju Date: Tue, 30 Jan 2024 12:04:05 +0000 Subject: [PATCH 16/40] Update main --- src/main/c/GameTic_Tac_Toe/game.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/main/c/GameTic_Tac_Toe/game.c b/src/main/c/GameTic_Tac_Toe/game.c index 51f5c7c..85a08a2 100644 --- a/src/main/c/GameTic_Tac_Toe/game.c +++ b/src/main/c/GameTic_Tac_Toe/game.c @@ -64,3 +64,8 @@ void makeMove() { makeMove(); // Rekursiver Aufruf, bis ein gültiger Zug gemacht wird } } + +int main() { + while (1) { + displayBoard(); + makeMove(); From bd7b0a63c647e259a8fb7ab9255d3a7d289996a8 Mon Sep 17 00:00:00 2001 From: Ariana Ginju Date: Tue, 30 Jan 2024 12:05:23 +0000 Subject: [PATCH 17/40] check winner --- src/main/c/GameTic_Tac_Toe/game.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/main/c/GameTic_Tac_Toe/game.c b/src/main/c/GameTic_Tac_Toe/game.c index 85a08a2..215af00 100644 --- a/src/main/c/GameTic_Tac_Toe/game.c +++ b/src/main/c/GameTic_Tac_Toe/game.c @@ -69,3 +69,10 @@ int main() { while (1) { displayBoard(); makeMove(); + + char winner = checkWinner(); + if (winner == 'X' || winner == 'O') { + displayBoard(); + printf("Spieler %c gewinnt!\n", winner); + break; + } From b02a1f7f6cf0cc7727c125ff136099760d521075 Mon Sep 17 00:00:00 2001 From: Ariana Ginju Date: Tue, 30 Jan 2024 12:20:45 +0000 Subject: [PATCH 18/40] possibility: draw --- src/main/c/GameTic_Tac_Toe/game.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/main/c/GameTic_Tac_Toe/game.c b/src/main/c/GameTic_Tac_Toe/game.c index 215af00..0f0d6f8 100644 --- a/src/main/c/GameTic_Tac_Toe/game.c +++ b/src/main/c/GameTic_Tac_Toe/game.c @@ -76,3 +76,9 @@ int main() { printf("Spieler %c gewinnt!\n", winner); break; } + + if (isBoardFull()) { + displayBoard(); + printf("Unentschieden!\n"); + break; + } From 421db299f35517a88096b718f0af89ab65693137 Mon Sep 17 00:00:00 2001 From: Ariana Ginju Date: Tue, 30 Jan 2024 12:22:57 +0000 Subject: [PATCH 19/40] Change player --- src/main/c/GameTic_Tac_Toe/game.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/main/c/GameTic_Tac_Toe/game.c b/src/main/c/GameTic_Tac_Toe/game.c index 0f0d6f8..b20f125 100644 --- a/src/main/c/GameTic_Tac_Toe/game.c +++ b/src/main/c/GameTic_Tac_Toe/game.c @@ -82,3 +82,10 @@ int main() { printf("Unentschieden!\n"); break; } + + // Wechsle den Spieler + currentPlayer = (currentPlayer == 'X') ? 'O' : 'X'; + } + + return 0; +} From 04b9365a8ac416738f3555c3f6eb17ea18a4ae2f Mon Sep 17 00:00:00 2001 From: Ariana Ginju Date: Tue, 30 Jan 2024 16:57:40 +0000 Subject: [PATCH 20/40] refactoring: lear condition for an invalid move --- src/main/c/GameTic_Tac_Toe/game.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/c/GameTic_Tac_Toe/game.c b/src/main/c/GameTic_Tac_Toe/game.c index b20f125..11d2a79 100644 --- a/src/main/c/GameTic_Tac_Toe/game.c +++ b/src/main/c/GameTic_Tac_Toe/game.c @@ -57,11 +57,11 @@ void makeMove() { int col = (choice - 1) % 3; // Überprüfe, ob das gewählte Feld gültig ist - if (choice >= 1 && choice <= 9 && board[row][col] != 'X' && board[row][col] != 'O') { - board[row][col] = currentPlayer; - } else { - printf("Ungültiger Zug! Bitte wähle erneut.\n"); + if (choice < 1 || choice > 9 || board[row][col] == 'X' || board[row][col] == 'O') { + printf("Ungültiger Zug! Bitte wähle erneut.\n"); makeMove(); // Rekursiver Aufruf, bis ein gültiger Zug gemacht wird + } else { + board[row][col] = currentPlayer; } } From a0ecdb63f1cbbe0fc19f296d86830176e43793b3 Mon Sep 17 00:00:00 2001 From: Ariana Ginju Date: Tue, 30 Jan 2024 17:05:35 +0000 Subject: [PATCH 21/40] refactoring: using else and if --- src/main/c/GameTic_Tac_Toe/game.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/c/GameTic_Tac_Toe/game.c b/src/main/c/GameTic_Tac_Toe/game.c index 11d2a79..278b1e5 100644 --- a/src/main/c/GameTic_Tac_Toe/game.c +++ b/src/main/c/GameTic_Tac_Toe/game.c @@ -27,7 +27,7 @@ char checkWinner() { } // Überprüfe diagonale Linien if (board[0][0] == board[1][1] && board[1][1] == board[2][2]) return board[0][0]; - if (board[0][2] == board[1][1] && board[1][1] == board[2][0]) return board[0][2]; + else if (board[0][2] == board[1][1] && board[1][1] == board[2][0]) return board[0][2]; // Kein Gewinner return ' '; From 990b115c96fed40a214baed9d0fe6d5dc5c63ec9 Mon Sep 17 00:00:00 2001 From: Ariana Ginju Date: Tue, 30 Jan 2024 17:15:30 +0000 Subject: [PATCH 22/40] refactoring: using an auxiliary feature for win conditions --- src/main/c/GameTic_Tac_Toe/game.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/main/c/GameTic_Tac_Toe/game.c b/src/main/c/GameTic_Tac_Toe/game.c index 278b1e5..0765c17 100644 --- a/src/main/c/GameTic_Tac_Toe/game.c +++ b/src/main/c/GameTic_Tac_Toe/game.c @@ -17,13 +17,19 @@ void displayBoard() { if (i < 2) printf("---------\n"); } } - +char checkLine(char a, char b, char c) { + if (a == b && b == c) return a; + return ' '; +} // Funktion zur Überprüfung des Gewinners char checkWinner() { // Überprüfe horizontale und vertikale Linien - for (int i = 0; i < 3; i++) { - if (board[i][0] == board[i][1] && board[i][1] == board[i][2]) return board[i][0]; - if (board[0][i] == board[1][i] && board[1][i] == board[2][i]) return board[0][i]; + for (int i = 0; i < 3; i++) { + char horizontalWinner = checkLine(board[i][0], board[i][1], board[i][2]); + char verticalWinner = checkLine(board[0][i], board[1][i], board[2][i]); + + if (horizontalWinner != ' ') return horizontalWinner; + if (verticalWinner != ' ') return verticalWinner; } // Überprüfe diagonale Linien if (board[0][0] == board[1][1] && board[1][1] == board[2][2]) return board[0][0]; From fec03368ca2cd1c5a0c2648c81f7709e179c9dd6 Mon Sep 17 00:00:00 2001 From: Ariana Ginju Date: Tue, 30 Jan 2024 17:18:52 +0000 Subject: [PATCH 23/40] refactoring: using an auxiliary function for diagonal lines --- src/main/c/GameTic_Tac_Toe/game.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/main/c/GameTic_Tac_Toe/game.c b/src/main/c/GameTic_Tac_Toe/game.c index 0765c17..215f890 100644 --- a/src/main/c/GameTic_Tac_Toe/game.c +++ b/src/main/c/GameTic_Tac_Toe/game.c @@ -32,8 +32,11 @@ char checkWinner() { if (verticalWinner != ' ') return verticalWinner; } // Überprüfe diagonale Linien - if (board[0][0] == board[1][1] && board[1][1] == board[2][2]) return board[0][0]; - else if (board[0][2] == board[1][1] && board[1][1] == board[2][0]) return board[0][2]; + char diagonal1Winner = checkLine(board[0][0], board[1][1], board[2][2]); + char diagonal2Winner = checkLine(board[0][2], board[1][1], board[2][0]); + + if (diagonal1Winner != ' ') return diagonal1Winner; + if (diagonal2Winner != ' ') return diagonal2Winner; // Kein Gewinner return ' '; From 483f2b54800761f661f66ec9e4f931386e707620 Mon Sep 17 00:00:00 2001 From: Ariana Ginju Date: Tue, 30 Jan 2024 17:43:49 +0000 Subject: [PATCH 24/40] refactoring: logic functions for player exchange Author: Ariana Ginju --- src/main/c/GameTic_Tac_Toe/game.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/main/c/GameTic_Tac_Toe/game.c b/src/main/c/GameTic_Tac_Toe/game.c index 215f890..a3f3ae2 100644 --- a/src/main/c/GameTic_Tac_Toe/game.c +++ b/src/main/c/GameTic_Tac_Toe/game.c @@ -56,6 +56,10 @@ int isBoardFull() { // Funktion zum Zug eines Spielers +char switchPlayer(char currentPlayer) { + return (currentPlayer == 'X') ? 'O' : 'X'; +} + void makeMove() { int choice; printf("Spieler %c, wähle eine Zahl (1-9): ", currentPlayer); @@ -71,6 +75,7 @@ void makeMove() { makeMove(); // Rekursiver Aufruf, bis ein gültiger Zug gemacht wird } else { board[row][col] = currentPlayer; + currentPlayer = switchPlayer(currentPlayer); } } From 29759098ad1f386a32dc74eb2b89f87d99ff4196 Mon Sep 17 00:00:00 2001 From: Ariana Ginju Date: Tue, 30 Jan 2024 17:53:55 +0000 Subject: [PATCH 25/40] refactoring: separation of the game loop into functions Author: Ariana Ginju --- src/main/c/GameTic_Tac_Toe/game.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/main/c/GameTic_Tac_Toe/game.c b/src/main/c/GameTic_Tac_Toe/game.c index a3f3ae2..f193efa 100644 --- a/src/main/c/GameTic_Tac_Toe/game.c +++ b/src/main/c/GameTic_Tac_Toe/game.c @@ -79,7 +79,7 @@ void makeMove() { } } -int main() { +void playGame() { while (1) { displayBoard(); makeMove(); @@ -95,11 +95,10 @@ int main() { displayBoard(); printf("Unentschieden!\n"); break; - } - - // Wechsle den Spieler - currentPlayer = (currentPlayer == 'X') ? 'O' : 'X'; + } } - +} +int main(){ + playGame(); return 0; } From 0d188261b2ad94bb5efa2a48e553496662ef42b1 Mon Sep 17 00:00:00 2001 From: Ariana Ginju Date: Tue, 30 Jan 2024 17:56:28 +0000 Subject: [PATCH 26/40] refactoring: using const --- src/main/c/GameTic_Tac_Toe/game.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main/c/GameTic_Tac_Toe/game.c b/src/main/c/GameTic_Tac_Toe/game.c index f193efa..f20e298 100644 --- a/src/main/c/GameTic_Tac_Toe/game.c +++ b/src/main/c/GameTic_Tac_Toe/game.c @@ -43,9 +43,11 @@ char checkWinner() { } // Funktion zur Überprüfung, ob das Spiel unentschieden ist +const int BOARD_SIZE = 3; + int isBoardFull() { - for (int i = 0; i < 3; i++) { - for (int j = 0; j < 3; j++) { + for (int i = 0; i < BOARD_SIZE; i++) { + for (int j = 0; j < BOARD_SIZE; j++) { if (board[i][j] != 'X' && board[i][j] != 'O') { return 0; } From f0480af37136c2200c5e0486ef55e54a1336f045 Mon Sep 17 00:00:00 2001 From: Ariana Ginju Date: Tue, 30 Jan 2024 18:04:27 +0000 Subject: [PATCH 27/40] refactoring: Clear condition in makeMove --- src/main/c/GameTic_Tac_Toe/game.c | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/src/main/c/GameTic_Tac_Toe/game.c b/src/main/c/GameTic_Tac_Toe/game.c index f20e298..ece4bb7 100644 --- a/src/main/c/GameTic_Tac_Toe/game.c +++ b/src/main/c/GameTic_Tac_Toe/game.c @@ -62,22 +62,28 @@ char switchPlayer(char currentPlayer) { return (currentPlayer == 'X') ? 'O' : 'X'; } +int isValidMove(int choice) { + int row = (choice - 1) / BOARD_SIZE; + int col = (choice - 1) % BOARD_SIZE; + + return (choice >= 1 && choice <= BOARD_SIZE * BOARD_SIZE && board[row][col] != 'X' && board[row][col] != 'O'); +} + void makeMove() { int choice; - printf("Spieler %c, wähle eine Zahl (1-9): ", currentPlayer); + printf("Spieler %c, waehle eine Zahl (1-9): ", currentPlayer); scanf("%d", &choice); // Konvertiere die Zahl in Zeilen- und Spaltenindex - int row = (choice - 1) / 3; - int col = (choice - 1) % 3; - - // Überprüfe, ob das gewählte Feld gültig ist - if (choice < 1 || choice > 9 || board[row][col] == 'X' || board[row][col] == 'O') { - printf("Ungültiger Zug! Bitte wähle erneut.\n"); - makeMove(); // Rekursiver Aufruf, bis ein gültiger Zug gemacht wird - } else { - board[row][col] = currentPlayer; - currentPlayer = switchPlayer(currentPlayer); +if (isValidMove(choice)) { + int row = (choice - 1) / / BOARD_SIZE; + int col = (choice - 1) % % BOARD_SIZE; + board[row][col] = currentPlayer; + currentPlayer = switchPlayer(currentPlayer); + +} else { + printf("Ungueltiger Zug! Bitte waehle erneut.\n"); + makeMove(); // Rekursiver Aufruf, bis ein gültiger Zug gemacht wird } } From 0221526e8e45ec1686d295f40d8660626af09bac Mon Sep 17 00:00:00 2001 From: Ariana Ginju Date: Tue, 30 Jan 2024 18:07:39 +0000 Subject: [PATCH 28/40] Update: corrected errors --- src/main/c/GameTic_Tac_Toe/game.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/c/GameTic_Tac_Toe/game.c b/src/main/c/GameTic_Tac_Toe/game.c index ece4bb7..e56e55f 100644 --- a/src/main/c/GameTic_Tac_Toe/game.c +++ b/src/main/c/GameTic_Tac_Toe/game.c @@ -76,8 +76,8 @@ void makeMove() { // Konvertiere die Zahl in Zeilen- und Spaltenindex if (isValidMove(choice)) { - int row = (choice - 1) / / BOARD_SIZE; - int col = (choice - 1) % % BOARD_SIZE; + int row = (choice - 1) / BOARD_SIZE; + int col = (choice - 1) % BOARD_SIZE; board[row][col] = currentPlayer; currentPlayer = switchPlayer(currentPlayer); From b690c7e673ae766b2cdc4f5f639fae28436f7e53 Mon Sep 17 00:00:00 2001 From: Ariana Ginju Date: Tue, 30 Jan 2024 18:14:59 +0000 Subject: [PATCH 29/40] refactoring: Using const for game symbol constants --- src/main/c/GameTic_Tac_Toe/game.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/c/GameTic_Tac_Toe/game.c b/src/main/c/GameTic_Tac_Toe/game.c index e56e55f..ec98aec 100644 --- a/src/main/c/GameTic_Tac_Toe/game.c +++ b/src/main/c/GameTic_Tac_Toe/game.c @@ -4,7 +4,8 @@ char board[3][3] = {{'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'}}; char currentPlayer = 'X'; - +const char PLAYER_X = 'X'; +const char PLAYER_O = 'O'; // Funktionen zur Anzeige des Spielbretts void displayBoard() { printf("Tic-Tac-Toe\n"); From 0721be55c82ee5cbbfe631cf08501bd091df9d6b Mon Sep 17 00:00:00 2001 From: Ariana Ginju Date: Tue, 30 Jan 2024 18:16:46 +0000 Subject: [PATCH 30/40] refactoring: Removing the currentPlayer global variable --- src/main/c/GameTic_Tac_Toe/game.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/c/GameTic_Tac_Toe/game.c b/src/main/c/GameTic_Tac_Toe/game.c index ec98aec..8615cb8 100644 --- a/src/main/c/GameTic_Tac_Toe/game.c +++ b/src/main/c/GameTic_Tac_Toe/game.c @@ -60,7 +60,7 @@ int isBoardFull() { // Funktion zum Zug eines Spielers char switchPlayer(char currentPlayer) { - return (currentPlayer == 'X') ? 'O' : 'X'; + return (currentPlayer == PLAYER_X) ? PLAYER_O : PLAYER_X; } int isValidMove(int choice) { From dff9fd6ec9fdcd792ff6f2628a11a7e213fcd57a Mon Sep 17 00:00:00 2001 From: Ariana Ginju Date: Tue, 30 Jan 2024 18:20:35 +0000 Subject: [PATCH 31/40] refactoring: Using a loop --- src/main/c/GameTic_Tac_Toe/game.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/main/c/GameTic_Tac_Toe/game.c b/src/main/c/GameTic_Tac_Toe/game.c index 8615cb8..177acc8 100644 --- a/src/main/c/GameTic_Tac_Toe/game.c +++ b/src/main/c/GameTic_Tac_Toe/game.c @@ -6,16 +6,22 @@ char board[3][3] = {{'1', '2', '3'}, char currentPlayer = 'X'; const char PLAYER_X = 'X'; const char PLAYER_O = 'O'; + // Funktionen zur Anzeige des Spielbretts void displayBoard() { printf("Tic-Tac-Toe\n"); - for (int i = 0; i < 3; i++) { - for (int j = 0; j < 3; j++) { + for (int i = 0; i < BOARD_SIZE; i++) { + for (int j = 0; j < BOARD_SIZE; j++) { printf("%c", board[i][j]); - if (j < 2) printf(" | "); - } + if (j < BOARD_SIZE - 1) printf(" | "); + } printf("\n"); - if (i < 2) printf("---------\n"); + if (i < BOARD_SIZE - 1) { + for (int k = 0; k < BOARD_SIZE * 4 - 1; k++) { + printf("-"); + } + printf("\n"); + } } } char checkLine(char a, char b, char c) { From 6d1d96c4d9732867d1f69a2ebca2af38fe51cd3c Mon Sep 17 00:00:00 2001 From: Ariana Ginju Date: Tue, 30 Jan 2024 18:35:42 +0000 Subject: [PATCH 32/40] refactoring: char replaced with enum --- src/main/c/GameTic_Tac_Toe/game.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main/c/GameTic_Tac_Toe/game.c b/src/main/c/GameTic_Tac_Toe/game.c index 177acc8..ea95618 100644 --- a/src/main/c/GameTic_Tac_Toe/game.c +++ b/src/main/c/GameTic_Tac_Toe/game.c @@ -24,14 +24,16 @@ void displayBoard() { } } } + char checkLine(char a, char b, char c) { if (a == b && b == c) return a; return ' '; } + // Funktion zur Überprüfung des Gewinners char checkWinner() { // Überprüfe horizontale und vertikale Linien - for (int i = 0; i < 3; i++) { + for (int i = 0; i < BOARD_SIZE; i++) { char horizontalWinner = checkLine(board[i][0], board[i][1], board[i][2]); char verticalWinner = checkLine(board[0][i], board[1][i], board[2][i]); @@ -65,7 +67,7 @@ int isBoardFull() { // Funktion zum Zug eines Spielers -char switchPlayer(char currentPlayer) { +enum switchPlayer(enum currentPlayer) { return (currentPlayer == PLAYER_X) ? PLAYER_O : PLAYER_X; } From 51aff089d80018462a17f45565432ff16bf07f76 Mon Sep 17 00:00:00 2001 From: Ariana Ginju Date: Tue, 30 Jan 2024 18:44:21 +0000 Subject: [PATCH 33/40] refactoring: replacing larger parts --- src/main/c/GameTic_Tac_Toe/game.c | 35 ++++++++++++++++--------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/src/main/c/GameTic_Tac_Toe/game.c b/src/main/c/GameTic_Tac_Toe/game.c index ea95618..3b7f017 100644 --- a/src/main/c/GameTic_Tac_Toe/game.c +++ b/src/main/c/GameTic_Tac_Toe/game.c @@ -80,14 +80,14 @@ int isValidMove(int choice) { void makeMove() { int choice; - printf("Spieler %c, waehle eine Zahl (1-9): ", currentPlayer); + printf("Spieler %c, waehle eine Zahl (1-9): ", currentPlayer == PLAYER_X) ? 'X' : 'O'); scanf("%d", &choice); // Konvertiere die Zahl in Zeilen- und Spaltenindex if (isValidMove(choice)) { int row = (choice - 1) / BOARD_SIZE; int col = (choice - 1) % BOARD_SIZE; - board[row][col] = currentPlayer; + board[row][col] = currentPlayer == PLAYER_X) ? 'X' : 'O'; currentPlayer = switchPlayer(currentPlayer); } else { @@ -96,25 +96,26 @@ if (isValidMove(choice)) { } } +int isGameOver() { + char winner = checkWinner(); + return (winner == PLAYER_X || winner == PLAYER_O || isBoardFull()); +} + +void printGameResult(char winner) { + if (winner == PLAYER_X || winner == PLAYER_O) { + printf("Spieler %c gewinnt!\n", (winner == PLAYER_X) ? 'X' : 'O'); + } else { + printf("Unentschieden!\n"); + } +} + void playGame() { - while (1) { + while (!isGameOver()) { displayBoard(); makeMove(); - - char winner = checkWinner(); - if (winner == 'X' || winner == 'O') { - displayBoard(); - printf("Spieler %c gewinnt!\n", winner); - break; - } - - if (isBoardFull()) { - displayBoard(); - printf("Unentschieden!\n"); - break; - } } -} + + //Fehlt etwas, änderung folgt; int main(){ playGame(); return 0; From 3989ecb68131b39183b56acdd5f9ba06e24f0cef Mon Sep 17 00:00:00 2001 From: Ariana Ginju Date: Tue, 30 Jan 2024 18:45:51 +0000 Subject: [PATCH 34/40] refactoring: added missing part --- src/main/c/GameTic_Tac_Toe/game.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/c/GameTic_Tac_Toe/game.c b/src/main/c/GameTic_Tac_Toe/game.c index 3b7f017..f72b814 100644 --- a/src/main/c/GameTic_Tac_Toe/game.c +++ b/src/main/c/GameTic_Tac_Toe/game.c @@ -115,7 +115,11 @@ void playGame() { makeMove(); } - //Fehlt etwas, änderung folgt; + displayBoard(); + char winner = checkWinner(); + printGameResult(winner); +} + int main(){ playGame(); return 0; From 4cdef01b3a4c00d51b701701ed1780c878177253 Mon Sep 17 00:00:00 2001 From: Ariana Ginju Date: Tue, 30 Jan 2024 19:14:53 +0000 Subject: [PATCH 35/40] refactoring: correct the errors --- src/main/c/GameTic_Tac_Toe/game.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/main/c/GameTic_Tac_Toe/game.c b/src/main/c/GameTic_Tac_Toe/game.c index f72b814..443fe8d 100644 --- a/src/main/c/GameTic_Tac_Toe/game.c +++ b/src/main/c/GameTic_Tac_Toe/game.c @@ -1,5 +1,6 @@ #include +const int BOARD_SIZE = 3; char board[3][3] = {{'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'}}; @@ -52,8 +53,6 @@ char checkWinner() { } // Funktion zur Überprüfung, ob das Spiel unentschieden ist -const int BOARD_SIZE = 3; - int isBoardFull() { for (int i = 0; i < BOARD_SIZE; i++) { for (int j = 0; j < BOARD_SIZE; j++) { @@ -67,7 +66,7 @@ int isBoardFull() { // Funktion zum Zug eines Spielers -enum switchPlayer(enum currentPlayer) { +char switchPlayer(char currentPlayer) { return (currentPlayer == PLAYER_X) ? PLAYER_O : PLAYER_X; } @@ -80,14 +79,14 @@ int isValidMove(int choice) { void makeMove() { int choice; - printf("Spieler %c, waehle eine Zahl (1-9): ", currentPlayer == PLAYER_X) ? 'X' : 'O'); + printf("Spieler %c, waehle eine Zahl (1-9): ", (currentPlayer == PLAYER_X) ? 'X' : 'O'); scanf("%d", &choice); // Konvertiere die Zahl in Zeilen- und Spaltenindex if (isValidMove(choice)) { int row = (choice - 1) / BOARD_SIZE; int col = (choice - 1) % BOARD_SIZE; - board[row][col] = currentPlayer == PLAYER_X) ? 'X' : 'O'; + board[row][col] = (currentPlayer == PLAYER_X) ? 'X' : 'O'; currentPlayer = switchPlayer(currentPlayer); } else { From 20e5561526c78d54b721eef846afba80e411298e Mon Sep 17 00:00:00 2001 From: Ariana Ginju Date: Wed, 31 Jan 2024 12:23:59 +0000 Subject: [PATCH 36/40] Add new directory --- test/tests_tictactoe.game/.gitkeep | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 test/tests_tictactoe.game/.gitkeep diff --git a/test/tests_tictactoe.game/.gitkeep b/test/tests_tictactoe.game/.gitkeep new file mode 100644 index 0000000..e69de29 From a1de4d4bb84dbd5a60038e1bbf0948729656be0c Mon Sep 17 00:00:00 2001 From: Ariana Ginju Date: Wed, 31 Jan 2024 12:27:00 +0000 Subject: [PATCH 37/40] Delete .gitkeep --- test/tests_tictactoe.game/.gitkeep | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 test/tests_tictactoe.game/.gitkeep diff --git a/test/tests_tictactoe.game/.gitkeep b/test/tests_tictactoe.game/.gitkeep deleted file mode 100644 index e69de29..0000000 From f756c806111db52ec747236f56681fff766dec18 Mon Sep 17 00:00:00 2001 From: Ariana Ginju Date: Wed, 31 Jan 2024 12:28:08 +0000 Subject: [PATCH 38/40] Add new file --- test/test_tictactoe.c | 1 + 1 file changed, 1 insertion(+) create mode 100644 test/test_tictactoe.c diff --git a/test/test_tictactoe.c b/test/test_tictactoe.c new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/test/test_tictactoe.c @@ -0,0 +1 @@ + From fc3efd1fec3871fba2a9a39ef41c5a0aa1314cb1 Mon Sep 17 00:00:00 2001 From: fdai7775 Date: Fri, 2 Feb 2024 10:12:17 +0000 Subject: [PATCH 39/40] Add new file tictactoe.h --- src/main/c/GameTic_Tac_Toe/tictactoe.h | 1 + 1 file changed, 1 insertion(+) create mode 100644 src/main/c/GameTic_Tac_Toe/tictactoe.h diff --git a/src/main/c/GameTic_Tac_Toe/tictactoe.h b/src/main/c/GameTic_Tac_Toe/tictactoe.h new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/src/main/c/GameTic_Tac_Toe/tictactoe.h @@ -0,0 +1 @@ + From 83ca5781232e1dfd702390a1dda2e51ac1ec3a2e Mon Sep 17 00:00:00 2001 From: fdai7775 Date: Fri, 2 Feb 2024 10:14:46 +0000 Subject: [PATCH 40/40] Update name game.c --- src/main/c/GameTic_Tac_Toe/{game.c => tictactoe.c} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/main/c/GameTic_Tac_Toe/{game.c => tictactoe.c} (100%) diff --git a/src/main/c/GameTic_Tac_Toe/game.c b/src/main/c/GameTic_Tac_Toe/tictactoe.c similarity index 100% rename from src/main/c/GameTic_Tac_Toe/game.c rename to src/main/c/GameTic_Tac_Toe/tictactoe.c