From e8769d4d3cc5ad106d79342b49fa3679ceb4de00 Mon Sep 17 00:00:00 2001 From: Dpbm Date: Thu, 30 May 2024 18:48:35 -0300 Subject: [PATCH 1/5] creating qubo class --- qubo/qubo.cpp | 12 ++++++++++++ qubo/qubo.h | 14 ++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 qubo/qubo.cpp create mode 100644 qubo/qubo.h diff --git a/qubo/qubo.cpp b/qubo/qubo.cpp new file mode 100644 index 0000000..44beaf0 --- /dev/null +++ b/qubo/qubo.cpp @@ -0,0 +1,12 @@ +#include "qubo.h" +#include + +namespace Qubo{ + QuboFunc::QuboFunc(double P){ + this->P = P; + } + + double QuboFunc::evaluate(double q1, double q2, double q3, uint8_t x1, uint8_t x2, uint8_t x3){ + return (q1*x1 + q2*x2 + q3*x3); + } +}; diff --git a/qubo/qubo.h b/qubo/qubo.h new file mode 100644 index 0000000..ccbf2e8 --- /dev/null +++ b/qubo/qubo.h @@ -0,0 +1,14 @@ +#pragma once + +#include + +namespace Qubo { + class QuboFunc{ + public: + QuboFunc(double P); + double evaluate(double q1, double q2, double q3, uint8_t x1, uint8_t x2, uint8_t x3); + + private: + double P; + }; +}; From 000b81b7d5587f43abee3157c554eda90c0f2f2a Mon Sep 17 00:00:00 2001 From: Dpbm Date: Thu, 30 May 2024 22:25:45 -0300 Subject: [PATCH 2/5] addded qubo player --- CMakeLists.txt | 8 +- game/players/qubo_player.cpp | 168 +++++++++++++++++++++++++++++++++++ game/players/qubo_player.h | 41 +++++++++ helpers/utils.cpp | 9 +- helpers/utils.h | 1 + qubo/CMakeLists.txt | 0 qubo/qubo.cpp | 32 ++++++- qubo/qubo.h | 14 ++- 8 files changed, 266 insertions(+), 7 deletions(-) create mode 100644 game/players/qubo_player.cpp create mode 100644 game/players/qubo_player.h create mode 100644 qubo/CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt index f076620..5f832af 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -25,6 +25,7 @@ add_subdirectory(game) add_subdirectory(genetic) add_subdirectory(matrix) add_subdirectory(machine) +add_subdirectory(qubo) add_subdirectory(nativefiledialog-extended) file(GLOB HELPERS_FILES "helpers/*.cpp") @@ -38,6 +39,9 @@ target_link_libraries(genetic players matrix machine board helpers) file(GLOB MATRIX_FILES "matrix/*.cpp") add_library(matrix ${MATRIX_FILES}) +file(GLOB QUBO_FILES "qubo/*.cpp") +add_library(qubo ${QUBO_FILES}) + file(GLOB MACHINE_FILES "machine/*.cpp") add_library(machine ${MACHINE_FILES}) target_link_libraries(machine helpers matrix) @@ -48,8 +52,8 @@ file(GLOB PLAYERS_FILES "game/players/*.cpp") add_library(board ${BOARD_FILE}) add_library(screens ${SCREENS_FILES}) add_library(players ${PLAYERS_FILES}) -target_link_libraries(players machine matrix genetic helpers ${SDL2_LIBRARIES}) -target_link_libraries(screens players board helpers genetic matrix ${SDL2_LIBRARIES} SDL2_ttf nfd) +target_link_libraries(players machine matrix genetic helpers qubo ${SDL2_LIBRARIES}) +target_link_libraries(screens players board helpers genetic matrix qubo ${SDL2_LIBRARIES} SDL2_ttf nfd) target_link_libraries(board helpers players) target_link_libraries(snake PRIVATE helpers board screens players ${SDL2_LIBRARIES} SDL2_ttf) diff --git a/game/players/qubo_player.cpp b/game/players/qubo_player.cpp new file mode 100644 index 0000000..e920ecb --- /dev/null +++ b/game/players/qubo_player.cpp @@ -0,0 +1,168 @@ +#include +#include "qubo_player.h" +#include "../../qubo/qubo.h" +#include "../../helpers/utils.h" + +using Qubo::QuboFunc; +using Utils::vec2; +using Utils::distance; + +namespace Players { + QuboPlayer::QuboPlayer(uint8_t board_w, uint8_t board_h) : Player(board_w, board_h){ + this->board_w = board_w; + this->board_h = board_h; + this->qubo = new QuboFunc((board_h*board_h) + 10); + } + + bool QuboPlayer::will_collide_itself(int16_t x, int16_t y){ + bool will_collide = false; + Node* actual_bpart = this->head->next; + while(actual_bpart != nullptr){ + + if(actual_bpart->value.x == x && + actual_bpart->value.y == y){ + will_collide = true; + break; + } + actual_bpart = actual_bpart->next; + } + return will_collide; + } + + bool QuboPlayer::will_collide_top_border(){ + return this->get_x() <= 0; + } + + bool QuboPlayer::will_collide_bottom_border(){ + return this->get_x() >= this->board_h-1; + } + + bool QuboPlayer::will_collide_left_border(){ + return this->get_y() <= 0; + } + + bool QuboPlayer::will_collide_right_border(){ + return this->get_x() >= this->board_w-1; + } + + void QuboPlayer::next_mov(const vec2& food){ + double q0 = 10000.0; //distance forward + double q1 = 10000.0; //distance down/right + double q2 = 10000.0; //distance up/left + + switch (this->dir) { + case RIGHT: + this->get_distances_right(food.x, food.y, &q0, &q1, &q2); + break; + case LEFT: + this->get_distances_left(food.x, food.y, &q0, &q1, &q2); + break; + case UP: + this->get_distances_up(food.x, food.y, &q0, &q1, &q2); + break; + case DOWN: + this->get_distances_up(food.x, food.y, &q0, &q1, &q2); + break; + default: + break; + } + + uint8_t* next_mov = this->qubo->minimize(q0, q1, q2); + + if(next_mov[0] == 0 && next_mov[1] == 0 && next_mov[2] == 1) + this->move_down_right(); + else if(next_mov[0] == 0 && next_mov[1] == 1 && next_mov[2] == 0) + this->move_up_left(); + + delete next_mov; + } + + void QuboPlayer::get_distances_right(int16_t fx, int16_t fy, double* q0, double* q1, double* q2){ + if(!this->will_collide_itself(this->get_x(), this->get_y()+1) && !this->will_collide_right_border()) + *q0 = distance(this->get_x(), this->get_y()+1, fx, fy); + + if(!this->will_collide_itself(this->get_x()+1, this->get_y()) && !this->will_collide_bottom_border()) + *q1 = distance(this->get_x()+1, this->get_y(), fx, fy); + + if(!this->will_collide_itself(this->get_x()-1, this->get_y()) && !this->will_collide_top_border()) + *q2 = distance(this->get_x()-1, this->get_y(), fx, fy); + } + + + void QuboPlayer::get_distances_left(int16_t fx, int16_t fy, double* q0, double* q1, double* q2){ + if(!this->will_collide_itself(this->get_x(), this->get_y()-1) && !this->will_collide_left_border()) + *q0 = distance(this->get_x(), this->get_y()-1, fx, fy); + + if(!this->will_collide_itself(this->get_x()+1, this->get_y()) && !this->will_collide_bottom_border()) + *q1 = distance(this->get_x()+1, this->get_y(), fx, fy); + + if(!this->will_collide_itself(this->get_x()-1, this->get_y()) && !this->will_collide_top_border()) + *q2 = distance(this->get_x()-1, this->get_y(), fx, fy); + } + + void QuboPlayer::get_distances_up(int16_t fx, int16_t fy, double* q0, double* q1, double* q2){ + if(!this->will_collide_itself(this->get_x()-1, this->get_y()) && !this->will_collide_top_border()) + *q0 = distance(this->get_x()-1, this->get_y(), fx, fy); + + if(!this->will_collide_itself(this->get_x(), this->get_y()+1) && !this->will_collide_right_border()) + *q1 = distance(this->get_x(), this->get_y()+1, fx, fy); + + if(!this->will_collide_itself(this->get_x(), this->get_y()-1) && !this->will_collide_left_border()) + *q2 = distance(this->get_x(), this->get_y()-1, fx, fy); + } + + void QuboPlayer::get_distances_down(int16_t fx, int16_t fy, double* q0, double* q1, double* q2){ + if(!this->will_collide_itself(this->get_x()+1, this->get_y()) && !this->will_collide_bottom_border()) + *q0 = distance(this->get_x()+1, this->get_y(), fx, fy); + + if(!this->will_collide_itself(this->get_x(), this->get_y()+1) && !this->will_collide_right_border()) + *q1 = distance(this->get_x(), this->get_y()+1, fx, fy); + + if(!this->will_collide_itself(this->get_x(), this->get_y()-1) && !this->will_collide_left_border()) + *q2 = distance(this->get_x(), this->get_y()-1, fx, fy); + } + + + void QuboPlayer::move_down_right(){ + switch (this->dir) { + case RIGHT: + this->direction_down(); + break; + case LEFT: + this->direction_down(); + break; + case UP: + this->direction_right(); + break; + case DOWN: + this->direction_right(); + break; + default: + break; + } + } + + void QuboPlayer::move_up_left(){ + switch (this->dir) { + case RIGHT: + this->direction_up(); + break; + case LEFT: + this->direction_up(); + break; + case UP: + this->direction_left(); + break; + case DOWN: + this->direction_left(); + break; + default: + break; + } + } + + QuboPlayer::~QuboPlayer(){ + delete this->qubo; + } + +} diff --git a/game/players/qubo_player.h b/game/players/qubo_player.h new file mode 100644 index 0000000..d7f96ad --- /dev/null +++ b/game/players/qubo_player.h @@ -0,0 +1,41 @@ +#pragma once + +#include "player.h" +#include "../../qubo/qubo.h" +#include "../../helpers/utils.h" +#include + +using Qubo::QuboFunc; +using Utils::vec2; + +namespace Players{ + class QuboPlayer : public Player { + public: + QuboPlayer(uint8_t board_w, uint8_t board_h); + ~QuboPlayer(); + + void next_mov(const vec2& food); + + private: + QuboFunc* qubo = nullptr; + uint8_t board_w = 0; + uint8_t board_h = 0; + + bool will_collide_itself(int16_t x, int16_t y); + + + bool will_collide_top_border(); + bool will_collide_bottom_border(); + bool will_collide_left_border(); + bool will_collide_right_border(); + + void get_distances_right(int16_t fx, int16_t fy, double* q0, double* q1, double* q2); + void get_distances_left(int16_t fx, int16_t fy, double* q0, double* q1, double* q2); + void get_distances_up(int16_t fx, int16_t fy, double* q0, double* q1, double* q2); + void get_distances_down(int16_t fx, int16_t fy, double* q0, double* q1, double* q2); + + void move_down_right(); + void move_up_left(); + }; + +}; diff --git a/helpers/utils.cpp b/helpers/utils.cpp index 998cf6e..41d61b9 100644 --- a/helpers/utils.cpp +++ b/helpers/utils.cpp @@ -1,3 +1,4 @@ +#include #include #include #include @@ -11,6 +12,8 @@ #include "../matrix/matrix.h" #include "../machine/machine.h" +using std::pow; +using std::sqrt; using std::stod; using std::stoi; using std::vector; @@ -261,5 +264,9 @@ namespace Utils { } return row; } - + + double distance(int16_t x1, int16_t y1, int16_t x2, int16_t y2){ + return sqrt(pow(x1-x2, 2) + pow(y1-y2, 2)); + } + } diff --git a/helpers/utils.h b/helpers/utils.h index b1dca38..9b950cc 100644 --- a/helpers/utils.h +++ b/helpers/utils.h @@ -27,4 +27,5 @@ namespace Utils { uint8_t* parse_activations(string line, uint8_t total_layers); Matrix* parse_weights_head(string line); double* parse_row(string line, uint8_t width); + double distance(int16_t x1, int16_t y1, int16_t x2, int16_t y2); } diff --git a/qubo/CMakeLists.txt b/qubo/CMakeLists.txt new file mode 100644 index 0000000..e69de29 diff --git a/qubo/qubo.cpp b/qubo/qubo.cpp index 44beaf0..ddd9bb0 100644 --- a/qubo/qubo.cpp +++ b/qubo/qubo.cpp @@ -1,12 +1,38 @@ -#include "qubo.h" #include +#include +#include "qubo.h" + +using std::pow; namespace Qubo{ QuboFunc::QuboFunc(double P){ this->P = P; } - double QuboFunc::evaluate(double q1, double q2, double q3, uint8_t x1, uint8_t x2, uint8_t x3){ - return (q1*x1 + q2*x2 + q3*x3); + double QuboFunc::evaluate(double q0, double q1, double q2, uint8_t x0, uint8_t x1, uint8_t x2){ + return (q0*x0 + q1*x1 + q2*x2) - P*(pow(x0 + x1 + x2 - 1, 2)); + } + + uint8_t* QuboFunc::minimize(double q0, double q1, double q2){ + //REMEMBER TO CLEAN IT AFTER USING! + uint8_t* result = new uint8_t[3]; + double best = 1000000000000; + + for(size_t i = 0; i < 8; i++){ + uint8_t x0 = x_combinations[i][0]; + uint8_t x1 = x_combinations[i][1]; + uint8_t x2 = x_combinations[i][2]; + + double val = this->evaluate(q0, q1, q2, x0, x1, x2); + + if(val < best){ + best = val; + result[0] = x0; + result[1] = x1; + result[2] = x2; + } + } + + return result; } }; diff --git a/qubo/qubo.h b/qubo/qubo.h index ccbf2e8..0738f17 100644 --- a/qubo/qubo.h +++ b/qubo/qubo.h @@ -3,10 +3,22 @@ #include namespace Qubo { + static uint8_t x_combinations[8][3] = { + {0, 0, 0}, + {0, 0, 1}, + {0, 1, 0}, + {0, 1, 1}, + {1, 0 ,0}, + {1, 0, 1}, + {1, 1, 0}, + {1, 1, 1} + }; + class QuboFunc{ public: QuboFunc(double P); - double evaluate(double q1, double q2, double q3, uint8_t x1, uint8_t x2, uint8_t x3); + double evaluate(double q0, double q1, double q2, uint8_t x0, uint8_t x1, uint8_t x2); + uint8_t* minimize(double q0, double q1, double q2); private: double P; From f13e60e5afa1637b83afb78e1b2e46a85156309c Mon Sep 17 00:00:00 2001 From: Dpbm Date: Thu, 30 May 2024 22:32:16 -0300 Subject: [PATCH 3/5] added qubo to the start screen --- game/players/qubo_player.h | 1 - game/screens/start_screen.cpp | 8 ++++++++ game/screens/start_screen.h | 2 ++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/game/players/qubo_player.h b/game/players/qubo_player.h index d7f96ad..aefb26f 100644 --- a/game/players/qubo_player.h +++ b/game/players/qubo_player.h @@ -23,7 +23,6 @@ namespace Players{ bool will_collide_itself(int16_t x, int16_t y); - bool will_collide_top_border(); bool will_collide_bottom_border(); bool will_collide_left_border(); diff --git a/game/screens/start_screen.cpp b/game/screens/start_screen.cpp index e35baba..f11508c 100644 --- a/game/screens/start_screen.cpp +++ b/game/screens/start_screen.cpp @@ -28,21 +28,25 @@ namespace Screens { SDL_Surface* ai_surface = TTF_RenderText_Solid(this->font, "PRESS 'A' TO TRAIN THE AI", this->text_color); SDL_Surface* ai_play_surface = TTF_RenderText_Solid(this->font, "PRESS 'S' TO AI PLAY", this->text_color); SDL_Surface* player_surface = TTF_RenderText_Solid(this->font, "PRESS 'D' TO PLAY", this->text_color); + SDL_Surface* qubo_surface = TTF_RenderText_Solid(this->font, "PRESS 'Q' TO QUBO PLAY", this->text_color); this->title_texture = SDL_CreateTextureFromSurface(render, title_surface); this->ai_texture = SDL_CreateTextureFromSurface(render, ai_surface); this->ai_play_texture = SDL_CreateTextureFromSurface(render, ai_play_surface); this->player_texture = SDL_CreateTextureFromSurface(render, player_surface); + this->qubo_texture = SDL_CreateTextureFromSurface(render, qubo_surface); this->title_shape = SDL_Rect{(WIDTH/2) - (title_surface->w/2), 60, title_surface->w, title_surface->h}; this->ai_shape = SDL_Rect{(WIDTH/2) - (ai_surface->w/2), 240, ai_surface->w, ai_surface->h}; this->ai_play_shape = SDL_Rect{(WIDTH/2) - (ai_play_surface->w/2), this->ai_shape.y+this->ai_shape.h+20, ai_play_surface->w, ai_play_surface->h}; this->player_shape = SDL_Rect{(WIDTH/2) - (player_surface->w/2), this->ai_play_shape.y+this->ai_play_shape.h+20, player_surface->w, player_surface->h}; + this->qubo_shape = SDL_Rect{(WIDTH/2) - (qubo_surface->w/2), this->player_shape.y+this->player_shape.h+20, qubo_surface->w, qubo_surface->h}; SDL_FreeSurface(title_surface); SDL_FreeSurface(ai_surface); SDL_FreeSurface(ai_play_surface); SDL_FreeSurface(player_surface); + SDL_FreeSurface(qubo_surface); } Screen* StartScreen::key_event(const SDL_Keycode& key){ @@ -53,6 +57,8 @@ namespace Screens { return new AIPlayScreen(this->render); case SDLK_d: return new PlayerScreen(this->render); + case SDLK_q: + return nullptr; default: break; } @@ -65,6 +71,7 @@ namespace Screens { SDL_RenderCopy(this->render, this->ai_texture, NULL, &this->ai_shape); SDL_RenderCopy(this->render, this->ai_play_texture, NULL, &this->ai_play_shape); SDL_RenderCopy(this->render, this->player_texture, NULL, &this->player_shape); + SDL_RenderCopy(this->render, this->qubo_texture, NULL, &this->qubo_shape); SDL_RenderPresent(this->render); } @@ -73,5 +80,6 @@ namespace Screens { SDL_DestroyTexture(this->ai_texture); SDL_DestroyTexture(this->ai_play_texture); SDL_DestroyTexture(this->player_texture); + SDL_DestroyTexture(this->qubo_texture); } }; diff --git a/game/screens/start_screen.h b/game/screens/start_screen.h index 9ca3eb6..d09a34c 100644 --- a/game/screens/start_screen.h +++ b/game/screens/start_screen.h @@ -22,11 +22,13 @@ namespace Screens { SDL_Texture* ai_texture; SDL_Texture* player_texture; SDL_Texture* ai_play_texture; + SDL_Texture* qubo_texture; SDL_Rect title_shape; SDL_Rect ai_shape; SDL_Rect player_shape; SDL_Rect ai_play_shape; + SDL_Rect qubo_shape; }; }; From ffdce92df4d06e7dcd45f1553dcfe57f845a4357 Mon Sep 17 00:00:00 2001 From: Dpbm Date: Thu, 30 May 2024 23:21:25 -0300 Subject: [PATCH 4/5] added qubo screen --- game/players/qubo_player.cpp | 14 +++-- game/screens/qubo_screen.cpp | 115 ++++++++++++++++++++++++++++++++++ game/screens/qubo_screen.h | 43 +++++++++++++ game/screens/start_screen.cpp | 3 +- qubo/qubo.cpp | 6 +- 5 files changed, 171 insertions(+), 10 deletions(-) create mode 100644 game/screens/qubo_screen.cpp create mode 100644 game/screens/qubo_screen.h diff --git a/game/players/qubo_player.cpp b/game/players/qubo_player.cpp index e920ecb..3ed3df3 100644 --- a/game/players/qubo_player.cpp +++ b/game/players/qubo_player.cpp @@ -42,14 +42,14 @@ namespace Players { } bool QuboPlayer::will_collide_right_border(){ - return this->get_x() >= this->board_w-1; + return this->get_y() >= this->board_w-1; } void QuboPlayer::next_mov(const vec2& food){ double q0 = 10000.0; //distance forward double q1 = 10000.0; //distance down/right double q2 = 10000.0; //distance up/left - + switch (this->dir) { case RIGHT: this->get_distances_right(food.x, food.y, &q0, &q1, &q2); @@ -66,13 +66,15 @@ namespace Players { default: break; } - + + uint8_t* next_mov = this->qubo->minimize(q0, q1, q2); - + + if(next_mov[0] == 0 && next_mov[1] == 0 && next_mov[2] == 1) - this->move_down_right(); - else if(next_mov[0] == 0 && next_mov[1] == 1 && next_mov[2] == 0) this->move_up_left(); + else if(next_mov[0] == 0 && next_mov[1] == 1 && next_mov[2] == 0) + this->move_down_right(); delete next_mov; } diff --git a/game/screens/qubo_screen.cpp b/game/screens/qubo_screen.cpp new file mode 100644 index 0000000..237cb62 --- /dev/null +++ b/game/screens/qubo_screen.cpp @@ -0,0 +1,115 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include "../../helpers/constants.h" +#include "qubo_screen.h" +#include "start_screen.h" + +using std::to_string; +using std::cout; +using std::endl; +using std::size_t; + +namespace Screens { + QuboScreen::QuboScreen(SDL_Renderer* render) : Screen(render){ + if(!this->font){ + cout << "Failed on getting font!" << TTF_GetError() << endl; + exit(1); + } + + SDL_Surface* score_text_surface = TTF_RenderText_Solid(this->font, "QUBO Score", this->text_color); + this->score_text_texture = SDL_CreateTextureFromSurface(render, score_text_surface); + this->score_text_shape = SDL_Rect{20, 20, score_text_surface->w, score_text_surface->h}; + SDL_FreeSurface(score_text_surface); + + if(this->score_text_texture == nullptr){ + cout << "Failed on creating score text texture!" << SDL_GetError() << endl; + exit(1); + } + this->board.add_player(this->player); + this->left_padding = 10 * SQUARE_SIDE; + } + + void QuboScreen::execute(bool& game_loop){ + bool won = this->player->get_score() >= this->max_score; + this->finished_game = won || this->player->is_dead(); + if(this->finished_game){ + SDL_Surface* game_over_surface = TTF_RenderText_Solid(this->title_font, won ? "QUBO Wins!!!" : "Game Over", this->text_color); + SDL_Texture* game_over_texture = SDL_CreateTextureFromSurface(this->render, game_over_surface); + SDL_Rect game_over_shape = SDL_Rect{(WIDTH/2)-(game_over_surface->w/2), (HEIGHT/2)-(game_over_surface->h), game_over_surface->w, game_over_surface->h}; + SDL_FreeSurface(game_over_surface); + + SDL_Surface* reset_surface = TTF_RenderText_Solid(this->font, "Press 'r' to reset", this->text_color); + SDL_Texture* reset_texture = SDL_CreateTextureFromSurface(this->render, reset_surface); + SDL_Rect reset_shape = SDL_Rect{(WIDTH/2)-(reset_surface->w/2), (HEIGHT/2)+(reset_surface->h)+20, reset_surface->w, reset_surface->h}; + SDL_FreeSurface(reset_surface); + + SDL_Surface* back_surface = TTF_RenderText_Solid(this->font, "Press 'g' to back to the start screen", this->text_color); + SDL_Texture* back_texture = SDL_CreateTextureFromSurface(this->render, back_surface); + SDL_Rect back_shape = SDL_Rect{(WIDTH/2)-(back_surface->w/2), (HEIGHT/2)+(back_surface->h)+50, back_surface->w, back_surface->h}; + SDL_FreeSurface(back_surface); + + SDL_SetRenderDrawColor(this->render, 0, 0, 0, 255); + SDL_RenderCopy(this->render, game_over_texture, NULL, &game_over_shape); + SDL_RenderCopy(this->render, reset_texture, NULL, &reset_shape); + SDL_RenderCopy(this->render, back_texture, NULL, &back_shape); + SDL_DestroyTexture(game_over_texture); + SDL_DestroyTexture(back_texture); + SDL_DestroyTexture(reset_texture); + return; + } + this->player->next_mov(this->board.get_food()); + this->board.update_player_pos(); + this->render_board(&this->board); + + SDL_SetRenderDrawColor(this->render, 0, 0, 0, 255); + SDL_RenderCopy(this->render, this->score_text_texture, NULL, &this->score_text_shape); + + if(this->score_texture != nullptr) + SDL_DestroyTexture(this->score_texture); + SDL_Surface* score_surface = TTF_RenderText_Solid(this->font, to_string(this->player->get_score()).c_str(), this->text_color); + this->score_texture = SDL_CreateTextureFromSurface(this->render, score_surface); + this->score_shape = SDL_Rect{20, 60, score_surface->w, score_surface->h}; + SDL_FreeSurface(score_surface); + + SDL_RenderCopy(this->render, this->score_texture, NULL, &this->score_shape); + } + + Screen* QuboScreen::key_event(const SDL_Keycode& key){ + switch (key) { + case SDLK_g: + if(this->finished_game) + return new StartScreen(this->render); + + case SDLK_r: + if(this->finished_game) + this->reset(); + + default: + break; + } + return nullptr; + } + + void QuboScreen::close_event(){ + } + + void QuboScreen::reset(){ + this->finished_game = false; + delete this->player; + this->player = new QuboPlayer(this->board_w, this->board_h); + this->board.add_player(this->player); + this->board.random_food(); + } + + QuboScreen::~QuboScreen(){ + delete this->player; + SDL_DestroyTexture(this->score_texture); + SDL_DestroyTexture(this->score_text_texture); + } +} diff --git a/game/screens/qubo_screen.h b/game/screens/qubo_screen.h new file mode 100644 index 0000000..48dfce4 --- /dev/null +++ b/game/screens/qubo_screen.h @@ -0,0 +1,43 @@ +#pragma once + +#include +#include +#include +#include "screens.h" +#include "../board.h" +#include "../players/qubo_player.h" + +using Screens::Screen; +using Game::Board; +using Players::QuboPlayer; + +namespace Screens{ + class QuboScreen: public Screen{ + public: + QuboScreen(SDL_Renderer* render); + ~QuboScreen(); + void execute(bool& game_loop); + Screen* key_event(const SDL_Keycode& key); + void close_event(); + + private: + uint8_t board_w = 45; + uint8_t board_h = 30; + Board board{board_w, board_h}; + + bool finished_game = false; + + uint16_t max_score = 1000; + QuboPlayer* player = new QuboPlayer(board_w, board_h); + + TTF_Font* font = TTF_OpenFont("./assets/pressstart.ttf", 20); + TTF_Font* title_font = TTF_OpenFont("./assets/pressstart.ttf", 40); + + SDL_Rect score_text_shape; + SDL_Rect score_shape; + SDL_Texture* score_text_texture = nullptr; + SDL_Texture* score_texture = nullptr; + + void reset(); + }; +}; diff --git a/game/screens/start_screen.cpp b/game/screens/start_screen.cpp index f11508c..2cf3f45 100644 --- a/game/screens/start_screen.cpp +++ b/game/screens/start_screen.cpp @@ -12,6 +12,7 @@ #include "ai_screen_play.h" #include "player_screen.h" #include "ai_screen.h" +#include "qubo_screen.h" using std::exit; using std::cout; @@ -58,7 +59,7 @@ namespace Screens { case SDLK_d: return new PlayerScreen(this->render); case SDLK_q: - return nullptr; + return new QuboScreen(this->render); default: break; } diff --git a/qubo/qubo.cpp b/qubo/qubo.cpp index ddd9bb0..aa5524f 100644 --- a/qubo/qubo.cpp +++ b/qubo/qubo.cpp @@ -10,11 +10,11 @@ namespace Qubo{ } double QuboFunc::evaluate(double q0, double q1, double q2, uint8_t x0, uint8_t x1, uint8_t x2){ - return (q0*x0 + q1*x1 + q2*x2) - P*(pow(x0 + x1 + x2 - 1, 2)); + return (q0*x0 + q1*x1 + q2*x2) + P*(pow(x0 + x1 + x2 - 1, 2)); } uint8_t* QuboFunc::minimize(double q0, double q1, double q2){ - //REMEMBER TO CLEAN IT AFTER USING! + //REMEMBER TO CLEAN AFTER USING IT! uint8_t* result = new uint8_t[3]; double best = 1000000000000; @@ -25,7 +25,7 @@ namespace Qubo{ double val = this->evaluate(q0, q1, q2, x0, x1, x2); - if(val < best){ + if(val <= best){ best = val; result[0] = x0; result[1] = x1; From 8a33481f52d44b6a01440c8e02a0bc8a749b4820 Mon Sep 17 00:00:00 2001 From: Dpbm Date: Thu, 30 May 2024 23:21:52 -0300 Subject: [PATCH 5/5] fixed score text --- game/screens/qubo_screen.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/game/screens/qubo_screen.cpp b/game/screens/qubo_screen.cpp index 237cb62..a6f9633 100644 --- a/game/screens/qubo_screen.cpp +++ b/game/screens/qubo_screen.cpp @@ -22,7 +22,7 @@ namespace Screens { exit(1); } - SDL_Surface* score_text_surface = TTF_RenderText_Solid(this->font, "QUBO Score", this->text_color); + SDL_Surface* score_text_surface = TTF_RenderText_Solid(this->font, "Score", this->text_color); this->score_text_texture = SDL_CreateTextureFromSurface(render, score_text_surface); this->score_text_shape = SDL_Rect{20, 20, score_text_surface->w, score_text_surface->h}; SDL_FreeSurface(score_text_surface);