Browse Source

refactoring: replacing larger parts

remotes/origin/Ariana
Ariana Ginju 11 months ago
committed by David Moeller
parent
commit
51aff089d8
  1. 35
      src/main/c/GameTic_Tac_Toe/game.c

35
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;

Loading…
Cancel
Save