From 8afb3708e1e94468c1c4b3887583d4e8bf4ca4ca Mon Sep 17 00:00:00 2001 From: henrykorir Date: Wed, 5 Jun 2024 23:31:29 +0300 Subject: [PATCH] Modified traversal algorithm to open neighboring empty fields if the clicked one is empty --- minesweeper.cpp | 54 +++++++++++++++++++++++++++---------------------- 1 file changed, 30 insertions(+), 24 deletions(-) diff --git a/minesweeper.cpp b/minesweeper.cpp index 42e766f..a8c227e 100644 --- a/minesweeper.cpp +++ b/minesweeper.cpp @@ -59,77 +59,83 @@ bool MainFrame::isFieldValid(int x, int y) void MainFrame::UnCover(int x, int y) { int countMines = 0; - wxStack lookHaed; + wxStack lh; if (isFieldValid(x - 1, y)) { if (matrix[x - 1][y]->GetType() == FIELD_MINE) countMines++; - else if (!instack[matrix[x - 1][y]->GetID()]) - lookHaed.push(matrix[x - 1][y]); + else + lh.push(matrix[x - 1][y]); } 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()]) - lookHaed.push(matrix[x - 1][y + 1]); + else + lh.push(matrix[x - 1][y + 1]); } if (isFieldValid(x, y + 1)) { if (matrix[x][y + 1]->GetType() == FIELD_MINE) countMines++; - else if (!instack[matrix[x][y + 1]->GetID()]) - lookHaed.push(matrix[x][y + 1]); + else + lh.push(matrix[x][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()]) - lookHaed.push(matrix[x + 1][y + 1]); + else + lh.push(matrix[x + 1][y + 1]); } if (isFieldValid(x + 1, y)) { if (matrix[x + 1][y]->GetType() == FIELD_MINE) countMines++; - else if (!instack[matrix[x + 1][y]->GetID()]) - lookHaed.push(matrix[x + 1][y]); + else + lh.push(matrix[x + 1][y]); } 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()]) - lookHaed.push(matrix[x + 1][y - 1]); + else + lh.push(matrix[x + 1][y - 1]); } if (isFieldValid(x, y - 1)) { if (matrix[x][y - 1]->GetType() == FIELD_MINE) countMines++; - else if (!instack[matrix[x][y - 1]->GetID()]) - lookHaed.push(matrix[x][y - 1]); + else + lh.push(matrix[x][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()]) - lookHaed.push(matrix[x - 1][y - 1]); + else + lh.push(matrix[x - 1][y - 1]); } matrix[x][y]->GetButton()->SetLabel(wxString::Format("%d", countMines)); visited[matrix[x][y]->GetID()] = true; - if (st.empty() || countMines > 0) - return; - while (lookHaed.size() > 0) + while (lh.size() > 0 && countMines == 0) + { + if (!instack[lh.top()->GetID()]) + { + st.push(lh.top()); + instack[lh.top()->GetID()] = true; + } + lh.pop(); + } + if (st.empty()) { - Field *field = lookHaed.top(); - instack[field->GetID()] = true; - st.push(field); - lookHaed.pop(); + while (lh.size() > 0) + lh.pop(); + return; } Field *top = st.top(); st.pop();