Skip to content

Commit

Permalink
Added logic to reveal mine map when a mine field is clicked
Browse files Browse the repository at this point in the history
  • Loading branch information
henrykorir committed Jun 9, 2024
1 parent 4e6ce1b commit e40dc8a
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 31 deletions.
Binary file modified bin/Debug/minesweeper
Binary file not shown.
9 changes: 2 additions & 7 deletions field.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,11 @@ wxButton * Field::GetButton()

void Field::OnButtonClick(wxCommandEvent& event)
{
MainFrame * parent = dynamic_cast<MainFrame*>(GetParent());
if(type == FIELD_MINE)
{
button->SetForegroundColour(wxColor(255,0,0));
button->SetLabel("M");
}
parent->Reveal();
else
{
MainFrame * parent = dynamic_cast<MainFrame*>(GetParent());
parent->UnCover(x,y);
}
}

wxBEGIN_EVENT_TABLE(Field, wxPanel)
Expand Down
44 changes: 30 additions & 14 deletions minesweeper.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "wx/wx.h"
#include <stdlib.h>
#include <vector>

#include "minesweeper.h"
#include "field.h"
Expand All @@ -22,14 +23,17 @@ MainFrame::MainFrame(const wxString &title)

int id = 0;

for (int i = 0; i < 9; i++)
for (int x = 0; x < 9; x++)
{
for (int j = 0; j < 9; j++)
for (int y = 0; y < 9; y++)
{
matrix[i][j] = new Field(this, i, j, FIELD_EMPTY, id);
matrix[x][y] = new Field(this, x, y, FIELD_EMPTY, id);
if (rand() / (float)RAND_MAX < PROB)
matrix[i][j]->SetType(FIELD_MINE);
gridSizer->Add(matrix[i][j]);
{
matrix[x][y]->SetType(FIELD_MINE);
minesLoci.push_back((x * 9) + y);
}
gridSizer->Add(matrix[x][y]);
id++;
}
}
Expand Down Expand Up @@ -60,7 +64,7 @@ void MainFrame::OnExitProgram(wxCloseEvent &event)
Destroy();
}

bool MainFrame::isFieldValid(int x, int y)
bool MainFrame::IsFieldValid(int x, int y)
{
return ((x >= 0) && (x <= 8) && (y >= 0) && (y <= 8));
}
Expand All @@ -70,57 +74,57 @@ void MainFrame::UnCover(int x, int y)
int countMines = 0;
wxStack<Field *> lh;

if (isFieldValid(x - 1, y))
if (IsFieldValid(x - 1, y))
{
if (matrix[x - 1][y]->GetType() == FIELD_MINE)
countMines++;
else
lh.push(matrix[x - 1][y]);
}

if (isFieldValid(x - 1, y + 1))
if (IsFieldValid(x - 1, y + 1))
{
if (matrix[x - 1][y + 1]->GetType() == FIELD_MINE)
countMines++;
else
lh.push(matrix[x - 1][y + 1]);
}
if (isFieldValid(x, y + 1))
if (IsFieldValid(x, y + 1))
{
if (matrix[x][y + 1]->GetType() == FIELD_MINE)
countMines++;
else
lh.push(matrix[x][y + 1]);
}
if (isFieldValid(x + 1, y + 1))
if (IsFieldValid(x + 1, y + 1))
{
if (matrix[x + 1][y + 1]->GetType() == FIELD_MINE)
countMines++;
else
lh.push(matrix[x + 1][y + 1]);
}
if (isFieldValid(x + 1, y))
if (IsFieldValid(x + 1, y))
{
if (matrix[x + 1][y]->GetType() == FIELD_MINE)
countMines++;
else
lh.push(matrix[x + 1][y]);
}
if (isFieldValid(x + 1, y - 1))
if (IsFieldValid(x + 1, y - 1))
{
if (matrix[x + 1][y - 1]->GetType() == FIELD_MINE)
countMines++;
else
lh.push(matrix[x + 1][y - 1]);
}
if (isFieldValid(x, y - 1))
if (IsFieldValid(x, y - 1))
{
if (matrix[x][y - 1]->GetType() == FIELD_MINE)
countMines++;
else
lh.push(matrix[x][y - 1]);
}
if (isFieldValid(x - 1, y - 1))
if (IsFieldValid(x - 1, y - 1))
{
if (matrix[x - 1][y - 1]->GetType() == FIELD_MINE)
countMines++;
Expand Down Expand Up @@ -154,6 +158,18 @@ void MainFrame::UnCover(int x, int y)
UnCover(top->GetX(), top->GetY());
}

void MainFrame::Reveal()
{
int width = 9;
for(int i = 0; i < minesLoci.size(); i++)
{
int index = minesLoci[i];
wxButton * button = matrix[index / width][index % width]->GetButton();
button->SetForegroundColour(wxColor(255,0,0));
button->SetLabel("M");
button->Disable();
}
}
wxDECLARE_APP(Minesweeper);
wxIMPLEMENT_APP(Minesweeper);

Expand Down
8 changes: 5 additions & 3 deletions minesweeper.depend
Original file line number Diff line number Diff line change
Expand Up @@ -1631,23 +1631,25 @@
1714995324 source:/home/freedom/Desktop/work/minesweeper/minesweeper/minesweeper.cpp
"wx/wx.h"

1717890325 source:/home/freedom/Desktop/work/minesweeper/minesweeper.cpp
1717932767 source:/home/freedom/Desktop/work/minesweeper/minesweeper.cpp
"wx/wx.h"
<stdlib.h>
<vector>
"minesweeper.h"
"field.h"

1717621837 source:/home/freedom/Desktop/work/minesweeper/field.cpp
1717924711 source:/home/freedom/Desktop/work/minesweeper/field.cpp
"wx/wx.h"
"field.h"
"minesweeper.h"

1716060291 /home/freedom/Desktop/work/minesweeper/field.h
"wx/wx.h"

1717883797 /home/freedom/Desktop/work/minesweeper/minesweeper.h
1717931288 /home/freedom/Desktop/work/minesweeper/minesweeper.h
"wx/wx.h"
"wx/stack.h"
<vector>
"field.h"

1714306976 /usr/local/include/wx-3.2/wx/stack.h
Expand Down
11 changes: 10 additions & 1 deletion minesweeper.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "wx/wx.h"
#include "wx/stack.h"
#include <vector>
#include "field.h"

#define PROB 0.2
Expand All @@ -21,13 +22,21 @@ class MainFrame : public wxFrame
void OnExitProgram(wxCloseEvent&);
void OnMouseEvent(wxMouseEvent&);
void UnCover(int, int);
void Reveal();

private:
class Point
{
public:
int x;
int y;
};
Field * matrix[9][9];
std::vector<int> minesLoci;
wxStack<Field*> st;
bool visited[81] {false};
bool instack[81] {false};
bool isFieldValid(int,int);
bool IsFieldValid(int,int);
wxDECLARE_EVENT_TABLE();
};

Expand Down
12 changes: 6 additions & 6 deletions minesweeper.layout
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@
<CodeBlocks_layout_file>
<FileVersion major="1" minor="0" />
<ActiveTarget name="Debug" />
<File name="field.h" open="1" top="0" tabpos="4" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<File name="minesweeper.cpp" open="1" top="1" tabpos="1" split="0" active="1" splitpos="0" zoom_1="2" zoom_2="0">
<Cursor>
<Cursor1 position="330" topLine="0" />
<Cursor1 position="53" topLine="165" />
</Cursor>
</File>
<File name="minesweeper.cpp" open="1" top="1" tabpos="1" split="0" active="1" splitpos="0" zoom_1="2" zoom_2="0">
<File name="field.h" open="1" top="0" tabpos="4" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="555" topLine="6" />
<Cursor1 position="330" topLine="3" />
</Cursor>
</File>
<File name="field.cpp" open="1" top="0" tabpos="2" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="828" topLine="34" />
<Cursor1 position="683" topLine="0" />
</Cursor>
</File>
<File name="minesweeper.h" open="1" top="0" tabpos="3" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="444" topLine="0" />
<Cursor1 position="485" topLine="17" />
</Cursor>
</File>
</CodeBlocks_layout_file>
Binary file modified obj/Debug/field.o
Binary file not shown.
Binary file modified obj/Debug/minesweeper.o
Binary file not shown.

0 comments on commit e40dc8a

Please sign in to comment.