Browse Source

refactoring: makeMove

remotes/origin/Ariana
fdai7775 11 months ago
parent
commit
e386967597
  1. 32
      src/main/c/GameTic_Tac_Toe/tictactoe.c
  2. 2
      src/main/c/GameTic_Tac_Toe/tictactoe.h

32
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();

2
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
Loading…
Cancel
Save