diff --git a/application.cpp b/application.cpp index f5e5bb860edaffafbd8cf1cbf5987f6b1cae294a..f0fea5bf6a3641762ac025851b4cc5f22cb4109f 100644 --- a/application.cpp +++ b/application.cpp @@ -1,7 +1,9 @@ #include "application.hpp" #include "graphics.hpp" #include "widget.hpp" -#include <iostream> +#include "menu.hpp" +#include "spinbox.hpp" + using namespace genv; using namespace std; @@ -12,10 +14,7 @@ Application::Application() } -void ellenoriz(vector<Widget> v) -{ -} void Application::registerWidget(Widget* w) { @@ -23,13 +22,81 @@ void Application::registerWidget(Widget* w) widgets.push_back(w); } + + +void Application :: board(int diffi) +{ + vector<vector<int>> board; + + if(diffi == 1) + { + board = { + {5, 3, 0, 0, 7, 0, 0, 0, 0}, + {6, 0, 0, 1, 9, 5, 0, 0, 0}, + {0, 9, 8, 0, 0, 0, 0, 6, 0}, + {8, 0, 0, 0, 6, 0, 0, 0, 3}, + {4, 0, 0, 8, 0, 3, 0, 0, 1}, + {7, 0, 0, 0, 2, 0, 0, 0, 6}, + {0, 6, 0, 0, 0, 0, 2, 8, 0}, + {0, 0, 0, 4, 1, 9, 0, 0, 5}, + {0, 0, 0, 0, 8, 0, 0, 7, 9} + }; + } + + + + for (int i = 0; i < 9; ++i) + { + vector<SpinBox*> rowWidgets; + + for (int j = 0; j < 9; ++j) + { + SpinBox* spin; + if (board[i][j] != 0) + { + spin = new SpinBox(this, 5 + j * 100, 5 + i * 100, 100, board[i][j], board[i][j], board[i][j], 0,100,255); + } + else + { + spin = new SpinBox(this, 5 + j * 100, 5 + i * 100, 100, 0, 9,0,255,255,255); + } + rowWidgets.push_back(spin); + } + + + widgets2.push_back(rowWidgets); + } +} + + void Application::event_loop() { + Menu m; int wrongi = -1; event ev; + int focus = -1; - while(gin >> ev ) + while (m.diffret() == 0 and gin >> ev) + { + + + m.draw(); + + m.handle(ev); + + gout << refresh; + + if(ev.type == ev_key and ev.keycode == key_escape) + { + break; + } + } + + board(m.diffret()); + + while(gin >> ev) { + if (ev.type == ev_mouse && ev.button==btn_left) { for (size_t i=0; i<widgets.size(); i++) @@ -42,11 +109,6 @@ void Application::event_loop() } wrongi = -1; - - - - - if (focus != -1) { widgets[focus]->handle(ev); for (int k = 0; k < 81; k += 9) { @@ -70,7 +132,7 @@ void Application::event_loop() for (int i = k; i < 81; i += 9) { for (int j = i + 9; j < 81; j += 9) { if (widgets[i]->getter() != 0 && widgets[i]->getter() == widgets[j]->getter()) { - wrongi = i; + wrongi = j; break; } } @@ -83,6 +145,7 @@ void Application::event_loop() } } + for (int row_offset = 0; row_offset < 9; row_offset += 3) { for (int col_offset = 0; col_offset < 9; col_offset += 3) { std::vector<bool> seen(10, false); @@ -111,7 +174,8 @@ void Application::event_loop() break; } } - } + } + for (int i = 0; i < widgets.size(); ++i) { if( i == wrongi){ @@ -121,8 +185,16 @@ void Application::event_loop() widgets[i]->draw2(); } } + + if(ev.type == ev_key and ev.keycode == key_escape) + { + break; + } + + gout << refresh; } + } diff --git a/application.hpp b/application.hpp index 2e9875fb2b813f6a264bab00a82aa45e547c16f6..e0b71899c4b0c5c4a7de273727ba7b2e538ffb69 100644 --- a/application.hpp +++ b/application.hpp @@ -2,11 +2,15 @@ #define APPLICATION_HPP #include <vector> #include "vector" -#include <iostream> +#include "menu.hpp" + + class Widget; +class SpinBox; + class Application { public: @@ -16,9 +20,16 @@ public: virtual void registerWidget(Widget*); void ellenoriz(std::vector<Widget> v); + void board(int ); + + + + protected: std::vector<Widget*> widgets; + std::vector<std::vector<SpinBox*>> widgets2; + }; #endif diff --git a/main.cpp b/main.cpp index b75b9553e543c42cad3fca0a934690c561e52a7f..f8cc5606b8f636535c918ae4101d5bc349eb1081 100644 --- a/main.cpp +++ b/main.cpp @@ -1,69 +1,13 @@ - #include "application.hpp" -#include "spinbox.hpp" -#include <vector> - -using namespace genv; using namespace std; -class MyApp : public Application -{ -public: - MyApp() - { - - } - - void palya() - { - vector<vector<int>> board = { - {5, 3, 0, 0, 7, 0, 0, 0, 0}, - {6, 0, 0, 1, 9, 5, 0, 0, 0}, - {0, 9, 8, 0, 0, 0, 0, 6, 0}, - {8, 0, 0, 0, 6, 0, 0, 0, 3}, - {4, 0, 0, 8, 0, 3, 0, 0, 1}, - {7, 0, 0, 0, 2, 0, 0, 0, 6}, - {0, 6, 0, 0, 0, 0, 2, 8, 0}, - {0, 0, 0, 4, 1, 9, 0, 0, 5}, - {0, 0, 0, 0, 8, 0, 0, 7, 9} - }; - - for (int i = 0; i < 9; ++i) - { - vector<SpinBox*> rowWidgets; - - for (int j = 0; j < 9; ++j) - { - SpinBox* spin; - if (board[i][j] != 0) - { - spin = new SpinBox(this, 5 + j * 100, 5 + i * 100, 100, board[i][j], board[i][j], board[i][j]); - } - else - { - spin = new SpinBox(this, 5 + j * 100, 5 + i * 100, 100, 0, 9,0); - } - rowWidgets.push_back(spin); - } - - - widgets.push_back(rowWidgets); - } - } - -protected: - vector<vector<SpinBox*>> widgets; -}; - int main() { - MyApp app; - app.palya(); - app.event_loop(); - + Application app; + app.event_loop(); return 0; } diff --git a/menu.cpp b/menu.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fe0e07d19734fac08c5ac64abd125d41cb39ad8b --- /dev/null +++ b/menu.cpp @@ -0,0 +1,47 @@ +#include "menu.hpp" +#include "graphics.hpp" + +using namespace genv; + +Menu::Menu() : diffnum(0) {} + +void Menu::draw() { + + gout.load_font("LiberationSans-Regular.ttf", 30); + + gout << move_to(20, 20) << color(255, 255, 255) << text("Sudoku, please select a difficulty"); + gout << move_to(50, 50) << color(255, 255, 255) << box(200, 50); + gout << move_to(120, 80) << color(0, 0, 0) << text("Easy (e)"); + + gout << move_to(50, 120) << color(255, 255, 255) << box(200, 50); + gout << move_to(110, 150) << color(0, 0, 0) << text("Medium (m)"); + + gout << move_to(50, 190) << color(255, 255, 255) << box(200, 50); + gout << move_to(120, 220) << color(0, 0, 0) << text("Hard (h)"); + + gout << refresh; +} + +void Menu::handle(genv::event ev) { + if (ev.type == ev_key) + { + if (ev.keycode == 'e') + { + diffnum = 1; + } + else if (ev.keycode == 'm') + { + diffnum = 2; + } + else if (ev.keycode == 'h') + { + diffnum = 3; + } + + + } +} + +int Menu::diffret() { + return diffnum; +} diff --git a/menu.hpp b/menu.hpp new file mode 100644 index 0000000000000000000000000000000000000000..4bdef761728155b76d976e506ef537c35107603f --- /dev/null +++ b/menu.hpp @@ -0,0 +1,19 @@ +#ifndef MENU_HPP +#define MENU_HPP +#include "graphics.hpp" + + +class Menu +{ + +protected: + int diffnum; + +public: + Menu(); + void draw(); + void handle(genv::event ev); + int diffret(); +}; + +#endif // MENU_HPP diff --git a/spinbox.cpp b/spinbox.cpp index bab81c3d24b8f885d8370bd1d2fabc91938fb2d4..812a326c9ffbf9cff97c174e49a63ba176cdc4ce 100644 --- a/spinbox.cpp +++ b/spinbox.cpp @@ -2,20 +2,17 @@ #include "graphics.hpp" #include "widget.hpp" - using namespace genv; using namespace std; -int mouse_x = 0; -int mouse_y = 0; - -SpinBox::SpinBox(Application* parent,int x, int y, int sizey, int min, int max, int start) : Widget(parent,x,y,sizey*3,sizey), _min(min), _max(max), _num(start), _isselected(0) -{ - -} +SpinBox::SpinBox(Application* parent,int x, int y, int sizey, int min, int max, int start, int r,int g, int b) : Widget(parent,x,y,sizey*3,sizey), _min(min), _max(max), _num(start),_r(r), + _g(g), _b(b), _isselected(0) +{} void SpinBox::draw() { + gout.load_font("LiberationSans-Regular.ttf", 40); + _sizex = _sizey; gout << move_to (_x,_y) << color(50,50,50) << box(_sizex,_sizey); @@ -29,12 +26,19 @@ void SpinBox::draw() int text_height = gout.cascent() + gout.cdescent(); - gout << move_to(_x + (_sizex - text_width) / 2, _y + (_sizey - text_height) / 2 + gout.cascent()) - << color(255, 0, 0) - << text(to_string(_num)); + if(_num != 0) + { + gout << move_to(_x + (_sizex - text_width) / 2, _y + (_sizey - text_height) / 2 + gout.cascent()) + << color(255, 0, 0) + << text(to_string(_num)); + } + + } void SpinBox::draw2() { + gout.load_font("LiberationSans-Regular.ttf", 40); + _sizex = _sizey; gout << move_to (_x,_y) << color(50,50,50) << box(_sizex,_sizey); @@ -48,9 +52,12 @@ void SpinBox::draw2() int text_height = gout.cascent() + gout.cdescent(); + if(_num != 0) + { gout << move_to(_x + (_sizex - text_width) / 2, _y + (_sizey - text_height) / 2 + gout.cascent()) - << color(0, 255, 0) - << text(to_string(_num)); + << color(_r, _g, _b) + << text(to_string(_num)); + } } bool SpinBox::isover(int ms_x, int ms_y) diff --git a/spinbox.hpp b/spinbox.hpp index f0411349bce278937c3382091d91874956f4a951..1e3b3ba7412b2ac8ba8605947b8f09201edef1c4 100644 --- a/spinbox.hpp +++ b/spinbox.hpp @@ -6,11 +6,11 @@ class SpinBox : public Widget { protected: - int _min, _max, _num; + int _min, _max, _num, _r, _g, _b; bool _isselected; public: - SpinBox(Application* ,int, int, int,int, int,int); + SpinBox(Application* ,int, int, int,int, int,int, int, int, int); void draw() override; void handle(genv::event) override;