You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

223 lines
6.1 KiB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
  1. #include <stdio.h>
  2. #include "TicTacToe.h"
  3. //Funktionen deklarieren
  4. // Funktion zum Zeichnen der Tafel
  5. void drawBoard(int pos1, int pos2, int pos3, int pos4, int pos5, int pos6, int pos7, int pos8, int pos9);
  6. // Funktion zum Wechseln des aktuellen Spielers
  7. int switchPlayer(int currentPlayer);
  8. // Funktion, um zu prüfen, ob es einen Gewinner gibt
  9. int checkForWin(int pos1, int pos2, int pos3, int pos4, int pos5, int pos6, int pos7, int pos8, int pos9, int currentPlayer);
  10. // Funktion zur Anzeige von Nachrichten
  11. void messages(int message, char player);
  12. int main() {
  13. messages(99, '\0');
  14. int pos1 = 1;
  15. int pos2 = 2;
  16. int pos3 = 3;
  17. int pos4 = 4;
  18. int pos5 = 5;
  19. int pos6 = 6;
  20. int pos7 = 7;
  21. int pos8 = 8;
  22. int pos9 = 9;
  23. int player = 10;
  24. drawBoard(pos1, pos2, pos3, pos4, pos5, pos6, pos7, pos8, pos9);
  25. // Spielschleife
  26. for (int i = 0; i < 9; i++) {
  27. int in;
  28. messages(4, (player == 10) ? 'X' : 'O');
  29. scanf("%d", &in);
  30. printf("\n");
  31. if (pos1 == in) {
  32. pos1 = player;
  33. } else if (pos2 == in) {
  34. pos2 = player;
  35. } else if (pos3 == in) {
  36. pos3 = player;
  37. } else if (pos4 == in) {
  38. pos4 = player;
  39. } else if (pos5 == in) {
  40. pos5 = player;
  41. } else if (pos6 == in) {
  42. pos6 = player;
  43. } else if (pos7 == in) {
  44. pos7 = player;
  45. } else if (pos8 == in) {
  46. pos8 = player;
  47. } else if (pos9 == in) {
  48. pos9 = player;
  49. } else {
  50. messages(0, (player == 10) ? 'X' : 'O');
  51. i--; // wiederholen
  52. continue;
  53. }
  54. drawBoard(pos1, pos2, pos3, pos4, pos5, pos6, pos7, pos8, pos9);
  55. if (checkForWin(pos1, pos2, pos3, pos4, pos5, pos6, pos7, pos8, pos9, player)) {
  56. messages(player, '\0'); // Winner
  57. break;
  58. }
  59. if (i == 8) {
  60. messages(3, '\0'); // Tie
  61. }
  62. player = switchPlayer(player);
  63. }
  64. return 0;
  65. }
  66. void drawBoard(int pos1, int pos2, int pos3, int pos4, int pos5, int pos6, int pos7, int pos8, int pos9) {
  67. char vertical = '|';
  68. char horizontal[] = "---";
  69. printf("%c%s%c%s%c%s%c\n", vertical, horizontal, vertical, horizontal, vertical, horizontal, vertical);
  70. printf("%c", vertical);
  71. if (pos1 == 10) {
  72. printf(" X ");
  73. } else if (pos1 == 11) {
  74. printf(" O ");
  75. } else {
  76. printf(" %d ",pos1);
  77. }
  78. printf("%c", vertical);
  79. if (pos2 == 10) {
  80. printf(" X ");
  81. } else if (pos2 == 11) {
  82. printf(" O ");
  83. } else {
  84. printf(" %d ",pos2);
  85. }
  86. printf("%c", vertical);
  87. if (pos3 == 10) {
  88. printf(" X ");
  89. } else if (pos3 == 11) {
  90. printf(" O ");
  91. } else {
  92. printf(" %d ",pos3);
  93. }
  94. printf("%c\n", vertical);
  95. printf("%c%s%c%s%c%s%c\n", vertical, horizontal, vertical, horizontal, vertical, horizontal, vertical);
  96. printf("%c", vertical);
  97. if (pos4 == 10) {
  98. printf(" X ");
  99. } else if (pos4 == 11) {
  100. printf(" O ");
  101. } else {
  102. printf(" %d ",pos4);
  103. }
  104. printf("%c", vertical);
  105. if (pos5 == 10) {
  106. printf(" X ");
  107. } else if (pos5 == 11) {
  108. printf(" O ");
  109. } else {
  110. printf(" %d ",pos5);
  111. }
  112. printf("%c", vertical);
  113. if (pos6 == 10) {
  114. printf(" X ");
  115. } else if (pos6 == 11) {
  116. printf(" O ");
  117. } else {
  118. printf(" %d ",pos6);
  119. }
  120. printf("%c\n", vertical);
  121. printf("%c%s%c%s%c%s%c\n", vertical, horizontal, vertical, horizontal, vertical, horizontal, vertical);
  122. printf("%c", vertical);
  123. if (pos7 == 10) {
  124. printf(" X ");
  125. } else if (pos7 == 11) {
  126. printf(" O ");
  127. } else {
  128. printf(" %d ",pos7);
  129. }
  130. printf("%c", vertical);
  131. if (pos8 == 10) {
  132. printf(" X ");
  133. } else if (pos8 == 11) {
  134. printf(" O ");
  135. } else {
  136. printf(" %d ",pos8);
  137. }
  138. printf("%c", vertical);
  139. if (pos9 == 10) {
  140. printf(" X ");
  141. } else if (pos9 == 11) {
  142. printf(" O ");
  143. } else {
  144. printf(" %d ",pos9);
  145. }
  146. printf("%c\n", vertical);
  147. printf("%c%s%c%s%c%s%c\n", vertical, horizontal, vertical, horizontal, vertical, horizontal, vertical);
  148. }
  149. int switchPlayer(int player) {
  150. if (player == 10) {
  151. return 11;
  152. } else {
  153. return 10;
  154. }
  155. }
  156. int checkForWin(int pos1, int pos2, int pos3, int pos4, int pos5, int pos6, int pos7, int pos8, int pos9, int player) {
  157. // Prüfung auf Gewinn
  158. if (pos1 == player && pos2 == player && pos3 == player) {
  159. return 1;
  160. } else if (pos4 == player && pos5 == player && pos6 == player) {
  161. return 1;
  162. } else if (pos7 == player && pos8 == player && pos9 == player) {
  163. return 1;
  164. } else if (pos1 == player && pos4 == player && pos7 == player) {
  165. return 1;
  166. } else if (pos2 == player && pos5 == player && pos8 == player) {
  167. return 1;
  168. } else if (pos3 == player && pos6 == player && pos9 == player) {
  169. return 1;
  170. } else if (pos1 == player && pos5 == player && pos9 == player) {
  171. return 1;
  172. } else if (pos3 == player && pos5 == player && pos7 == player) {
  173. return 1;
  174. } else {
  175. return 0;
  176. }
  177. }
  178. void messages(int message, char player) {
  179. switch (message) {
  180. case 0:
  181. printf("\nInvalid input. Please try again.\n");
  182. break;
  183. case 10:
  184. printf("\nPlayer X has won!\n\n");
  185. break;
  186. case 11:
  187. printf("\nPlayer O has won!\n\n");
  188. break;
  189. case 3:
  190. printf("\nIt's a draw. Good game!\n\n");
  191. break;
  192. case 4:
  193. printf("\nPlayer %c, enter your move (1-9): ", player);
  194. break;
  195. case 99:
  196. printf("\n\nWelcome to the TIC TAC TOE game!\n\nINSTRUCTIONS:\n");
  197. printf("Players take turns entering a number from 1 to 9 to mark their position on the board.\n");
  198. printf("Player 'X' begins first, and player 'O' second.\n");
  199. printf("The board looks like this: Start!\n\n");
  200. break;
  201. default:
  202. break;
  203. }
  204. }