Skip to content

Commit 9036cfc

Browse files
committed
Basic game working
1 parent 76e49af commit 9036cfc

File tree

1 file changed

+34
-10
lines changed

1 file changed

+34
-10
lines changed

src/main.cpp

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <functional>
22
#include <iostream>
3+
#include <random>
34

45
#include <docopt/docopt.h>
56
#include <ftxui/component/captured_mouse.hpp>// for ftxui
@@ -44,9 +45,19 @@ struct GameBoard
4445
}
4546
}
4647

47-
bool get(std::size_t x, std::size_t y) { return values[x][y]; }
48+
void visit(auto visitor)
49+
{
50+
for (std::size_t x = 0; x < width; ++x) {
51+
for (std::size_t y = 0; y < height; ++y) { visitor(x, y, *this); }
52+
}
53+
}
4854

49-
GameBoard() { update_strings(); }
55+
bool get(std::size_t x, std::size_t y) const { return values[x][y]; }
56+
57+
GameBoard()
58+
{
59+
visit([](const auto x, const auto y, auto &gameboard) { gameboard.set(x, y, true); });
60+
}
5061

5162
void update_strings()
5263
{
@@ -64,6 +75,17 @@ struct GameBoard
6475
if (x < width - 1) { toggle(x + 1, y); }
6576
if (y < height - 1) { toggle(x, y + 1); }
6677
}
78+
79+
bool solved() const
80+
{
81+
for (std::size_t x = 0; x < width; ++x) {
82+
for (std::size_t y = 0; y < height; ++y) {
83+
if (!get(x, y)) { return false; }
84+
}
85+
}
86+
87+
return true;
88+
}
6789
};
6890

6991
int main(int argc, const char **argv)
@@ -86,7 +108,10 @@ int main(int argc, const char **argv)
86108
std::vector<ftxui::Component> buttons;
87109
for (std::size_t x = 0; x < gb.width; ++x) {
88110
for (std::size_t y = 0; y < gb.height; ++y) {
89-
buttons.push_back(ftxui::Button(&gb.strings[x][y], [x, y, &gb] { gb.press(x,y); }));
111+
buttons.push_back(ftxui::Button(&gb.strings[x][y], [x, y, &gb, &screen] {
112+
gb.press(x, y);
113+
if (gb.solved()) { screen.ExitLoopClosure()(); }
114+
}));
90115
}
91116
}
92117
return buttons;
@@ -113,15 +138,14 @@ int main(int argc, const char **argv)
113138
return ftxui::vbox(std::move(columns));
114139
};
115140

116-
auto renderer = Renderer(container, make_layout);
141+
std::mt19937 gen32;
142+
std::uniform_int_distribution<std::size_t> x(static_cast<std::size_t>(0), gb.width - 1);
143+
std::uniform_int_distribution<std::size_t> y(static_cast<std::size_t>(0), gb.height - 1);
117144

145+
for (int i = 0; i < 100; ++i) { gb.press(x(gen32), y(gen32)); }
146+
147+
auto renderer = Renderer(container, make_layout);
118148

119-
/*
120-
return vbox({ text("Turn all boxes to 'on':"),
121-
text(""),
122-
hbox(toggle_1_selected == 0 ? color(Color::Green, quit_button->Render())
123-
: color(Color::Blue, quit_button->Render())) });
124-
*/
125149

126150
screen.Loop(renderer);
127151

0 commit comments

Comments
 (0)