#include #include "TicTacToe.h" //Funktionen deklarieren // Funktion zum Zeichnen der Tafel void drawBoard(int pos1, int pos2, int pos3, int pos4, int pos5, int pos6, int pos7, int pos8, int pos9); // Funktion zum Wechseln des aktuellen Spielers int switchPlayer(int currentPlayer); // Funktion, um zu prüfen, ob es einen Gewinner gibt int checkForWin(int pos1, int pos2, int pos3, int pos4, int pos5, int pos6, int pos7, int pos8, int pos9, int currentPlayer); // Funktion zur Anzeige von Nachrichten void messages(int message, char player); int run_TicTacToe() { messages(99, '\0'); int pos1 = 1; int pos2 = 2; int pos3 = 3; int pos4 = 4; int pos5 = 5; int pos6 = 6; int pos7 = 7; int pos8 = 8; int pos9 = 9; int player = 10; drawBoard(pos1, pos2, pos3, pos4, pos5, pos6, pos7, pos8, pos9); // Spielschleife for (int i = 0; i < 9; i++) { int in; messages(4, (player == 10) ? 'X' : 'O'); scanf("%d", &in); printf("\n"); if (pos1 == in) { pos1 = player; } else if (pos2 == in) { pos2 = player; } else if (pos3 == in) { pos3 = player; } else if (pos4 == in) { pos4 = player; } else if (pos5 == in) { pos5 = player; } else if (pos6 == in) { pos6 = player; } else if (pos7 == in) { pos7 = player; } else if (pos8 == in) { pos8 = player; } else if (pos9 == in) { pos9 = player; } else { messages(0, (player == 10) ? 'X' : 'O'); i--; // wiederholen continue; } drawBoard(pos1, pos2, pos3, pos4, pos5, pos6, pos7, pos8, pos9); if (checkForWin(pos1, pos2, pos3, pos4, pos5, pos6, pos7, pos8, pos9, player)) { messages(player, '\0'); // Winner break; } if (i == 8) { messages(3, '\0'); // Tie } player = switchPlayer(player); } return 0; } void drawBoard(int pos1, int pos2, int pos3, int pos4, int pos5, int pos6, int pos7, int pos8, int pos9) { char vertical = '|'; char horizontal[] = "---"; printf("%c%s%c%s%c%s%c\n", vertical, horizontal, vertical, horizontal, vertical, horizontal, vertical); printf("%c", vertical); if (pos1 == 10) { printf(" X "); } else if (pos1 == 11) { printf(" O "); } else { printf(" %d ",pos1); } printf("%c", vertical); if (pos2 == 10) { printf(" X "); } else if (pos2 == 11) { printf(" O "); } else { printf(" %d ",pos2); } printf("%c", vertical); if (pos3 == 10) { printf(" X "); } else if (pos3 == 11) { printf(" O "); } else { printf(" %d ",pos3); } printf("%c\n", vertical); printf("%c%s%c%s%c%s%c\n", vertical, horizontal, vertical, horizontal, vertical, horizontal, vertical); printf("%c", vertical); if (pos4 == 10) { printf(" X "); } else if (pos4 == 11) { printf(" O "); } else { printf(" %d ",pos4); } printf("%c", vertical); if (pos5 == 10) { printf(" X "); } else if (pos5 == 11) { printf(" O "); } else { printf(" %d ",pos5); } printf("%c", vertical); if (pos6 == 10) { printf(" X "); } else if (pos6 == 11) { printf(" O "); } else { printf(" %d ",pos6); } printf("%c\n", vertical); printf("%c%s%c%s%c%s%c\n", vertical, horizontal, vertical, horizontal, vertical, horizontal, vertical); printf("%c", vertical); if (pos7 == 10) { printf(" X "); } else if (pos7 == 11) { printf(" O "); } else { printf(" %d ",pos7); } printf("%c", vertical); if (pos8 == 10) { printf(" X "); } else if (pos8 == 11) { printf(" O "); } else { printf(" %d ",pos8); } printf("%c", vertical); if (pos9 == 10) { printf(" X "); } else if (pos9 == 11) { printf(" O "); } else { printf(" %d ",pos9); } printf("%c\n", vertical); printf("%c%s%c%s%c%s%c\n", vertical, horizontal, vertical, horizontal, vertical, horizontal, vertical); } int switchPlayer(int player) { if (player == 10) { return 11; } else { return 10; } } int checkForWin(int pos1, int pos2, int pos3, int pos4, int pos5, int pos6, int pos7, int pos8, int pos9, int player) { // Prüfung auf Gewinn if (pos1 == player && pos2 == player && pos3 == player) { return 1; } else if (pos4 == player && pos5 == player && pos6 == player) { return 1; } else if (pos7 == player && pos8 == player && pos9 == player) { return 1; } else if (pos1 == player && pos4 == player && pos7 == player) { return 1; } else if (pos2 == player && pos5 == player && pos8 == player) { return 1; } else if (pos3 == player && pos6 == player && pos9 == player) { return 1; } else if (pos1 == player && pos5 == player && pos9 == player) { return 1; } else if (pos3 == player && pos5 == player && pos7 == player) { return 1; } else { return 0; } } void messages(int message, char player) { switch (message) { case 0: printf("\nInvalid input. Please try again.\n"); break; case 10: printf("\nPlayer X has won!\n\n"); break; case 11: printf("\nPlayer O has won!\n\n"); break; case 3: printf("\nIt's a draw. Good game!\n\n"); break; case 4: printf("\nPlayer %c, enter your move (1-9): ", player); break; case 99: printf("\n\nWelcome to the TIC TAC TOE game!\n\nINSTRUCTIONS:\n"); printf("Players take turns entering a number from 1 to 9 to mark their position on the board.\n"); printf("Player 'X' begins first, and player 'O' second.\n"); printf("The board looks like this: Start!\n\n"); break; default: break; } }