Skip to content
Snippets Groups Projects
Commit 15fd260a authored by Pelotrio's avatar Pelotrio
Browse files

Initial commit

parents
No related branches found
No related tags found
No related merge requests found
/cmake-build-debug/
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
<?xml version="1.0" encoding="UTF-8"?>
<module classpath="CMake" type="CPP_MODULE" version="4" />
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CMakeWorkspace" PROJECT_DIR="$PROJECT_DIR$" />
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/MiniMineSweeper.iml" filepath="$PROJECT_DIR$/.idea/MiniMineSweeper.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
cmake_minimum_required(VERSION 3.23)
project(MiniMineSweeper)
set(CMAKE_CXX_STANDARD 14)
add_executable(MiniMineSweeper main.cpp)
main.cpp 0 → 100644
#include <iostream>
#include <ctime>
#include <cstring>
using namespace std;
const int MINES = 10;
int main() {
cout << "Mini-Minesweeper" << endl;
srand(time(nullptr));
int mines[MINES];
for (int &mine: mines) {
mine = rand() % 100;
}
char hiddenBoard[10][10];
memset(hiddenBoard, '_', sizeof(hiddenBoard));
// Fill the array with the amount of mines around each field
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
int count = 0;
for (int mine: mines) {
if (mine / 10 == i && mine % 10 == j) {
hiddenBoard[i][j] = 'o';
} else if (mine / 10 == i - 1 && mine % 10 == j - 1) {
count++;
} else if (mine / 10 == i - 1 && mine % 10 == j) {
count++;
} else if (mine / 10 == i - 1 && mine % 10 == j + 1) {
count++;
} else if (mine / 10 == i && mine % 10 == j - 1) {
count++;
} else if (mine / 10 == i && mine % 10 == j + 1) {
count++;
} else if (mine / 10 == i + 1 && mine % 10 == j - 1) {
count++;
} else if (mine / 10 == i + 1 && mine % 10 == j) {
count++;
} else if (mine / 10 == i + 1 && mine % 10 == j + 1) {
count++;
}
}
if (hiddenBoard[i][j] != 'o') {
hiddenBoard[i][j] = count + '0';
}
}
}
// Open board
char board[10][10];
memset(board, '*', sizeof(board));
bool gameOver = false;
int moves = 0;
while (!gameOver) {
// Print the board
cout << " A B C D E F G H I J" << endl;
for (int i = 0; i < 10; i++) {
if (i < 9) cout << i + 1 << " ";
else cout << i + 1;
for (int j = 0; j < 10; j++) {
cout << board[i][j] << " ";
}
cout << endl;
}
// Get the input in format "B3"
string input;
while (input.length() != 2 || input[0] < 'A' || input[0] > 'J' || input[1] < '0' || input[1] > '9') {
cout << "Bitte geben Sie eine Zahl zwischen A0 und J9 ein:";
cin >> input;
}
// Convert the input to a number
int col = input[0] - 'A';
int row = input[1] - '0' - 1;
cout << row << " " << col << endl;
cout << hiddenBoard[row][col] << endl;
// Open the field and display the number of mines around it
if (board[row][col] != '_') {
// Iterate over the 8 fields around the selected field
for (int i = row - 1; i <= row + 1; i++) {
for (int j = col - 1; j <= col + 1; j++) {
// Check if the field is in the board
if (i >= 0 && i < 10 && j >= 0 && j < 10) {
//Open the board if it is not already open
if(board[i][j] != '_') {
board[i][j] = hiddenBoard[i][j];
moves++;
}
}
}
}
// Check if the field is a mine
if (hiddenBoard[row][col] == 'o') {
gameOver = true;
} else {
board[row][col] = '_';
moves++;
}
} else {
cout << "Sie haben bereits auf dieses Feld geklickt!" << endl;
continue;
}
}
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment