|
@ -1,3 +1,17 @@ |
|
|
|
|
|
// Author fdai7726 |
|
|
|
|
|
|
|
|
|
|
|
/* |
|
|
|
|
|
*Ein Vier-Gewinnt-Spiel, das in der Kommandozeile gespielt wird. |
|
|
|
|
|
*Das Programm ermöglicht es zwei Spielern, abwechselnd Steine in eine Spalte zu setzen, |
|
|
|
|
|
*um eine Reihe von vier Steinen horizontal, |
|
|
|
|
|
*vertikal oder diagonal zu bilden und das Spiel zu gewinnen. |
|
|
|
|
|
*Das Spielfeld wird grafisch in der Kommandozeile dargestellt, |
|
|
|
|
|
*und das Programm überprüft automatisch, |
|
|
|
|
|
*ob einer der Spieler gewonnen hat oder ob das Spielfeld voll ist. |
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//Importiere wichtige Bibliotheken |
|
|
//Importiere wichtige Bibliotheken |
|
|
#include <stdio.h> |
|
|
#include <stdio.h> |
|
|
#include <stdlib.h> |
|
|
#include <stdlib.h> |
|
@ -17,62 +31,35 @@ |
|
|
#define CYAN "\033[0;36m" |
|
|
#define CYAN "\033[0;36m" |
|
|
#define WHITE "\033[0;37m" |
|
|
#define WHITE "\033[0;37m" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//Funktionsprototyp für initializeBoard |
|
|
|
|
|
|
|
|
//Funktionsprototypen |
|
|
void initializeBoard(char board[ROWS][COLS]); |
|
|
void initializeBoard(char board[ROWS][COLS]); |
|
|
|
|
|
|
|
|
//Funktionsprototyp für printBoard |
|
|
|
|
|
void printBoard(char board[ROWS][COLS]); |
|
|
void printBoard(char board[ROWS][COLS]); |
|
|
|
|
|
|
|
|
// Funktionsprototyp für clearScreen |
|
|
|
|
|
void clearScreen(); |
|
|
void clearScreen(); |
|
|
|
|
|
|
|
|
//Funktionsprototyp für isColumnFull |
|
|
|
|
|
int isColumnFull(char board[ROWS][COLS], int col); |
|
|
int isColumnFull(char board[ROWS][COLS], int col); |
|
|
|
|
|
|
|
|
//Funktionsprototyp für dropPiece |
|
|
|
|
|
int dropPiece(char board[ROWS][COLS], int col, char player); |
|
|
int dropPiece(char board[ROWS][COLS], int col, char player); |
|
|
//Funktionsprototyp für checkWin |
|
|
|
|
|
int checkWin(char board[ROWS][COLS], char player); |
|
|
int checkWin(char board[ROWS][COLS], char player); |
|
|
// Funktionsprototyp für checkHorizontal |
|
|
|
|
|
int checkHorizontal(char board[ROWS][COLS], char player); |
|
|
int checkHorizontal(char board[ROWS][COLS], char player); |
|
|
|
|
|
|
|
|
// Funktionsprototyp für checkVertical |
|
|
|
|
|
int checkVertical(char board[ROWS][COLS], char player); |
|
|
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); |
|
|
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 showInvalidInputMessage(); |
|
|
|
|
|
|
|
|
|
|
|
//Funktionsprototyp für showColumnFullMessage |
|
|
|
|
|
|
|
|
void showInvalidInputWarning(); |
|
|
void showColumnFullMessage(); |
|
|
void showColumnFullMessage(); |
|
|
|
|
|
|
|
|
//Funktionsprototyp für showWinMessage |
|
|
|
|
|
void showWinMessage(int player); |
|
|
void showWinMessage(int player); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//Write starter function |
|
|
|
|
|
int main_function() { |
|
|
int main_function() { |
|
|
char board[ROWS][COLS]; |
|
|
char board[ROWS][COLS]; |
|
|
int currentPlayer = 1; // Spieler 1 beginnt |
|
|
|
|
|
|
|
|
int Player = 1; |
|
|
|
|
|
|
|
|
initializeBoard(board); |
|
|
initializeBoard(board); |
|
|
printBoard(board); |
|
|
printBoard(board); |
|
|
|
|
|
|
|
|
int column; |
|
|
int column; |
|
|
while (1) { |
|
|
while (1) { |
|
|
printf(YELLOW"Spieler %d, wähle eine Spalte (1-7): "RESET_COLOR, currentPlayer); |
|
|
|
|
|
|
|
|
printf(YELLOW"Spieler %d, wähle eine Spalte (1-7): "RESET_COLOR, Player); |
|
|
|
|
|
|
|
|
scanf("%d", &column); |
|
|
scanf("%d", &column); |
|
|
if (column < 1 || column > 7) { |
|
|
if (column < 1 || column > 7) { |
|
|
showInvalidInputMessage(); |
|
|
|
|
|
|
|
|
showInvalidInputWarning(); |
|
|
continue; |
|
|
continue; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -83,20 +70,19 @@ int main_function() { |
|
|
continue; |
|
|
continue; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (dropPiece(board, column, (currentPlayer == 1) ? 'X' : 'O')) { |
|
|
|
|
|
|
|
|
if (dropPiece(board, column, (Player == 1) ? 'X' : 'O')) { |
|
|
printBoard(board); |
|
|
printBoard(board); |
|
|
if (checkWin(board, (currentPlayer == 1) ? 'X' : 'O')) { |
|
|
|
|
|
showWinMessage(currentPlayer); |
|
|
|
|
|
|
|
|
if (checkWin(board, (Player == 1) ? 'X' : 'O')) { |
|
|
|
|
|
showWinMessage(Player); |
|
|
break; |
|
|
break; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
currentPlayer = (currentPlayer == 1) ? 2 : 1; |
|
|
|
|
|
|
|
|
Player = (Player == 1) ? 2 : 1; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
return 0; |
|
|
return 0; |
|
|
} |
|
|
} |
|
|
// Write initializeBoard function |
|
|
|
|
|
void initializeBoard(char board[ROWS][COLS]) { |
|
|
void initializeBoard(char board[ROWS][COLS]) { |
|
|
for (int i = 0; i < ROWS; i++) { |
|
|
for (int i = 0; i < ROWS; i++) { |
|
|
for (int j = 0; j < COLS; j++) { |
|
|
for (int j = 0; j < COLS; j++) { |
|
@ -106,7 +92,6 @@ void initializeBoard(char board[ROWS][COLS]) { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Write printBoard function |
|
|
|
|
|
void printBoard(char board[ROWS][COLS]) { |
|
|
void printBoard(char board[ROWS][COLS]) { |
|
|
clearScreen(); |
|
|
clearScreen(); |
|
|
printf("\n"); |
|
|
printf("\n"); |
|
@ -125,7 +110,6 @@ void printBoard(char board[ROWS][COLS]) { |
|
|
|
|
|
|
|
|
printf(" 1 2 3 4 5 6 7\n\n"); |
|
|
printf(" 1 2 3 4 5 6 7\n\n"); |
|
|
} |
|
|
} |
|
|
// Write clearScreen function |
|
|
|
|
|
void clearScreen() { |
|
|
void clearScreen() { |
|
|
#ifdef _WIN32 |
|
|
#ifdef _WIN32 |
|
|
system("cls"); |
|
|
system("cls"); |
|
@ -134,12 +118,10 @@ void clearScreen() { |
|
|
#endif |
|
|
#endif |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Write isColumnFull function |
|
|
|
|
|
int isColumnFull(char board[ROWS][COLS], int col) { |
|
|
int isColumnFull(char board[ROWS][COLS], int col) { |
|
|
return (board[0][col] != ' '); |
|
|
return (board[0][col] != ' '); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
//Write dropPiece function |
|
|
|
|
|
int dropPiece(char board[ROWS][COLS], int col, char player) { |
|
|
int dropPiece(char board[ROWS][COLS], int col, char player) { |
|
|
for (int i = ROWS - 1; i >= 0; i--) { |
|
|
for (int i = ROWS - 1; i >= 0; i--) { |
|
|
if (board[i][col] == ' ') { |
|
|
if (board[i][col] == ' ') { |
|
@ -148,11 +130,10 @@ int dropPiece(char board[ROWS][COLS], int col, char player) { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
return 0; // Column is full |
|
|
|
|
|
|
|
|
return 0; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Write checkHorizontal function |
|
|
|
|
|
int checkHorizontal(char board[ROWS][COLS], char player) { |
|
|
int checkHorizontal(char board[ROWS][COLS], char player) { |
|
|
for (int row = 0; row < ROWS; row++) { |
|
|
for (int row = 0; row < ROWS; row++) { |
|
|
for (int col = 0; col <= COLS - 4; col++) { |
|
|
for (int col = 0; col <= COLS - 4; col++) { |
|
@ -160,14 +141,13 @@ int checkHorizontal(char board[ROWS][COLS], char player) { |
|
|
board[row][col + 1] == player && |
|
|
board[row][col + 1] == player && |
|
|
board[row][col + 2] == player && |
|
|
board[row][col + 2] == player && |
|
|
board[row][col + 3] == player) { |
|
|
board[row][col + 3] == player) { |
|
|
return 1; // Gewonnen |
|
|
|
|
|
|
|
|
return 1; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
return 0; |
|
|
return 0; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
//Write checkVertical function |
|
|
|
|
|
int checkVertical(char board[ROWS][COLS], char player) { |
|
|
int checkVertical(char board[ROWS][COLS], char player) { |
|
|
for (int col = 0; col < COLS; col++) { |
|
|
for (int col = 0; col < COLS; col++) { |
|
|
for (int row = 0; row <= ROWS - 4; row++) { |
|
|
for (int row = 0; row <= ROWS - 4; row++) { |
|
@ -175,7 +155,7 @@ int checkVertical(char board[ROWS][COLS], char player) { |
|
|
board[row + 1][col] == player && |
|
|
board[row + 1][col] == player && |
|
|
board[row + 2][col] == player && |
|
|
board[row + 2][col] == player && |
|
|
board[row + 3][col] == player) { |
|
|
board[row + 3][col] == player) { |
|
|
return 1; // Gewonnen |
|
|
|
|
|
|
|
|
return 1; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
@ -184,7 +164,6 @@ int checkVertical(char board[ROWS][COLS], char player) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Write checkDiagonalLR function |
|
|
|
|
|
int checkDiagonalLR(char board[ROWS][COLS], char player) { |
|
|
int checkDiagonalLR(char board[ROWS][COLS], char player) { |
|
|
for (int row = 0; row <= ROWS - 4; row++) { |
|
|
for (int row = 0; row <= ROWS - 4; row++) { |
|
|
for (int col = 0; col <= COLS - 4; col++) { |
|
|
for (int col = 0; col <= COLS - 4; col++) { |
|
@ -192,7 +171,7 @@ int checkDiagonalLR(char board[ROWS][COLS], char player) { |
|
|
board[row + 1][col + 1] == player && |
|
|
board[row + 1][col + 1] == player && |
|
|
board[row + 2][col + 2] == player && |
|
|
board[row + 2][col + 2] == player && |
|
|
board[row + 3][col + 3] == player) { |
|
|
board[row + 3][col + 3] == player) { |
|
|
return 1; // Gewonnen |
|
|
|
|
|
|
|
|
return 1; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
@ -202,7 +181,6 @@ int checkDiagonalLR(char board[ROWS][COLS], char player) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Write checkDiagonalRL function |
|
|
|
|
|
int checkDiagonalRL(char board[ROWS][COLS], char player) { |
|
|
int checkDiagonalRL(char board[ROWS][COLS], char player) { |
|
|
for (int row = 0; row <= ROWS - 4; row++) { |
|
|
for (int row = 0; row <= ROWS - 4; row++) { |
|
|
for (int col = 3; col < COLS; col++) { |
|
|
for (int col = 3; col < COLS; col++) { |
|
@ -210,7 +188,7 @@ int checkDiagonalRL(char board[ROWS][COLS], char player) { |
|
|
board[row + 1][col - 1] == player && |
|
|
board[row + 1][col - 1] == player && |
|
|
board[row + 2][col - 2] == player && |
|
|
board[row + 2][col - 2] == player && |
|
|
board[row + 3][col - 3] == player) { |
|
|
board[row + 3][col - 3] == player) { |
|
|
return 1; // Gewonnen |
|
|
|
|
|
|
|
|
return 1; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
@ -218,7 +196,6 @@ int checkDiagonalRL(char board[ROWS][COLS], char player) { |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Write checkWin function |
|
|
|
|
|
int checkWin(char board[ROWS][COLS], char player) { |
|
|
int checkWin(char board[ROWS][COLS], char player) { |
|
|
return checkHorizontal(board, player) || |
|
|
return checkHorizontal(board, player) || |
|
|
checkVertical(board, player) || |
|
|
checkVertical(board, player) || |
|
@ -226,30 +203,29 @@ int checkWin(char board[ROWS][COLS], char player) { |
|
|
checkDiagonalRL(board, player); |
|
|
checkDiagonalRL(board, player); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Write showMessage function |
|
|
|
|
|
|
|
|
// Die Funktion showMessage gibt eine formatierte Nachricht aus, wobei die Farbe der Nachricht dynamisch festgelegt werden kann. |
|
|
void showMessage(const char* messageColor, const char* message) { |
|
|
void showMessage(const char* messageColor, const char* message) { |
|
|
printf("%s%s"RESET_COLOR, messageColor, message); |
|
|
printf("%s%s"RESET_COLOR, messageColor, message); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Write showInvalidInputMessage function |
|
|
|
|
|
void showInvalidInputMessage() { |
|
|
|
|
|
|
|
|
//Die Funktion showInvalidInputWarning zeigt eine Warnung für ungültige Benutzereingaben an. Sie ruft die Funktion showMessage auf, um die Nachricht in roter Farbe auszugeben, die den Benutzer darüber informiert, dass die Eingabe ungültig ist und er eine Spalte zwischen 1 und 7 wählen soll. |
|
|
|
|
|
|
|
|
|
|
|
void showInvalidInputWarning() { |
|
|
showMessage(RED, "Ungültige Eingabe. Bitte wähle eine Spalte zwischen 1 und 7.\n"); |
|
|
showMessage(RED, "Ungültige Eingabe. Bitte wähle eine Spalte zwischen 1 und 7.\n"); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Write showColumnFullMessage function |
|
|
|
|
|
void showColumnFullMessage() { |
|
|
void showColumnFullMessage() { |
|
|
showMessage(RED, "Die Spalte ist voll. Bitte wähle eine andere.\n"); |
|
|
|
|
|
|
|
|
showMessage(RED, "Die ausgewählte Spalte ist bereits belegt. Bitte wähle eine andere Spalte aus.\n"); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Write showWinMessage function |
|
|
|
|
|
void showWinMessage(int player) { |
|
|
void showWinMessage(int player) { |
|
|
printf("Spieler %d hat gewonnen!\n", player); |
|
|
|
|
|
|
|
|
printf("Spieler %d ist der Gewinner!\n", player); |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|