|
|
@ -7,9 +7,109 @@ |
|
|
|
#define unknown_character 'x' |
|
|
|
#define mine_character '@' |
|
|
|
|
|
|
|
int calculate_mines(int height, int width) { |
|
|
|
int num_mines; |
|
|
|
if (height <= 1 || width <= 1) { |
|
|
|
|
|
|
|
GameResult initializeGame(TicTacToeGame* game) { |
|
|
|
// Initialisiere das Spielfeld |
|
|
|
for (int i = 0; i < 3; ++i) { |
|
|
|
for (int j = 0; j < 3; ++j) { |
|
|
|
game->board[i][j] = EMPTY; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// Wir setzen den Seed für die Zufallszahlgenerierung basierend auf der aktuellen Zeit |
|
|
|
srand(time(NULL)); |
|
|
|
|
|
|
|
// Münzwurf, um den Startspieler zu bestimmen |
|
|
|
game->currentPlayer = (rand() % 2 == 0) ? PLAYER_X : PLAYER_O; |
|
|
|
|
|
|
|
// Rückgabe des Ergebnisses |
|
|
|
return SUCCESS; |
|
|
|
} |
|
|
|
GameResult makeMove(TicTacToeGame* game, int row, int col) { |
|
|
|
if (row < 0 || row >= 3 || col < 0 || col >= 3 || game->board[row][col] != EMPTY) { |
|
|
|
return INVALID_MOVE; |
|
|
|
} |
|
|
|
|
|
|
|
game->board[row][col] = game->currentPlayer; |
|
|
|
|
|
|
|
GameResult result = checkGameResult(game); |
|
|
|
|
|
|
|
if (result == SUCCESS) { |
|
|
|
// Spielerwechsel, wenn der Zug gültig ist und das Spiel noch läuft |
|
|
|
game->currentPlayer = (game->currentPlayer == PLAYER_X) ? PLAYER_O : PLAYER_X; |
|
|
|
} |
|
|
|
|
|
|
|
return result; |
|
|
|
} |
|
|
|
|
|
|
|
Player getCurrentPlayer(const TicTacToeGame * game) { |
|
|
|
return game->currentPlayer; |
|
|
|
} |
|
|
|
|
|
|
|
int getNumberOfMoves(const TicTacToeGame* game) { |
|
|
|
int moves = 0; |
|
|
|
for (int i = 0; i < 3; ++i) { |
|
|
|
for (int j = 0; j < 3; ++j) { |
|
|
|
if (game->board[i][j] != EMPTY) { |
|
|
|
moves++; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
return moves; |
|
|
|
} |
|
|
|
|
|
|
|
int isBoardFull(const TicTacToeGame* game) { |
|
|
|
for (int i = 0; i < 3; ++i) { |
|
|
|
for (int j = 0; j < 3; ++j) { |
|
|
|
if (game->board[i][j] == EMPTY) { |
|
|
|
return 0; // Das Spielfeld ist nicht vollständig gefüllt |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
return 1; // Das Spielfeld ist vollständig gefüllt |
|
|
|
} |
|
|
|
|
|
|
|
int isFieldEmpty(const TicTacToeGame* game, int row, int col) { |
|
|
|
// Überprüfe, ob die angegebenen Zeilen- und Spaltenindizes innerhalb des Spielfelds liegen |
|
|
|
if (row < 0 || row >= 3 || col < 0 || col >= 3) { |
|
|
|
return 0; // Ungültige Indizes, das Feld ist nicht leer |
|
|
|
} |
|
|
|
|
|
|
|
// Überprüfe, ob das Feld leer ist |
|
|
|
return game->board[row][col] == EMPTY; |
|
|
|
} |
|
|
|
|
|
|
|
GameResult checkGameResult(const TicTacToeGame* game) { |
|
|
|
// Prüfen, ob ein Sieg oder ein Unentschieden vorliegt und das entsprechende Ergebnis zurückgeben |
|
|
|
for (int i = 0; i < 3; ++i) { |
|
|
|
//Zeilen und Spalten auf einen Gewinn prüfen |
|
|
|
if ((game->board[i][0] == game->board[i][1] && game->board[i][1] == game->board[i][2] && game->board[i][0] != EMPTY) || |
|
|
|
(game->board[0][i] == game->board[1][i] && game->board[1][i] == game->board[2][i] && game->board[0][i] != EMPTY)) { |
|
|
|
return GAME_WIN; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// Unentschieden Prüfen |
|
|
|
int draw = 1; |
|
|
|
for (int i = 0; i < 3; ++i) { |
|
|
|
for (int j = 0; j < 3; ++j) { |
|
|
|
if (game->board[i][j] == EMPTY) { |
|
|
|
draw = 0; |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
if (!draw) { |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return draw ? GAME_DRAW : SUCCESS; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
int berechneMinen(int hoehe, int breite) { |
|
|
|
int anzahl_minen; |
|
|
|
if (hoehe <= 1 || breite <= 1) { |
|
|
|
return 0; |
|
|
|
} |
|
|
|
else { |
|
|
@ -48,6 +148,16 @@ void distribute_mines(char** mines, int height, int width, int num_mines) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// Diagonalen Sieg prüfen |
|
|
|
if ((game->board[0][0] == game->board[1][1] && game->board[1][1] == game->board[2][2] && game->board[0][0] != EMPTY) || |
|
|
|
(game->board[0][2] == game->board[1][1] && game->board[1][1] == game->board[2][0] && game->board[0][2] != EMPTY)) { |
|
|
|
return GAME_WIN; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int minesweeper() { |
|
|
|
int height, width, row, column; |
|
|
|