diff --git a/application.cpp b/application.cpp index 9d48dda82ab0448c876da5d7541d4ff59841a71b..0eeb984f852a16be061651d0add2a4fd311ab60e 100644 --- a/application.cpp +++ b/application.cpp @@ -3,12 +3,13 @@ #include "widget.hpp" #include "menu.hpp" #include "spinbox.hpp" +#include <iostream> using namespace genv; using namespace std; -Application::Application() +Application::Application() : game_state(0) { gout.open(910,910); @@ -80,6 +81,7 @@ void Application :: board(int diffi) 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); @@ -88,24 +90,15 @@ void Application :: board(int diffi) { 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); } } -bool Application::check_board() { - for (int i = 0; i < 9; ++i) { - for (int j = 0; j < 9; ++j) { - if (widgets2[i][j]->getter() != board_values[i][j]) { - return false; - } - } - } - return true; -} + void Application::event_loop() @@ -117,24 +110,27 @@ void Application::event_loop() event ev; - while (m.diffret() == 0 and gin >> ev) + + while (game_state == 0 and gin >> ev) { m.draw(); - m.handle(ev); + m.handle(ev, game_state); + gout << refresh; - } - board(m.diffret()); + } - while(gin >> ev) - { + board(m.diffret()); + while(gin >> ev) + { + cout << game_state << endl; if (ev.type == ev_mouse && ev.button==btn_left) { for (size_t i=0; i<widgets.size(); i++) @@ -147,72 +143,12 @@ void Application::event_loop() } wrongi = -1; - if (focus != -1) { + if (focus != -1) + { widgets[focus]->handle(ev); - for (int k = 0; k < 81; k += 9) { - for (int i = k; i < 9 + k; ++i) { - for (int j = i + 1; j < 9 + k; ++j) { - if (widgets[i]->getter() != 0 && widgets[i]->getter() == widgets[j]->getter()) { - wrongi = i; - break; - } - } - if (wrongi != -1) { - break; - } - } - if (wrongi != -1) { - break; - } - } + gm.checker(widgets, wrongi); - for (int k = 0; k < 9; ++k) { - 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 = j; - break; - } - } - if (wrongi != -1) { - break; - } - } - if (wrongi != -1) { - break; - } - } - - - 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); - - for (int i = row_offset; i < row_offset + 3; ++i) { - for (int j = col_offset; j < col_offset + 3; ++j) { - int num = widgets[i * 9 + j]->getter(); - if (num != 0) { - if (seen[num]) { - wrongi = i * 9 + j; - break; - } else { - seen[num] = true; - } - } - } - if (wrongi != -1) { - break; - } - } - if (wrongi != -1) { - break; - } - } - if (wrongi != -1) { - break; - } - } - } + } for (int i = 0; i < widgets.size(); ++i) { @@ -224,35 +160,22 @@ void Application::event_loop() } } + if(ev.type == ev_key and ev.keycode == key_escape) { break; } - int rowsum = 0; - int colsum = 0; - for (int i = 0; i < 9; ++i) - { - for (int j = 0; j < 9; ++j) - { - rowsum += widgets2[i][j]->getter(); - } - } - for (int j = 0; j < 9; ++j) - { - for (int i = 0; i < 9; ++i) - { - colsum += widgets2[i][j]->getter(); - } - } - if(colsum == 9*45 and rowsum == 9*45 and wrongi == -1) + if(gm.game_end(widgets2, wrongi)) { break; } + gout << refresh; } } + diff --git a/application.hpp b/application.hpp index b2690788c530fd705b9f6dc28491d67bb06acf83..7e5b7b3fab994723fdf693cb8d46b91fd34a6157 100644 --- a/application.hpp +++ b/application.hpp @@ -1,8 +1,9 @@ #ifndef APPLICATION_HPP #define APPLICATION_HPP #include <vector> -#include "vector" #include "menu.hpp" +#include "gamemaster.hpp" + @@ -19,18 +20,16 @@ public: virtual void event_loop(); virtual void registerWidget(Widget*); void ellenoriz(std::vector<Widget> v); - bool check_board(); void board(int ); - - - - protected: std::vector<Widget*> widgets; std::vector<std::vector<SpinBox*>> widgets2; Menu m; std::vector<std::vector<int>> board_values; + GameMaster gm; + int game_state; + }; diff --git a/gamemaster.cpp b/gamemaster.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fba861b39378c7eabffd3ebe3b19d83e88ece167 --- /dev/null +++ b/gamemaster.cpp @@ -0,0 +1,101 @@ +#include "gamemaster.hpp" +#include "widget.hpp" +#include "spinbox.hpp" +using namespace std; + +GameMaster::GameMaster() {} + + +void GameMaster::checker(vector<Widget*> widgets, int &wrongi) +{ + for (int k = 0; k < 81; k += 9) { + for (int i = k; i < 9 + k; ++i) { + for (int j = i + 1; j < 9 + k; ++j) { + if (widgets[i]->getter() != 0 && widgets[i]->getter() == widgets[j]->getter()) { + wrongi = i; + break; + } + } + if (wrongi != -1) { + break; + } + } + if (wrongi != -1) { + break; + } + } + + for (int k = 0; k < 9; ++k) { + 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 = j; + break; + } + } + if (wrongi != -1) { + break; + } + } + if (wrongi != -1) { + break; + } + } + + + 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); + + for (int i = row_offset; i < row_offset + 3; ++i) { + for (int j = col_offset; j < col_offset + 3; ++j) { + int num = widgets[i * 9 + j]->getter(); + if (num != 0) { + if (seen[num]) { + wrongi = i * 9 + j; + break; + } else { + seen[num] = true; + } + } + } + if (wrongi != -1) { + break; + } + } + if (wrongi != -1) { + break; + } + } + if (wrongi != -1) { + break; + } + } +} +bool GameMaster::game_end(vector<vector<SpinBox*>> widgets2, int wrongi) +{ + int rowsum = 0; + int colsum = 0; + for (int i = 0; i < 9; ++i) + { + for (int j = 0; j < 9; ++j) + { + rowsum += widgets2[i][j]->getter(); + } + } + for (int j = 0; j < 9; ++j) + { + for (int i = 0; i < 9; ++i) + { + colsum += widgets2[i][j]->getter(); + } + } + if(colsum == 9*45 and rowsum == 9*45 and wrongi == -1) + { + return true; + } + else + { + return false; + } +} diff --git a/gamemaster.hpp b/gamemaster.hpp new file mode 100644 index 0000000000000000000000000000000000000000..a8a7fd7a68bff70842e2790c4f617619572972bd --- /dev/null +++ b/gamemaster.hpp @@ -0,0 +1,29 @@ +#ifndef GAMEMASTER_HPP +#define GAMEMASTER_HPP +#include <vector> + + +using namespace std; + +class Widget; + +class SpinBox; + +class GameMaster +{ + +protected: + + +public: + GameMaster(); + + void checker(vector<Widget*> widgets, int &wrongi); + bool game_end(vector<vector<SpinBox*>>, int wrongi); + + + + +}; + +#endif // GAMEMASTER_HPP diff --git a/menu.cpp b/menu.cpp index 7856a68c28df34e224cd8ea37a694bc179ddd4a2..74099b82403fffec46dfdb8c77fa2cd5edd6fd45 100644 --- a/menu.cpp +++ b/menu.cpp @@ -22,20 +22,23 @@ void Menu::draw() { gout << refresh; } -void Menu::handle(genv::event ev) { +void Menu::handle(genv::event ev, int &state) { if (ev.type == ev_key) { if (ev.keycode == 'e') { diffnum = 1; + state = 1; } else if (ev.keycode == 'm') { diffnum = 2; + state = 1; } else if (ev.keycode == 'h') { diffnum = 3; + state = 1; } } } diff --git a/menu.hpp b/menu.hpp index 4bdef761728155b76d976e506ef537c35107603f..34eead178b6484d02c9989473e09906b3223b81e 100644 --- a/menu.hpp +++ b/menu.hpp @@ -12,7 +12,7 @@ protected: public: Menu(); void draw(); - void handle(genv::event ev); + void handle(genv::event ev, int &state); int diffret(); }; diff --git a/spinbox.cpp b/spinbox.cpp index f270908fd59b7fc20f0c33f1f7d2cba44c36834d..8fb4ea846e7805bbac0db4a483cec09c8a9cb7da 100644 --- a/spinbox.cpp +++ b/spinbox.cpp @@ -22,6 +22,15 @@ void SpinBox::draw() gout << move_to(_x +2*_sizey + 1, _y + 1) << color(0,0,0) << box(_sizey - 2, _sizey -2); + for (int i = 1; i < 3; ++i) + { + gout << move_to(5 + i* 300,5) << color(50,50,50) << box(5,900); + } + for (int i = 1; i < 3; ++i) + { + gout << move_to(5,5+ i* 300) << color(50,50,50) << box(900,5); + } + int text_width = gout.twidth(to_string(_num)); @@ -49,14 +58,23 @@ void SpinBox::draw2() gout << move_to(_x +2*_sizey + 1, _y + 1) << color(0,0,0) << box(_sizey - 2, _sizey -2); + for (int i = 1; i < 3; ++i) + { + gout << move_to(5 + i* 300,5) << color(50,50,50) << box(5,900); + } + for (int i = 1; i < 3; ++i) + { + gout << move_to(5,5+ i* 300) << color(50,50,50) << box(900,5); + } + int text_width = gout.twidth(to_string(_num)); 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(_r, _g, _b) + gout << move_to(_x + (_sizex - text_width) / 2, _y + (_sizey - text_height) / 2 + gout.cascent()) + << color(_r, _g, _b) << text(to_string(_num)); } }