|
|
@ -4,27 +4,36 @@ 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"); |
|
|
|
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) { |
|
|
|
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]); |
|
|
|
|
|
|
@ -43,9 +52,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; |
|
|
|
} |
|
|
@ -56,50 +67,60 @@ int isBoardFull() { |
|
|
|
|
|
|
|
|
|
|
|
// Funktion zum Zug eines Spielers |
|
|
|
char switchPlayer(char currentPlayer) { |
|
|
|
return (currentPlayer == 'X') ? 'O' : 'X'; |
|
|
|
enum switchPlayer(enum currentPlayer) { |
|
|
|
return (currentPlayer == PLAYER_X) ? PLAYER_O : PLAYER_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 == PLAYER_X) ? 'X' : 'O'); |
|
|
|
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 == 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 |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
int main() { |
|
|
|
while (1) { |
|
|
|
displayBoard(); |
|
|
|
makeMove(); |
|
|
|
|
|
|
|
char winner = checkWinner(); |
|
|
|
if (winner == 'X' || winner == 'O') { |
|
|
|
displayBoard(); |
|
|
|
printf("Spieler %c gewinnt!\n", winner); |
|
|
|
break; |
|
|
|
} |
|
|
|
int isGameOver() { |
|
|
|
char winner = checkWinner(); |
|
|
|
return (winner == PLAYER_X || winner == PLAYER_O || isBoardFull()); |
|
|
|
} |
|
|
|
|
|
|
|
if (isBoardFull()) { |
|
|
|
displayBoard(); |
|
|
|
printf("Unentschieden!\n"); |
|
|
|
break; |
|
|
|
} |
|
|
|
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"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// Wechsle den Spieler |
|
|
|
currentPlayer = (currentPlayer == 'X') ? 'O' : 'X'; |
|
|
|
void playGame() { |
|
|
|
while (!isGameOver()) { |
|
|
|
displayBoard(); |
|
|
|
makeMove(); |
|
|
|
} |
|
|
|
|
|
|
|
displayBoard(); |
|
|
|
char winner = checkWinner(); |
|
|
|
printGameResult(winner); |
|
|
|
} |
|
|
|
|
|
|
|
int main(){ |
|
|
|
playGame(); |
|
|
|
return 0; |
|
|
|
} |