From 22ce5b5fc45d3de430691a3439044bfb96a2d4f7 Mon Sep 17 00:00:00 2001 From: fdai7775 Date: Tue, 30 Jan 2024 11:52:41 +0000 Subject: [PATCH 1/9] 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 2/9] 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 3/9] 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 4/9] 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 5/9] 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 6/9] 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 7/9] 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 8/9] 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 9/9] 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; + }