Skip to content

Commit

Permalink
Added clock and flag stats
Browse files Browse the repository at this point in the history
  • Loading branch information
henrykorir committed Jun 12, 2024
1 parent 2ed31e5 commit 610b9c0
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 26 deletions.
Binary file modified bin/Debug/minesweeper
Binary file not shown.
7 changes: 7 additions & 0 deletions field.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ void Field::OnButtonClick(wxCommandEvent& event)
parent->UnCover(x,y);
}

void Field::OnMouseRightClick(wxMouseEvent& event)
{
this->SetBackgroundColour(wxColor(128, 128, 128));
wxMessageBox("HEYYEY");
}

wxBEGIN_EVENT_TABLE(Field, wxPanel)
EVT_BUTTON(wxID_ANY, Field::OnButtonClick)
EVT_RIGHT_DOWN(Field::OnMouseRightClick)
wxEND_EVENT_TABLE()
1 change: 1 addition & 0 deletions field.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class Field : public wxPanel
void SetID(int);
int GetID();
wxAnyButton * GetButton();
void OnMouseRightClick(wxMouseEvent&);

private:
int x;
Expand Down
Binary file added flag-icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added flag.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added mine.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
52 changes: 39 additions & 13 deletions minesweeper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,32 @@ wxEND_EVENT_TABLE()
MainFrame::MainFrame(const wxString &title)
: wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE | wxMINIMIZE_BOX)
{
SetIcon(wxIcon("logo.png",wxBITMAP_TYPE_PNG, -1,-1));
SetIcon(wxIcon("mine.png",wxBITMAP_TYPE_PNG, -1,-1));
m_timer = new wxTimer(this, TIMER_ID);
m_timer->Start(1000);

wxBoxSizer * topSizer = new wxBoxSizer(wxVERTICAL);
wxBoxSizer * clockSizer = new wxBoxSizer(wxVERTICAL);
wxBoxSizer * flagSizer = new wxBoxSizer(wxVERTICAL);
wxBoxSizer * dashSizer = new wxBoxSizer(wxHORIZONTAL);
wxGridSizer * gridSizer = new wxGridSizer(9, 9, 0, 0);

wxBitmap clock(clock_xpm);
wxBitmap::Rescale(clock, wxSize(32,32));
wxBitmap::Rescale(clock, wxSize(24,24));
wxStaticBitmap* staticBitmap = new wxStaticBitmap(this, wxID_STATIC,clock);
text = new wxStaticText(this,wxID_ANY,wxT("0"), wxDefaultPosition,wxDefaultSize, 0);
dashSizer->Add(staticBitmap, 0, wxALIGN_CENTER, 10);
dashSizer->Add(text, 0, wxALIGN_CENTER, 10);
clockText = new wxStaticText(this,wxID_ANY,wxT("00:00:00"), wxDefaultPosition,wxDefaultSize, wxALIGN_CENTRE_HORIZONTAL);
clockSizer->Add(staticBitmap, 0, wxCENTER);
clockSizer->Add(clockText, 0, wxCENTER);

wxBitmap flag("red-flag.png", wxBITMAP_TYPE_PNG);
wxBitmap::Rescale(flag, wxSize(24,24));
wxStaticBitmap* flagBitmap = new wxStaticBitmap(this, wxID_STATIC,flag);
flagText = new wxStaticText(this,wxID_ANY,wxT("0"), wxDefaultPosition,wxDefaultSize, wxALIGN_CENTRE_HORIZONTAL);
flagSizer->Add(flagBitmap, 0, wxCENTER);
flagSizer->Add(flagText, 0, wxCENTER);

dashSizer->Add(clockSizer, 0, wxALIGN_CENTER | wxALL);
dashSizer->Add(flagSizer, 0, wxALIGN_CENTER | wxALL);

int id = 0;

Expand All @@ -38,7 +50,7 @@ MainFrame::MainFrame(const wxString &title)
for (int y = 0; y < 9; y++)
{
matrix[x][y] = new Field(this, x, y, FIELD_EMPTY, id);
if (rand() / (float)RAND_MAX < PROB)
if (rand() / (float)RAND_MAX < PROB && minesLoci.size() < 10)
{
matrix[x][y]->SetType(FIELD_MINE);
minesLoci.push_back((x * 9) + y);
Expand All @@ -48,7 +60,7 @@ MainFrame::MainFrame(const wxString &title)
}
}

topSizer->Add(dashSizer, 0, wxALIGN_CENTER | wxALL, 8);
topSizer->Add(dashSizer, 0, wxALIGN_CENTER | wxALL, 2);
topSizer->Add(gridSizer, 0, wxALIGN_CENTER);

SetSizer(topSizer);
Expand Down Expand Up @@ -181,10 +193,8 @@ void MainFrame::Reveal()
int index = minesLoci[i];
wxAnyButton * button = matrix[index / width][index % width]->GetButton();
button->SetForegroundColour(wxColor(255,0,0));
button->SetLabel("M");
//wxBitmap mine("logo.png",wxBITMAP_TYPE_PNG);
//button->SetBitmap(mine, wxLEFT);
//button->Disable();
wxBitmap mine("logo.png",wxBITMAP_TYPE_PNG);
button->SetBitmap(mine, wxLEFT);
}

for(size_t idx = 0; idx < numberOfFields; idx++)
Expand All @@ -193,11 +203,27 @@ void MainFrame::Reveal()
}
}

wxString MainFrame::FormatClock()
{
int h, m, s, t = interval / 1000;
h = t / 3600;
t = t % 3600;
wxString hh = h < 10 ? wxString::Format("0%d", h) : wxString::Format("%d", h);

m = t / 60;
t = t % 60;
wxString mm = m < 10 ? wxString::Format("0%d", m) : wxString::Format("%d", m);

s = t;
wxString ss = s < 10 ? wxString::Format("0%d", s) : wxString::Format("%d", s);
wxString period = hh + ":" + mm + ":" + ss;

return (period);
}
void MainFrame::OnTimer(wxTimerEvent& event)
{
interval += event.GetInterval();
wxString s = wxString::Format("%d",interval/1000);
text->SetLabel(s);
clockText->SetLabel(FormatClock());
}

void MainFrame::StopTimer()
Expand Down
8 changes: 4 additions & 4 deletions minesweeper.depend
Original file line number Diff line number Diff line change
Expand Up @@ -1631,7 +1631,7 @@
1714995324 source:/home/freedom/Desktop/work/minesweeper/minesweeper/minesweeper.cpp
"wx/wx.h"

1718144832 source:/home/freedom/Desktop/work/minesweeper/minesweeper.cpp
1718194759 source:/home/freedom/Desktop/work/minesweeper/minesweeper.cpp
"wx/wx.h"
<stdlib.h>
<vector>
Expand All @@ -1640,15 +1640,15 @@
"red.xpm"
"clock.xpm"

1718138416 source:/home/freedom/Desktop/work/minesweeper/field.cpp
1718192400 source:/home/freedom/Desktop/work/minesweeper/field.cpp
"wx/wx.h"
"field.h"
"minesweeper.h"

1717960345 /home/freedom/Desktop/work/minesweeper/field.h
1718191308 /home/freedom/Desktop/work/minesweeper/field.h
"wx/wx.h"

1717947304 /home/freedom/Desktop/work/minesweeper/minesweeper.h
1718193861 /home/freedom/Desktop/work/minesweeper/minesweeper.h
"wx/wx.h"
"wx/stack.h"
<vector>
Expand Down
4 changes: 3 additions & 1 deletion minesweeper.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@ class MainFrame : public wxFrame
int interval{0};
std::vector<int> minesLoci;
wxTimer * m_timer;
wxStaticText * text;
wxStaticText * clockText;
wxStaticText * flagText;
wxStack<Field*> st;
bool visited[81] {false};
bool instack[81] {false};
bool IsFieldValid(int,int);
wxString FormatClock();
wxDECLARE_EVENT_TABLE();
};

Expand Down
16 changes: 8 additions & 8 deletions minesweeper.layout
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@
<CodeBlocks_layout_file>
<FileVersion major="1" minor="0" />
<ActiveTarget name="Debug" />
<File name="field.cpp" open="1" top="0" tabpos="2" split="0" active="1" splitpos="0" zoom_1="5" zoom_2="0">
<Cursor>
<Cursor1 position="314" topLine="53" />
</Cursor>
</File>
<File name="minesweeper.cpp" open="1" top="1" tabpos="1" split="0" active="1" splitpos="0" zoom_1="7" zoom_2="0">
<Cursor>
<Cursor1 position="5718" topLine="0" />
<Cursor1 position="1582" topLine="42" />
</Cursor>
</File>
<File name="field.cpp" open="1" top="0" tabpos="2" split="0" active="1" splitpos="0" zoom_1="5" zoom_2="0">
<File name="red.xpm" open="1" top="0" tabpos="6" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="314" topLine="54" />
<Cursor1 position="93" topLine="0" />
</Cursor>
</File>
<File name="field.h" open="1" top="0" tabpos="4" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
Expand All @@ -22,11 +27,6 @@
<Cursor1 position="518" topLine="18" />
</Cursor>
</File>
<File name="red.xpm" open="1" top="0" tabpos="6" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="93" topLine="0" />
</Cursor>
</File>
<File name="clock.xpm" open="1" top="0" tabpos="5" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="109" topLine="0" />
Expand Down
Binary file added red-flag.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 610b9c0

Please sign in to comment.