|
|
//Importiere wichtige Bibliotheken
#include <stdio.h>
#include <stdlib.h>
//Definiere Feldgröße
#define ROWS 6
#define COLS 7
// Definiere Farben
#define RESET_COLOR "\033[0m"
#define BLACK "\033[0;30m"
#define RED "\033[0;31m"
#define GREEN "\033[0;32m"
#define YELLOW "\033[0;33m"
#define BLUE "\033[0;34m"
#define MAGENTA "\033[0;35m"
#define CYAN "\033[0;36m"
#define WHITE "\033[0;37m"
//Funktionsprototyp für initializeBoard
void initializeBoard(char board[ROWS][COLS]);
//Funktionsprototyp für printBoard
void printBoard(char board[ROWS][COLS]);
// Funktionsprototyp für clearScreen
void clearScreen();
//Funktionsprototyp für isColumnFull
int isColumnFull(char board[ROWS][COLS], int col);
//Funktionsprototyp für dropPiece
int dropPiece(char board[ROWS][COLS], int col, char player); //Funktionsprototyp für checkWin
int checkWin(char board[ROWS][COLS], char player); // Funktionsprototyp für checkHorizontal
int checkHorizontal(char board[ROWS][COLS], char player);
// Funktionsprototyp für checkVertical
int checkVertical(char board[ROWS][COLS], char player);
// Funktionsprototyp für checkDiagonalLR
int checkDiagonalLR(char board[ROWS][COLS], char player);
// Funktionsprototyp für checkDiagonalRL
int checkDiagonalRL(char board[ROWS][COLS], char player);
// Funktionsprototyp für showMessage
void showMessage(const char* messageColor, const char* message); //Funktionsprototyp für showInvalidInputMessage
void showInvalidInputWarning();
//Funktionsprototyp für showColumnFullMessage
void showColumnFullMessage();
//Funktionsprototyp für showWinMessage
void showWinMessage(int player);
//Write starter function
int main_function() { char board[ROWS][COLS]; int currentPlayer = 1; // Spieler 1 beginnt
initializeBoard(board); printBoard(board);
int column; while (1) { printf(YELLOW"Spieler %d, wähle eine Spalte (1-7): "RESET_COLOR, currentPlayer);
scanf("%d", &column); if (column < 1 || column > 7) { showInvalidInputWarning(); continue; }
column--;
if (isColumnFull(board, column)) { showColumnFullMessage(); continue; }
if (dropPiece(board, column, (currentPlayer == 1) ? 'X' : 'O')) { printBoard(board); if (checkWin(board, (currentPlayer == 1) ? 'X' : 'O')) { showWinMessage(currentPlayer); break; }
currentPlayer = (currentPlayer == 1) ? 2 : 1; } }
return 0; } // Write initializeBoard function
void initializeBoard(char board[ROWS][COLS]) { for (int i = 0; i < ROWS; i++) { for (int j = 0; j < COLS; j++) { board[i][j] = ' '; } } }
// Write printBoard function
void printBoard(char board[ROWS][COLS]) { clearScreen(); printf("\n");
for (int i = 0; i < ROWS; i++) { for (int j = 0; j < COLS; j++) { printf("| %c ", board[i][j]); } printf("|\n");
for (int j = 0; j < COLS; j++) { printf("----"); } printf("-\n"); }
printf(" 1 2 3 4 5 6 7\n\n"); } // Write clearScreen function
void clearScreen() { #ifdef _WIN32
system("cls"); #else
system("clear"); #endif
}
// Write isColumnFull function
int isColumnFull(char board[ROWS][COLS], int col) { return (board[0][col] != ' '); }
//Write dropPiece function
int dropPiece(char board[ROWS][COLS], int col, char player) { for (int i = ROWS - 1; i >= 0; i--) { if (board[i][col] == ' ') { board[i][col] = player; return 1; } }
return 0; // Column is full
}
// Write checkHorizontal function
int checkHorizontal(char board[ROWS][COLS], char player) { for (int row = 0; row < ROWS; row++) { for (int col = 0; col <= COLS - 4; col++) { if (board[row][col] == player && board[row][col + 1] == player && board[row][col + 2] == player && board[row][col + 3] == player) { return 1; // Gewonnen
} } } return 0; }
//Write checkVertical function
int checkVertical(char board[ROWS][COLS], char player) { for (int col = 0; col < COLS; col++) { for (int row = 0; row <= ROWS - 4; row++) { if (board[row][col] == player && board[row + 1][col] == player && board[row + 2][col] == player && board[row + 3][col] == player) { return 1; // Gewonnen
} } } return 0; }
// Write checkDiagonalLR function
int checkDiagonalLR(char board[ROWS][COLS], char player) { for (int row = 0; row <= ROWS - 4; row++) { for (int col = 0; col <= COLS - 4; col++) { if (board[row][col] == player && board[row + 1][col + 1] == player && board[row + 2][col + 2] == player && board[row + 3][col + 3] == player) { return 1; // Gewonnen
} } } return 0; }
// Write checkDiagonalRL function
int checkDiagonalRL(char board[ROWS][COLS], char player) { for (int row = 0; row <= ROWS - 4; row++) { for (int col = 3; col < COLS; col++) { if (board[row][col] == player && board[row + 1][col - 1] == player && board[row + 2][col - 2] == player && board[row + 3][col - 3] == player) { return 1; // Gewonnen
} } } return 0;
}
// Write checkWin function
int checkWin(char board[ROWS][COLS], char player) { return checkHorizontal(board, player) || checkVertical(board, player) || checkDiagonalLR(board, player) || checkDiagonalRL(board, player); }
// Write showMessage function
void showMessage(const char* messageColor, const char* message) { printf("%s%s"RESET_COLOR, messageColor, message); }
// Write showInvalidInputMessage function
void showInvalidInputWarning() { showMessage(RED, "Ungültige Eingabe. Bitte wähle eine Spalte zwischen 1 und 7.\n"); }
// Write showColumnFullMessage function
void showColumnFullMessage() { showMessage(RED, "Die ausgewählte Spalte ist bereits belegt. Bitte wähle eine andere Spalte aus.\n"); }
// Write showWinMessage function
void showWinMessage(int player) { printf("Spieler %d hat gewonnen!\n", player); }
|