From b9cc942a3b82de120c548bb980f1cb4abd5f01fe Mon Sep 17 00:00:00 2001 From: fdai7775 Date: Wed, 24 Jan 2024 12:36:32 +0000 Subject: [PATCH 01/40] Add new directory --- 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 ebfdb2133a457dcc45f56d980572263c0ca30a12 Mon Sep 17 00:00:00 2001 From: fdai7775 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 376d864b3a65658c1f6c50b5e182cbb9d365cf7a Mon Sep 17 00:00:00 2001 From: fdai7775 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 b50761f3182096294c5a2ad328b0e5d25ac548fd 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 8957916a23fb9ac4b6548b6942b421cc5c0dad24 Mon Sep 17 00:00:00 2001 From: fdai7775 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 a2b296651064ff9c44370ae4494e2bbb0b4a5e7e Mon Sep 17 00:00:00 2001 From: fdai7775 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 2d7b1b0e8cda5029a6694ca40002147c4b37f86d Mon Sep 17 00:00:00 2001 From: fdai7775 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 00ee1fdb18c618c51a313b7a0faa1a1294286c34 Mon Sep 17 00:00:00 2001 From: fdai7775 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 22ce5b5fc45d3de430691a3439044bfb96a2d4f7 Mon Sep 17 00:00:00 2001 From: fdai7775 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 f770bfd5d647d60b268f0a267b006343b50c2aaa Mon Sep 17 00:00:00 2001 From: fdai7775 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 d75ff5cdbf9d229e47f300ecee996e83ec902ae4 Mon Sep 17 00:00:00 2001 From: fdai7775 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 95c97636763ff420483d77a59bf813e4fac5b54d Mon Sep 17 00:00:00 2001 From: fdai7775 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 c0df50c14754154612ecd07674579ea5abdeb251 Mon Sep 17 00:00:00 2001 From: fdai7775 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 68f804befc16b175915ddd7c4aa066c3b5667d32 Mon Sep 17 00:00:00 2001 From: fdai7775 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 57ff8f28ae42deb751d92346429a7eb54dee673d Mon Sep 17 00:00:00 2001 From: fdai7775 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 e99a489aa351de1759d2f54cc771860fb8a7e88b Mon Sep 17 00:00:00 2001 From: fdai7775 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 097e26462eaec31300dc48366252ef1f57ebbd12 Mon Sep 17 00:00:00 2001 From: fdai7775 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 c4c5255c32e69b0b45164e5fe6098ead52f76bcb Mon Sep 17 00:00:00 2001 From: fdai7775 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 44325c521fed24d036f30b85d670ff9f5af962e8 Mon Sep 17 00:00:00 2001 From: fdai7775 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 eb9a2755fe911b7b7760353dc20384acbea304d5 Mon Sep 17 00:00:00 2001 From: fdai7775 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 5567f10afcad179c0a7def3929b264a963b207a5 Mon Sep 17 00:00:00 2001 From: fdai7775 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 4af8ceda5d461297fa85ba5c0bf158a4874f1090 Mon Sep 17 00:00:00 2001 From: fdai7775 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 ad6d3bbdcfe3e7aad435dee8e30e4bcfb7ff818a Mon Sep 17 00:00:00 2001 From: fdai7775 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 a6c12ae026290794ac6649cdae8e9da72d85f285 Mon Sep 17 00:00:00 2001 From: fdai7775 Date: Tue, 30 Jan 2024 17:43:49 +0000 Subject: [PATCH 24/40] refactoring: logic functions for player exchange --- 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 6e55a92f3ee10ff8330579b75aec2beff2e2b58d Mon Sep 17 00:00:00 2001 From: fdai7775 Date: Tue, 30 Jan 2024 17:53:55 +0000 Subject: [PATCH 25/40] refactoring: separation of the game loop into functions --- 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 36d75c111cbcc1a583de5762d5d8ef4b44f708c1 Mon Sep 17 00:00:00 2001 From: fdai7775 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 6c7fc28dc481897bcb97ca729968930eb764239c Mon Sep 17 00:00:00 2001 From: fdai7775 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 18a2eeaa64286be27a672a14db8e5d93166e5d17 Mon Sep 17 00:00:00 2001 From: fdai7775 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 1abf73507ee06529b853a80e64fbae52006217dc Mon Sep 17 00:00:00 2001 From: fdai7775 Date: Tue, 30 Jan 2024 18:14:59 +0000 Subject: [PATCH 30/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 98533bbc98c2651716fbe3ed42f4f902a0cfb337 Mon Sep 17 00:00:00 2001 From: fdai7775 Date: Tue, 30 Jan 2024 18:16:46 +0000 Subject: [PATCH 31/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 6a843132cff3f0493108d4e31870d35d49c0f615 Mon Sep 17 00:00:00 2001 From: fdai7775 Date: Tue, 30 Jan 2024 18:20:35 +0000 Subject: [PATCH 32/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 6956db884ccb4754d4f78c00e834035ca2244b8d Mon Sep 17 00:00:00 2001 From: fdai7775 Date: Tue, 30 Jan 2024 18:35:42 +0000 Subject: [PATCH 33/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 2ce9e44041607a5540c09bd2e23d749023f89b9b Mon Sep 17 00:00:00 2001 From: fdai7775 Date: Tue, 30 Jan 2024 18:44:21 +0000 Subject: [PATCH 34/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 673de7e2e293f3540618bd7bdec3b39ec5fd28c6 Mon Sep 17 00:00:00 2001 From: fdai7775 Date: Tue, 30 Jan 2024 18:45:51 +0000 Subject: [PATCH 35/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 b12449338944ee154d3da893562a62fd897012e8 Mon Sep 17 00:00:00 2001 From: fdai7775 Date: Tue, 30 Jan 2024 19:14:53 +0000 Subject: [PATCH 36/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 66b03b0a20621e2660b7f2c9077a7023d8ce5da4 Mon Sep 17 00:00:00 2001 From: Ariana Ginju Date: Wed, 31 Jan 2024 12:23:59 +0000 Subject: [PATCH 37/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 5e74794e40fd23875ccb0ff5b9056fd47fe2c236 Mon Sep 17 00:00:00 2001 From: Ariana Ginju Date: Wed, 31 Jan 2024 12:27:00 +0000 Subject: [PATCH 38/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 d6dd92dc1e6a83cac81770d4236ab31892ad02b1 Mon Sep 17 00:00:00 2001 From: Ariana Ginju Date: Wed, 31 Jan 2024 12:28:08 +0000 Subject: [PATCH 39/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 @@ +