From 6e55a92f3ee10ff8330579b75aec2beff2e2b58d Mon Sep 17 00:00:00 2001 From: fdai7775 Date: Tue, 30 Jan 2024 17:53:55 +0000 Subject: [PATCH 01/11] 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 02/11] 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 03/11] 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 04/11] 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 06/11] 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 07/11] 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 08/11] 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 09/11] 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 10/11] 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 11/11] 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;