From e38696759767f2abca6d52699cd3d3dc30f892c4 Mon Sep 17 00:00:00 2001 From: fdai7775 Date: Fri, 2 Feb 2024 12:45:27 +0000 Subject: [PATCH] refactoring: makeMove --- src/main/c/GameTic_Tac_Toe/tictactoe.c | 32 +++++++++++++++----------- src/main/c/GameTic_Tac_Toe/tictactoe.h | 2 +- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/src/main/c/GameTic_Tac_Toe/tictactoe.c b/src/main/c/GameTic_Tac_Toe/tictactoe.c index 6cab3ec..e7eeee9 100644 --- a/src/main/c/GameTic_Tac_Toe/tictactoe.c +++ b/src/main/c/GameTic_Tac_Toe/tictactoe.c @@ -77,21 +77,18 @@ int isValidMove(int choice) { return (choice >= 1 && choice <= BOARD_SIZE * BOARD_SIZE && board[row][col] != 'X' && board[row][col] != 'O'); } -void makeMove() { - int choice; - printf("Spieler %c, waehle eine Zahl (1-9): ", (currentPlayer == PLAYER_X) ? 'X' : 'O'); - scanf("%d", &choice); +void makeMove(int 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'; - currentPlayer = switchPlayer(currentPlayer); + // 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'; + currentPlayer = switchPlayer(currentPlayer); -} else { - printf("Ungueltiger Zug! Bitte waehle erneut.\n"); - makeMove(); // Rekursiver Aufruf, bis ein gültiger Zug gemacht wird + } else { + printf("Ungueltiger Zug! Bitte waehle erneut.\n"); + makeMove(get_move()); // Rekursiver Aufruf, bis ein gültiger Zug gemacht wird } } @@ -100,6 +97,13 @@ int isGameOver() { return (winner == PLAYER_X || winner == PLAYER_O || isBoardFull()); } +int get_move(){ + int choice; + printf("Spieler %c, waehle eine Zahl (1-9): ", (currentPlayer == PLAYER_X) ? 'X' : 'O'); + scanf("%d", &choice); + return choice; +} + void printGameResult(char winner) { if (winner == PLAYER_X || winner == PLAYER_O) { printf("Spieler %c gewinnt!\n", (winner == PLAYER_X) ? 'X' : 'O'); @@ -111,7 +115,7 @@ void printGameResult(char winner) { void playGame() { while (!isGameOver()) { displayBoard(); - makeMove(); + makeMove(get_move()); } displayBoard(); diff --git a/src/main/c/GameTic_Tac_Toe/tictactoe.h b/src/main/c/GameTic_Tac_Toe/tictactoe.h index b7b4e6f..93c7d14 100644 --- a/src/main/c/GameTic_Tac_Toe/tictactoe.h +++ b/src/main/c/GameTic_Tac_Toe/tictactoe.h @@ -7,6 +7,6 @@ int isValidMove(int choice); char checkLine(char a, char b, char c); void start_tictactoe(); char checkWinner(); -void makeMove(); +void makeMove(int choice); #endif