diff --git a/application.cpp b/application.cpp index 93d70b9f3a25bd82767194d72bd347ae74f6a087..0e3f8619a10870f2c8788c7354d4393d826fbc46 100644 --- a/application.cpp +++ b/application.cpp @@ -1,6 +1,9 @@ #include "application.hpp" #include "widget.hpp" #include "gamemaster.hpp" +#include <set> + +#define focus_index 9*focus.sor+focus.oszlop using namespace genv; @@ -13,7 +16,20 @@ void Application::event_loop() { Index focus(0,0); event ev; - widgets[0]->kezel(ev); + widgets[focus_index]->kezel(ev); + + std::set<int> aura_halmaz = _game->aura(focus); // azert van itt is, hogy a megnyitaskor is rajzoljon aurat + for (int i=0; i<widgets.size(); i++) + { + if (aura_halmaz.find(i) != aura_halmaz.end()) + { + action("aura",i,1); + } + else + { + action("aura",i,0); + } + } for (Widget *w : widgets) { @@ -28,7 +44,7 @@ void Application::event_loop() if (ev.keycode==key_up || ev.keycode==key_down || ev.keycode==key_left || ev.keycode==key_right) { - widgets[9*focus.sor+focus.oszlop]->unfocus(); + widgets[focus_index]->unfocus(); if (ev.keycode==key_up) { @@ -51,17 +67,31 @@ void Application::event_loop() focus.oszlop += 1; } - widgets[9*focus.sor+focus.oszlop]->kezel(ev); + std::set<int> aura_halmaz = _game->aura(focus); + + for (int i=0; i<widgets.size(); i++) + { + if (aura_halmaz.find(i) != aura_halmaz.end()) + { + action("aura",i,1); + } + else + { + action("aura",i,0); + } + } + + widgets[focus_index]->kezel(ev); } if (ev.keycode >= 49 && ev.keycode <= 57) { - action(9*focus.sor+focus.oszlop, ev.keycode-48); + action("ertek",focus_index, ev.keycode-48); } if (ev.keycode==key_delete || ev.keycode==key_backspace) { - action(9*focus.sor+focus.oszlop, 0); + action("ertek",focus_index, 0); } } @@ -71,7 +101,7 @@ void Application::event_loop() } gout << refresh; - _game->save(); + _game->save_fajlba(); } } diff --git a/application.hpp b/application.hpp index 0e66d3aebd2b43885519409402593ce8fc629c44..9b664d3b384e3d626c9a0c7188239e5d4c18de99 100644 --- a/application.hpp +++ b/application.hpp @@ -2,6 +2,7 @@ #define APPLICATION_HPP #include "graphics.hpp" +#include <iostream> #include <vector> class Widget; @@ -34,7 +35,7 @@ class Application virtual void event_loop(); virtual void register_widget(Widget*); virtual void register_gm(GameMaster*); - virtual void action(int,int) = 0; + virtual void action(std::string,int,int) = 0; protected: std::vector<Widget*> widgets; diff --git a/field.cpp b/field.cpp index 4e11fd60aa5f262e50f3bb3b3538f69ab1daabcf..bc9316502f03ff2034f961e932881c4ea5b00492 100644 --- a/field.cpp +++ b/field.cpp @@ -1,6 +1,5 @@ #include "field.hpp" #include <string> -#include <iostream> using namespace genv; @@ -75,13 +74,19 @@ void Field::rajzol() const gout << move_to(_x+(_szel-gout.twidth(std::to_string(_ertek)))/2, _y+(_mag+gout.cascent())/2) << text(std::to_string(_ertek)); + + //gout << text(" ") << text(std::to_string(_aura)); } } void Field::kezel(genv::event ev) { _kijelolt=!_kijelolt; - std::cout << _ertek << std::endl; +} + +int Field::get_ertek() +{ + return _ertek; } void Field::set_ertek(int uj) @@ -89,3 +94,13 @@ void Field::set_ertek(int uj) if (!_fix) _ertek=uj; } + +void Field::set_aura_true() +{ + _aura=true; +} + +void Field::set_aura_false() +{ + _aura=false; +} diff --git a/field.hpp b/field.hpp index e6a632c602390620912ab0f3af5c43d48cadf3c9..cd020825c3d11ae56aac502ccfbe38f51068abf7 100644 --- a/field.hpp +++ b/field.hpp @@ -11,7 +11,11 @@ class Field : public Widget virtual void rajzol() const override; virtual void kezel(genv::event) override; + virtual int get_ertek(); + virtual void set_ertek(int); + virtual void set_aura_true(); + virtual void set_aura_false(); protected: int _ertek; diff --git a/gamemaster.cpp b/gamemaster.cpp index 5b9e5218a5924a80b8d47e07a09cd8ac00b1d818..3f95118d369537474aee9ef58ec5c4dd205c5ace 100644 --- a/gamemaster.cpp +++ b/gamemaster.cpp @@ -1,5 +1,8 @@ #include "gamemaster.hpp" +#define jelenlegi_i 9*jelenlegi.sor+jelenlegi.oszlop +#define sorszam_i 9*sorszam.sor+sorszam.oszlop + using namespace std; GameMaster::GameMaster(Application* parent, std::string mo, std::string fe) : @@ -10,13 +13,48 @@ GameMaster::GameMaster(Application* parent, std::string mo, std::string fe) : _feladat = fajl_beolvasas(_fajl_feladat); _save = _feladat; _fajl_save = "save.txt"; -} + // megmondja hogy melyik indexek melyik nagy negyzethez tartoznak + _negyzetek.resize(9); + for (int j=0; j<9; j++) // sor + { + for (int i=0; i<9; i++) // oszlop + { + Index sorszam(j,i); + int negyzet = negyzet_szama(sorszam); + _negyzetek[negyzet].push_back(sorszam_i); + } + } +} -vector<Index> GameMaster::aura(Index jelenlegi) +set<int> GameMaster::aura(Index jelenlegi) { + set<int> s; + + int negyzet=negyzet_szama(jelenlegi); + + for (int i=0; i<9; i++) + { + Index sorszam(jelenlegi.sor,i); + s.insert(sorszam_i); + } + for (int i=0; i<9; i++) + { + Index sorszam(i,jelenlegi.oszlop); + s.insert(sorszam_i); + } + + for (int i=0; i<9; i++) + { + int sorszam = _negyzetek[negyzet][i]; + s.insert(sorszam); + } + + s.erase(jelenlegi_i); + + return s; } int GameMaster::negyzet_szama(Index jelenlegi) @@ -50,9 +88,10 @@ int GameMaster::negyzet_szama(Index jelenlegi) } } -void GameMaster::action() +void GameMaster::save(int sorszam, int ertek) { - + _save[sorszam]=ertek; + save_fajlba(); } bool GameMaster::szabalyos_e(Index jelenlegi) @@ -86,7 +125,7 @@ std::vector<int> GameMaster::fajl_beolvasas(std::string fajlnev) return v; } -void GameMaster::save() +void GameMaster::save_fajlba() { fajl_kiiras(_fajl_save,_save); } diff --git a/gamemaster.hpp b/gamemaster.hpp index c61648d5a5f7f8d4c19c648c7bd2fbbc6a64b519..99bcadb35fc6e7ec77c596e4fe985f2ab1c3e1d9 100644 --- a/gamemaster.hpp +++ b/gamemaster.hpp @@ -3,6 +3,7 @@ #include "application.hpp" #include <vector> +#include <set> #include <fstream> #include <iostream> // for debug @@ -10,19 +11,24 @@ class GameMaster { public: GameMaster(Application*,std::string mo, std::string fe); + std::vector<int> fajl_beolvasas(std::string fajlnev); void fajl_kiiras(std::string fajlnev, std::vector<int> v) const; - void save(); + void save_fajlba(); + std::vector<int> get_save(); - std::vector<Index> aura(Index); + virtual void save(int,int); + + std::set<int> aura(Index); virtual int negyzet_szama(Index); virtual bool szabalyos_e(Index); - virtual void action(); + protected: std::string _fajl_megoldas, _fajl_feladat, _fajl_save; std::vector<int> _megoldas, _feladat, _save; Application *_parent; + std::vector<std::vector<int>> _negyzetek; }; #endif // GAMEMASTER_HPP diff --git a/main.cpp b/main.cpp index 9a548edb48b96d121486384d69962ccfaac62824..5b495895181ea0e7931f8c607b0bf52629e637e4 100644 --- a/main.cpp +++ b/main.cpp @@ -16,9 +16,9 @@ class MyApp : public Application game = new GameMaster(this,"mo1.txt","fe1.txt"); vector<int> game_kezdo = game->get_save(); - for (int j=0; j<9; j++) + for (int j=0; j<9; j++) // sor { - for (int i=0; i<9; i++) + for (int i=0; i<9; i++) // oszlop { Field *f = new Field(this,i*78,j*78,78,9*j+i,game_kezdo[9*j+i]); palya.push_back(f); @@ -26,10 +26,21 @@ class MyApp : public Application } } - void action(int i, int be) + void action(std::string id, int sorszam, int ertek) override { - palya[i]->set_ertek(be); - game->action(); + if (id=="ertek") + { + palya[sorszam]->set_ertek(ertek); + game->save(sorszam,ertek); + } + + if (id=="aura") + { + if (ertek==1) + palya[sorszam]->set_aura_true(); + else if (ertek==0) + palya[sorszam]->set_aura_false(); + } } protected: diff --git a/widget.hpp b/widget.hpp index 2da5101ca9e9640656d545b7e3759d2746dc3e79..16170b88c17bd865ca96d89b6ba253d557d9c879 100644 --- a/widget.hpp +++ b/widget.hpp @@ -4,12 +4,12 @@ #include "graphics.hpp" #include "application.hpp" -#define dark_purple 56,0,102 -#define mid_purple 133,133,255 -#define light_purple 204,204,255 -#define dark_grey 51,51,51 -#define mid_grey 191,191,191 -#define light_grey 230,230,230 +#define dark_purple 130,38,184 +#define mid_purple 194,135,230 +#define light_purple 220,186,241 +#define dark_grey 111,111,111 +#define mid_grey 183,183,183 +#define light_grey 214,214,214 class Widget {