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() { void makeMove() {
int choice; 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); scanf("%d", &choice);
// Konvertiere die Zahl in Zeilen- und Spaltenindex // Konvertiere die Zahl in Zeilen- und Spaltenindex
if (isValidMove(choice)) { if (isValidMove(choice)) {
int row = (choice - 1) / BOARD_SIZE; int row = (choice - 1) / BOARD_SIZE;
int col = (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); currentPlayer = switchPlayer(currentPlayer);
} else { } 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() { void playGame() {
while (1) {
while (!isGameOver()) {
displayBoard(); displayBoard();
makeMove(); 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(){ int main(){
playGame(); playGame();
return 0; return 0;

Loading…
Cancel
Save