From 29e1295300ec85da263264870559c3c7ca3d8457 Mon Sep 17 00:00:00 2001 From: "fodor.agoston" <fodor.agoston@hallgato.ppke.hu> Date: Mon, 20 May 2024 03:40:34 +0200 Subject: [PATCH] =?UTF-8?q?v=C3=A9gleges=20verzi=C3=B3,=20j=C3=A1t=C3=A9km?= =?UTF-8?q?ester=20oszt=C3=A1llyal=20is?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application.cpp | 121 +++++++++--------------------------------------- application.hpp | 11 ++--- gamemaster.cpp | 101 ++++++++++++++++++++++++++++++++++++++++ gamemaster.hpp | 29 ++++++++++++ menu.cpp | 5 +- menu.hpp | 2 +- spinbox.cpp | 22 ++++++++- 7 files changed, 182 insertions(+), 109 deletions(-) create mode 100644 gamemaster.cpp create mode 100644 gamemaster.hpp diff --git a/application.cpp b/application.cpp index 9d48dda..0eeb984 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 b269078..7e5b7b3 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 0000000..fba861b --- /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 0000000..a8a7fd7 --- /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 7856a68..74099b8 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 4bdef76..34eead1 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 f270908..8fb4ea8 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)); } } -- GitLab