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.

256 lines
6.3 KiB

  1. //Importiere wichtige Bibliotheken
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. //Definiere Feldgröße
  5. #define ROWS 6
  6. #define COLS 7
  7. // Definiere Farben
  8. #define RESET_COLOR "\033[0m"
  9. #define BLACK "\033[0;30m"
  10. #define RED "\033[0;31m"
  11. #define GREEN "\033[0;32m"
  12. #define YELLOW "\033[0;33m"
  13. #define BLUE "\033[0;34m"
  14. #define MAGENTA "\033[0;35m"
  15. #define CYAN "\033[0;36m"
  16. #define WHITE "\033[0;37m"
  17. //Funktionsprototyp für initializeBoard
  18. void initializeBoard(char board[ROWS][COLS]);
  19. //Funktionsprototyp für printBoard
  20. void printBoard(char board[ROWS][COLS]);
  21. // Funktionsprototyp für clearScreen
  22. void clearScreen();
  23. //Funktionsprototyp für isColumnFull
  24. int isColumnFull(char board[ROWS][COLS], int col);
  25. //Funktionsprototyp für dropPiece
  26. int dropPiece(char board[ROWS][COLS], int col, char player);
  27. //Funktionsprototyp für checkWin
  28. int checkWin(char board[ROWS][COLS], char player);
  29. // Funktionsprototyp für checkHorizontal
  30. int checkHorizontal(char board[ROWS][COLS], char player);
  31. // Funktionsprototyp für checkVertical
  32. int checkVertical(char board[ROWS][COLS], char player);
  33. // Funktionsprototyp für checkDiagonalLR
  34. int checkDiagonalLR(char board[ROWS][COLS], char player);
  35. // Funktionsprototyp für checkDiagonalRL
  36. int checkDiagonalRL(char board[ROWS][COLS], char player);
  37. // Funktionsprototyp für showMessage
  38. void showMessage(const char* messageColor, const char* message);
  39. //Funktionsprototyp für showInvalidInputMessage
  40. void showInvalidInputWarning();
  41. //Funktionsprototyp für showColumnFullMessage
  42. void showColumnFullMessage();
  43. //Funktionsprototyp für showWinMessage
  44. void showWinMessage(int player);
  45. //Write starter function
  46. int main_function() {
  47. char board[ROWS][COLS];
  48. int currentPlayer = 1; // Spieler 1 beginnt
  49. initializeBoard(board);
  50. printBoard(board);
  51. int column;
  52. while (1) {
  53. printf(YELLOW"Spieler %d, wähle eine Spalte (1-7): "RESET_COLOR, currentPlayer);
  54. scanf("%d", &column);
  55. if (column < 1 || column > 7) {
  56. showInvalidInputWarning();
  57. continue;
  58. }
  59. column--;
  60. if (isColumnFull(board, column)) {
  61. showColumnFullMessage();
  62. continue;
  63. }
  64. if (dropPiece(board, column, (currentPlayer == 1) ? 'X' : 'O')) {
  65. printBoard(board);
  66. if (checkWin(board, (currentPlayer == 1) ? 'X' : 'O')) {
  67. showWinMessage(currentPlayer);
  68. break;
  69. }
  70. currentPlayer = (currentPlayer == 1) ? 2 : 1;
  71. }
  72. }
  73. return 0;
  74. }
  75. // Write initializeBoard function
  76. void initializeBoard(char board[ROWS][COLS]) {
  77. for (int i = 0; i < ROWS; i++) {
  78. for (int j = 0; j < COLS; j++) {
  79. board[i][j] = ' ';
  80. }
  81. }
  82. }
  83. // Write printBoard function
  84. void printBoard(char board[ROWS][COLS]) {
  85. clearScreen();
  86. printf("\n");
  87. for (int i = 0; i < ROWS; i++) {
  88. for (int j = 0; j < COLS; j++) {
  89. printf("| %c ", board[i][j]);
  90. }
  91. printf("|\n");
  92. for (int j = 0; j < COLS; j++) {
  93. printf("----");
  94. }
  95. printf("-\n");
  96. }
  97. printf(" 1 2 3 4 5 6 7\n\n");
  98. }
  99. // Write clearScreen function
  100. void clearScreen() {
  101. #ifdef _WIN32
  102. system("cls");
  103. #else
  104. system("clear");
  105. #endif
  106. }
  107. // Write isColumnFull function
  108. int isColumnFull(char board[ROWS][COLS], int col) {
  109. return (board[0][col] != ' ');
  110. }
  111. //Write dropPiece function
  112. int dropPiece(char board[ROWS][COLS], int col, char player) {
  113. for (int i = ROWS - 1; i >= 0; i--) {
  114. if (board[i][col] == ' ') {
  115. board[i][col] = player;
  116. return 1;
  117. }
  118. }
  119. return 0; // Column is full
  120. }
  121. // Write checkHorizontal function
  122. int checkHorizontal(char board[ROWS][COLS], char player) {
  123. for (int row = 0; row < ROWS; row++) {
  124. for (int col = 0; col <= COLS - 4; col++) {
  125. if (board[row][col] == player &&
  126. board[row][col + 1] == player &&
  127. board[row][col + 2] == player &&
  128. board[row][col + 3] == player) {
  129. return 1; // Gewonnen
  130. }
  131. }
  132. }
  133. return 0;
  134. }
  135. //Write checkVertical function
  136. int checkVertical(char board[ROWS][COLS], char player) {
  137. for (int col = 0; col < COLS; col++) {
  138. for (int row = 0; row <= ROWS - 4; row++) {
  139. if (board[row][col] == player &&
  140. board[row + 1][col] == player &&
  141. board[row + 2][col] == player &&
  142. board[row + 3][col] == player) {
  143. return 1; // Gewonnen
  144. }
  145. }
  146. }
  147. return 0;
  148. }
  149. // Write checkDiagonalLR function
  150. int checkDiagonalLR(char board[ROWS][COLS], char player) {
  151. for (int row = 0; row <= ROWS - 4; row++) {
  152. for (int col = 0; col <= COLS - 4; col++) {
  153. if (board[row][col] == player &&
  154. board[row + 1][col + 1] == player &&
  155. board[row + 2][col + 2] == player &&
  156. board[row + 3][col + 3] == player) {
  157. return 1; // Gewonnen
  158. }
  159. }
  160. }
  161. return 0;
  162. }
  163. // Write checkDiagonalRL function
  164. int checkDiagonalRL(char board[ROWS][COLS], char player) {
  165. for (int row = 0; row <= ROWS - 4; row++) {
  166. for (int col = 3; col < COLS; col++) {
  167. if (board[row][col] == player &&
  168. board[row + 1][col - 1] == player &&
  169. board[row + 2][col - 2] == player &&
  170. board[row + 3][col - 3] == player) {
  171. return 1; // Gewonnen
  172. }
  173. }
  174. }
  175. return 0;
  176. }
  177. // Write checkWin function
  178. int checkWin(char board[ROWS][COLS], char player) {
  179. return checkHorizontal(board, player) ||
  180. checkVertical(board, player) ||
  181. checkDiagonalLR(board, player) ||
  182. checkDiagonalRL(board, player);
  183. }
  184. // Write showMessage function
  185. void showMessage(const char* messageColor, const char* message) {
  186. printf("%s%s"RESET_COLOR, messageColor, message);
  187. }
  188. // Write showInvalidInputMessage function
  189. void showInvalidInputWarning() {
  190. showMessage(RED, "Ungültige Eingabe. Bitte wähle eine Spalte zwischen 1 und 7.\n");
  191. }
  192. // Write showColumnFullMessage function
  193. void showColumnFullMessage() {
  194. showMessage(RED, "Die ausgewählte Spalte ist bereits belegt. Bitte wähle eine andere Spalte aus.\n");
  195. }
  196. // Write showWinMessage function
  197. void showWinMessage(int player) {
  198. printf("Spieler %d hat gewonnen!\n", player);
  199. }