Skip to content

Commit

Permalink
Added strategy for opening adjacent empty fields
Browse files Browse the repository at this point in the history
  • Loading branch information
henrykorir committed Jun 5, 2024
1 parent ad1df34 commit fb875b8
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 83 deletions.
Binary file modified bin/Debug/minesweeper
Binary file not shown.
135 changes: 63 additions & 72 deletions minesweeper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,21 @@

wxBEGIN_EVENT_TABLE(MainFrame, wxFrame)
EVT_CLOSE(MainFrame::OnExitProgram)
wxEND_EVENT_TABLE()
wxEND_EVENT_TABLE()


MainFrame::MainFrame(const wxString& title)
: wxFrame(NULL,wxID_ANY, title,wxDefaultPosition,wxDefaultSize,wxDEFAULT_DIALOG_STYLE | wxMINIMIZE_BOX)
MainFrame::MainFrame(const wxString &title)
: wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE | wxMINIMIZE_BOX)
{
gridSizer = new wxGridSizer(9,9,0,0);
gridSizer = new wxGridSizer(9, 9, 0, 0);

int id = 0;

for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
matrix[i][j] = new Field(this, i,j, FIELD_EMPTY,id);
if (rand()/(float)RAND_MAX < PROB)
matrix[i][j] = new Field(this, i, j, FIELD_EMPTY, id);
if (rand() / (float)RAND_MAX < PROB)
matrix[i][j]->SetType(FIELD_MINE);
gridSizer->Add(matrix[i][j]);
id++;
Expand All @@ -38,16 +37,16 @@ MainFrame::MainFrame(const wxString& title)

MainFrame::~MainFrame()
{
for(int i = 0; i < 9; i++)
for(int j = 0; j < 9; j++)
for (int i = 0; i < 9; i++)
for (int j = 0; j < 9; j++)
{
delete matrix[i][j]->GetButton();
delete matrix[i][j];
}
Destroy();
}

void MainFrame::OnExitProgram(wxCloseEvent& event)
void MainFrame::OnExitProgram(wxCloseEvent &event)
{
Destroy();
}
Expand All @@ -60,103 +59,95 @@ bool MainFrame::isFieldValid(int x, int y)
void MainFrame::UnCover(int x, int y)
{
int countMines = 0;
wxStack<Field *> lookHaed;

if(isFieldValid(x-1, y))
if (isFieldValid(x - 1, y))
{
if (matrix[x-1][y]->GetType() == FIELD_MINE) countMines++;
else if(!instack[matrix[x-1][y]->GetID()])
{
st.push(matrix[x-1][y]);
instack[matrix[x-1][y]->GetID()] = true;
}
if (matrix[x - 1][y]->GetType() == FIELD_MINE)
countMines++;
else if (!instack[matrix[x - 1][y]->GetID()])
lookHaed.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 if(!instack[matrix[x-1][y+1]->GetID()])
{
st.push(matrix[x-1][y+1]);
instack[matrix[x-1][y+1]->GetID()] = true;
}
if (matrix[x - 1][y + 1]->GetType() == FIELD_MINE)
countMines++;
else if (!instack[matrix[x - 1][y + 1]->GetID()])
lookHaed.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 if(!instack[matrix[x][y+1]->GetID()])
{
st.push(matrix[x][y+1]);
instack[matrix[x][y+1]->GetID()] = true;
}
if (matrix[x][y + 1]->GetType() == FIELD_MINE)
countMines++;
else if (!instack[matrix[x][y + 1]->GetID()])
lookHaed.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 if(!instack[matrix[x+1][y+1]->GetID()])
{
st.push(matrix[x+1][y+1]);
instack[matrix[x+1][y+1]->GetID()] = true;
}
if (matrix[x + 1][y + 1]->GetType() == FIELD_MINE)
countMines++;
else if (!instack[matrix[x + 1][y + 1]->GetID()])
lookHaed.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 if(!instack[matrix[x+1][y]->GetID()])
{
st.push(matrix[x+1][y]);
instack[matrix[x+1][y]->GetID()] = true;
}
if (matrix[x + 1][y]->GetType() == FIELD_MINE)
countMines++;
else if (!instack[matrix[x + 1][y]->GetID()])
lookHaed.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 if(!instack[matrix[x+1][y-1]->GetID()])
{
st.push(matrix[x+1][y-1]);
instack[matrix[x+1][y-1]->GetID()] = true;
}
if (matrix[x + 1][y - 1]->GetType() == FIELD_MINE)
countMines++;
else if (!instack[matrix[x + 1][y - 1]->GetID()])
lookHaed.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 if(!instack[matrix[x][y-1]->GetID()])
{
st.push(matrix[x][y-1]);
instack[matrix[x][y-1]->GetID()] = true;
}
if (matrix[x][y - 1]->GetType() == FIELD_MINE)
countMines++;
else if (!instack[matrix[x][y - 1]->GetID()])
lookHaed.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 if(!instack[matrix[x-1][y-1]->GetID()])
{
st.push(matrix[x-1][y-1]);
instack[matrix[x-1][y-1]->GetID()] = true;
}
if (matrix[x - 1][y - 1]->GetType() == FIELD_MINE)
countMines++;
else if (!instack[matrix[x - 1][y - 1]->GetID()])
lookHaed.push(matrix[x - 1][y - 1]);
}

matrix[x][y]->GetButton()->SetLabel(wxString::Format("%d",countMines));
matrix[x][y]->GetButton()->SetLabel(wxString::Format("%d", countMines));
visited[matrix[x][y]->GetID()] = true;

if(st.empty()) return;
Field * top = st.top();
if (st.empty() || countMines > 0)
return;
while (lookHaed.size() > 0)
{
Field *field = lookHaed.top();
instack[field->GetID()] = true;
st.push(field);
lookHaed.pop();
}
Field *top = st.top();
st.pop();

UnCover(top->GetX(), top->GetY());

}

wxDECLARE_APP(Minesweeper);
wxIMPLEMENT_APP(Minesweeper);

bool Minesweeper::OnInit()
{
srand((unsigned) time(NULL));
srand((unsigned)time(NULL));

if (!wxApp::OnInit())
return false;

MainFrame * frame = new MainFrame(_("Minesweeper"));
MainFrame *frame = new MainFrame(_("Minesweeper"));
frame->Show();

return true;
Expand Down
6 changes: 3 additions & 3 deletions minesweeper.depend
Original file line number Diff line number Diff line change
Expand Up @@ -1631,21 +1631,21 @@
1714995324 source:/home/freedom/Desktop/work/minesweeper/minesweeper/minesweeper.cpp
"wx/wx.h"

1716585350 source:/home/freedom/Desktop/work/minesweeper/minesweeper.cpp
1716922103 source:/home/freedom/Desktop/work/minesweeper/minesweeper.cpp
"wx/wx.h"
<stdlib.h>
"minesweeper.h"
"field.h"

1716585559 source:/home/freedom/Desktop/work/minesweeper/field.cpp
1716722955 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"

1716583035 /home/freedom/Desktop/work/minesweeper/minesweeper.h
1716060291 /home/freedom/Desktop/work/minesweeper/minesweeper.h
"wx/wx.h"
"wx/stack.h"
"field.h"
Expand Down
16 changes: 8 additions & 8 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="minesweeper.h" open="1" top="0" tabpos="3" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="493" topLine="0" />
</Cursor>
</File>
<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="1223" topLine="51" />
<Cursor1 position="3863" topLine="132" />
</Cursor>
</File>
<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.h" open="1" top="0" tabpos="3" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="330" topLine="0" />
<Cursor1 position="444" topLine="10" />
</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" />
</Cursor>
</File>
<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="330" topLine="3" />
</Cursor>
</File>
</CodeBlocks_layout_file>
Binary file modified obj/Debug/minesweeper.o
Binary file not shown.

0 comments on commit fb875b8

Please sign in to comment.