diff --git a/bin/Debug/minesweeper b/bin/Debug/minesweeper index 27f575c..f446c9f 100755 Binary files a/bin/Debug/minesweeper and b/bin/Debug/minesweeper differ diff --git a/minesweeper.cpp b/minesweeper.cpp index 4d494a0..42e766f 100644 --- a/minesweeper.cpp +++ b/minesweeper.cpp @@ -6,13 +6,12 @@ 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; @@ -20,8 +19,8 @@ MainFrame::MainFrame(const wxString& title) { 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++; @@ -38,8 +37,8 @@ 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]; @@ -47,7 +46,7 @@ MainFrame::~MainFrame() Destroy(); } -void MainFrame::OnExitProgram(wxCloseEvent& event) +void MainFrame::OnExitProgram(wxCloseEvent &event) { Destroy(); } @@ -60,90 +59,82 @@ bool MainFrame::isFieldValid(int x, int y) void MainFrame::UnCover(int x, int y) { int countMines = 0; + wxStack 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); @@ -151,12 +142,12 @@ 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; diff --git a/minesweeper.depend b/minesweeper.depend index 0573a7c..a8da9c4 100644 --- a/minesweeper.depend +++ b/minesweeper.depend @@ -1631,13 +1631,13 @@ 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" "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" @@ -1645,7 +1645,7 @@ 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" diff --git a/minesweeper.layout b/minesweeper.layout index 12c1304..ec2ec39 100644 --- a/minesweeper.layout +++ b/minesweeper.layout @@ -2,19 +2,14 @@ - - - - - - + - + - + @@ -22,4 +17,9 @@ + + + + + diff --git a/obj/Debug/minesweeper.o b/obj/Debug/minesweeper.o index e5cbb1a..6813931 100644 Binary files a/obj/Debug/minesweeper.o and b/obj/Debug/minesweeper.o differ