flags and bombs

master
thunic 2 years ago
parent 7339aaf6bf
commit 2ebf6cc365

@ -32,7 +32,7 @@ typedef struct {
int** generateGameBoard(int size); int** generateGameBoard(int size);
void placeBombs(int** gameBoard, int size, int numBombs); void placeBombs(int** gameBoard, int size, int numBombs);
void displayGameBoardUART(int** gameBoard, int size); void displayGameBoardUART(int** gameBoard, int size, int numBombs, int numFlags);
void freeGameBoard(int** gameBoard, int size); void freeGameBoard(int** gameBoard, int size);
int** createHiddenGameBoard(int** gameBoard, int size); int** createHiddenGameBoard(int** gameBoard, int size);
int** revealCell(int** gameBoard, int** hiddenGameBoard, int size, int row, int col); int** revealCell(int** gameBoard, int** hiddenGameBoard, int size, int row, int col);

@ -72,6 +72,7 @@ int main(void)
/* USER CODE BEGIN 1 */ /* USER CODE BEGIN 1 */
int size = 12; int size = 12;
int numBombs = 10; int numBombs = 10;
int numFlags = 0;
int hiddenGameBoard = 0; int hiddenGameBoard = 0;
int** gameBoard = (int**)malloc(size * sizeof(int*)); int** gameBoard = (int**)malloc(size * sizeof(int*));
@ -102,13 +103,15 @@ int main(void)
gameBoard= generateGameBoard(size); gameBoard= generateGameBoard(size);
placeBombs(gameBoard, size, numBombs); placeBombs(gameBoard, size, numBombs);
clearSerialOutput(); clearSerialOutput();
displayGameBoardUART(gameBoard, size); displayGameBoardUART(gameBoard, size, numBombs, numFlags);
hiddenGameBoard= createHiddenGameBoard(gameBoard, size); hiddenGameBoard= createHiddenGameBoard(gameBoard, size);
displayGameBoardUART(hiddenGameBoard, size); displayGameBoardUART(hiddenGameBoard, size, numBombs, numFlags);
hiddenGameBoard= revealCell(gameBoard, hiddenGameBoard, size, 3, 5); hiddenGameBoard= revealCell(gameBoard, hiddenGameBoard, size, 3, 5);
displayGameBoardUART(hiddenGameBoard, size); hiddenGameBoard= setFlag(hiddenGameBoard, 6,7, numFlags);
displayGameBoardUART(hiddenGameBoard, size, numBombs, numFlags);
/* USER CODE END 2 */ /* USER CODE END 2 */

@ -72,7 +72,7 @@ void placeBombs(int** gameBoard, int size, int numBombs) {
} }
} }
void displayGameBoardUART(int** gameBoard, int size) { void displayGameBoardUART(int** gameBoard, int size, int numBombs, int numFlags) {
// Leere den Empfangspuffer der UART-Schnittstelle // Leere den Empfangspuffer der UART-Schnittstelle
clearSerialBuffer(&huart2); clearSerialBuffer(&huart2);
@ -108,6 +108,9 @@ void displayGameBoardUART(int** gameBoard, int size) {
offset += snprintf(buffer + offset, sizeof(buffer) - offset, "\r\n"); offset += snprintf(buffer + offset, sizeof(buffer) - offset, "\r\n");
} }
// Anzeige der verbleibenden Anzahl von Bomben
offset += snprintf(buffer + offset, sizeof(buffer) - offset, "\r\nVerbleibende Anzahl von Bomben: %d\r\n", numBombs - numFlags);
// Senden des formatierten Strings über UART // Senden des formatierten Strings über UART
HAL_UART_Transmit(&huart2, (uint8_t*)buffer, offset, 100); HAL_UART_Transmit(&huart2, (uint8_t*)buffer, offset, 100);
} }
@ -173,8 +176,8 @@ void revealEmptyCells(int** gameBoard, int** hiddenGameBoard, int size, Position
int** revealCell(int** gameBoard, int** hiddenGameBoard, int size, int row, int col) { int** revealCell(int** gameBoard, int** hiddenGameBoard, int size, int row, int col) {
Position pos; Position pos;
pos.row = row - 1; pos.row = row;
pos.col = col - 1; pos.col = col;
// Überprüfen, ob die Position im gültigen Bereich liegt // Überprüfen, ob die Position im gültigen Bereich liegt
if (pos.row < 0 || pos.row >= size || pos.col < 0 || pos.col >= size) { if (pos.row < 0 || pos.row >= size || pos.col < 0 || pos.col >= size) {
@ -199,12 +202,15 @@ int** revealCell(int** gameBoard, int** hiddenGameBoard, int size, int row, int
return hiddenGameBoard; return hiddenGameBoard;
} }
void setFlag(int** hiddenGameBoard, int row, int col) { void setFlag(int** hiddenGameBoard, int row, int col, int numFlags) {
if (hiddenGameBoard[row][col] == FLAG_CELL) if (hiddenGameBoard[row][col] == FLAG_CELL)
{ {
hiddenGameBoard[row][col] = HIDDEN_CELL; hiddenGameBoard[row][col] = HIDDEN_CELL;
numFlags --;
} }
else{ else if (hiddenGameBoard[row][col] == HIDDEN_CELL){
hiddenGameBoard[row][col] = FLAG_CELL; hiddenGameBoard[row][col] = FLAG_CELL;
numFlags ++;
} }
return numFlags;
} }

Loading…
Cancel
Save