Skip to content

Commit

Permalink
Added gitignore and base solution for filling table and count surroun…
Browse files Browse the repository at this point in the history
…ding mines in every fields in table.
  • Loading branch information
ReginaCsaszar committed Oct 16, 2017
1 parent d04dcf2 commit 3a939b5
Show file tree
Hide file tree
Showing 3 changed files with 280 additions and 0 deletions.
88 changes: 88 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# Created by .ignore support plugin (hsz.mobi)
### C++ template
# Prerequisites
*.d

# Compiled Object files
*.slo
*.lo
*.o
*.obj

# Precompiled Headers
*.gch
*.pch

# Compiled Dynamic libraries
*.so
*.dylib
*.dll

# Fortran module files
*.mod
*.smod

# Compiled Static libraries
*.lai
*.la
*.a
*.lib

# Executables
*.exe
*.app

# User-specific stuff:
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/dictionaries

# Sensitive or high-churn files:
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.xml
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml

# Gradle:
.idea/**/gradle.xml
.idea/**/libraries

# CMake
cmake-build-debug/

# Mongo Explorer plugin:
.idea/**/mongoSettings.xml

## File-based project format:
*.iws

## Plugin-specific files:

# IntelliJ
/out/

# mpeltonen/sbt-idea plugin
.idea_modules/

# JIRA plugin
atlassian-ide-plugin.xml

# Cursive Clojure plugin
.idea/replstate.xml

# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties

### CLion Patch ###
# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721

# *.iml
# modules.xml
# .idea/misc.xml
# *.ipr
7 changes: 7 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
cmake_minimum_required(VERSION 3.8)
project(cuddly_robot)

set(CMAKE_CXX_STANDARD 11)

set(SOURCE_FILES main.cpp)
add_executable(cuddly_robot ${SOURCE_FILES})
185 changes: 185 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
#include <iostream>
#include <random>

namespace {

char mine = '*';
char empty = '.';
char space = '0';

class Minesweeper {
public:
Minesweeper(const size_t width, const size_t height, const int bomb)
: width(width), height(height), mines(bomb),
table(new char[width * height]) {
fillTable();
}

/* In a real implementation there would also be a
* - copy constructor
* - assignment operator
* - move constructor
* - move assignment operator
* We will learn about them later
*/



virtual ~Minesweeper() {
delete[] table;
}

void countNeighbours() {

for(int y = 0; y < height; y++) {

for(int x = 0; x < width; x++) {

if (table[y*width+x] != mine) {

int mineCount = 0;
if (table[(y-1)*width+x] == mine) { mineCount++;}
if (table[(y+1)*width+x] == mine) { mineCount++;}

if (x > 0) {
if (table[(y-1)*width+x-1] == mine) { mineCount++;}
if (table[y*width+x-1] == mine) { mineCount++;}
if (table[(y+1)*width+x-1] == mine) { mineCount++;}
}

if (x < width-1) {
if (table[(y-1)*width+x+1]== mine) { mineCount++ ;}
if (table[y*width+x+1]== mine) { mineCount++ ;}
if (table[(y+1)*width+x+1]== mine) { mineCount++ ;}
}

table[y*width+x] = mineCount +'0';
}
}
}
}

void printTable() const {
std::cout<<"\nThe table content:\n\n";
for(int y = 0; y < height; y++)
{
for(int x = 0; x < width; x++)
{
std::cout <<table[y*width+x]<<" ";
}
std::cout<<std::endl;
}

}

void reveal(int x, int y) {

int index = y*width+x;
char *field = &table[index];

if ( *field == mine) {
std::cout <<"\nBOOOM! YOU DIED!"<<std::endl;

} else if ( *field == '0') {
char *revealTable = new char[width * height];

revealTable[index] = *field;
revealNeighboors(x, y, revealTable);

std::cout<<"\nThe reveal table content:\n\n";
for(int y = 0; y < height; y++) {
for(int x = 0; x < width; x++) {
std::cout <<revealTable[y*width+x]<<" "; }
std::cout<<std::endl; }
delete[] revealTable;

} else {
std::cout <<"You revealed a number "<< *field <<std::endl;
}


}

private:
void fillTable() {

for(int i = 0; i < height*width; i++) {
table[i] = empty;
}

std::random_device rd;
std::mt19937_64 gen(rd());
std::uniform_int_distribution<size_t> randIndex(0, width*height);

for(int i = 0; i < mines; i++) {
size_t m = randIndex(gen);
if(table[m] == mine) {
i--;
} else {
table[m] = mine; }
}
}

void revealNeighboors(size_t x, size_t y, char *revealTable) {

if ((table[(y-1)*width+x] == space)&&(revealTable[(y-1)*width+x] != space)) {
revealTable[(y-1)*width+x] = space;
revealNeighboors(x, y-1, revealTable);
}
if ((table[(y+1)*width+x] == space)&&(revealTable[(y+1)*width+x] != space)) {
revealTable[(y+1)*width+x] = space;
revealNeighboors(x, y+1, revealTable);
}

if (x > 0) {
if ((table[(y-1)*width+x-1] == space)&&(revealTable[(y-1)*width+x-1] != space)) {
revealTable[(y-1)*width+x-1] = space;
revealNeighboors(x-1, y-1, revealTable);
}
if ((table[y*width+x-1] == space)&&(revealTable[y*width+x-1] != space)) {
revealTable[y*width+x-1] = space;
revealNeighboors(x-1, y, revealTable);
}
if ((table[(y+1)*width+x-1] == space)&&(revealTable[(y+1)*width+x-1] != space)) {
revealTable[(y+1)*width+x-1] = space;
revealNeighboors(x-1, y+1, revealTable);
}
}

if (x < width-1) {
if ((table[(y-1)*width+x+1]== space)&&(revealTable[(y-1)*width+x+1] != space)) {
revealTable[(y-1)*width+x+1]= space;
revealNeighboors(x+1, y-1, revealTable);
}
if ((table[y*width+x+1]== space)&&(revealTable[y*width+x+1] != space)) {
revealTable[y*width+x+1] = space;
revealNeighboors(x+1, y, revealTable);
}
if ((table[(y+1)*width+x+1]== space)&&(revealTable[(y+1)*width+x+1] != space)) {
revealTable[(y+1)*width+x+1] = space;
revealNeighboors(x+1, y+1, revealTable);

}
}
}

const size_t width, height;
const int mines;
char *table;
};
}

int main() {
try {
Minesweeper ms(10, 10, 15);
ms.printTable();
ms.countNeighbours();
ms.printTable();
ms.reveal(5, 5);
} catch (const std::bad_alloc &e) {
std::cerr << "Couldn't allocate enough memory for minesweeper table" << std::endl;
return EXIT_FAILURE;
}

return 0;
}

0 comments on commit 3a939b5

Please sign in to comment.