Skip to content

Commit ffdce92

Browse files
committed
added qubo screen
1 parent f13e60e commit ffdce92

File tree

5 files changed

+171
-10
lines changed

5 files changed

+171
-10
lines changed

game/players/qubo_player.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,14 @@ namespace Players {
4242
}
4343

4444
bool QuboPlayer::will_collide_right_border(){
45-
return this->get_x() >= this->board_w-1;
45+
return this->get_y() >= this->board_w-1;
4646
}
4747

4848
void QuboPlayer::next_mov(const vec2& food){
4949
double q0 = 10000.0; //distance forward
5050
double q1 = 10000.0; //distance down/right
5151
double q2 = 10000.0; //distance up/left
52-
52+
5353
switch (this->dir) {
5454
case RIGHT:
5555
this->get_distances_right(food.x, food.y, &q0, &q1, &q2);
@@ -66,13 +66,15 @@ namespace Players {
6666
default:
6767
break;
6868
}
69-
69+
70+
7071
uint8_t* next_mov = this->qubo->minimize(q0, q1, q2);
71-
72+
73+
7274
if(next_mov[0] == 0 && next_mov[1] == 0 && next_mov[2] == 1)
73-
this->move_down_right();
74-
else if(next_mov[0] == 0 && next_mov[1] == 1 && next_mov[2] == 0)
7575
this->move_up_left();
76+
else if(next_mov[0] == 0 && next_mov[1] == 1 && next_mov[2] == 0)
77+
this->move_down_right();
7678

7779
delete next_mov;
7880
}

game/screens/qubo_screen.cpp

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
#include <SDL2/SDL_timer.h>
2+
#include <cstddef>
3+
#include <SDL2/SDL_keycode.h>
4+
#include <SDL2/SDL_render.h>
5+
#include <SDL2/SDL_ttf.h>
6+
#include <SDL2/SDL_surface.h>
7+
#include <SDL2/SDL_stdinc.h>
8+
#include <string>
9+
#include "../../helpers/constants.h"
10+
#include "qubo_screen.h"
11+
#include "start_screen.h"
12+
13+
using std::to_string;
14+
using std::cout;
15+
using std::endl;
16+
using std::size_t;
17+
18+
namespace Screens {
19+
QuboScreen::QuboScreen(SDL_Renderer* render) : Screen(render){
20+
if(!this->font){
21+
cout << "Failed on getting font!" << TTF_GetError() << endl;
22+
exit(1);
23+
}
24+
25+
SDL_Surface* score_text_surface = TTF_RenderText_Solid(this->font, "QUBO Score", this->text_color);
26+
this->score_text_texture = SDL_CreateTextureFromSurface(render, score_text_surface);
27+
this->score_text_shape = SDL_Rect{20, 20, score_text_surface->w, score_text_surface->h};
28+
SDL_FreeSurface(score_text_surface);
29+
30+
if(this->score_text_texture == nullptr){
31+
cout << "Failed on creating score text texture!" << SDL_GetError() << endl;
32+
exit(1);
33+
}
34+
this->board.add_player(this->player);
35+
this->left_padding = 10 * SQUARE_SIDE;
36+
}
37+
38+
void QuboScreen::execute(bool& game_loop){
39+
bool won = this->player->get_score() >= this->max_score;
40+
this->finished_game = won || this->player->is_dead();
41+
if(this->finished_game){
42+
SDL_Surface* game_over_surface = TTF_RenderText_Solid(this->title_font, won ? "QUBO Wins!!!" : "Game Over", this->text_color);
43+
SDL_Texture* game_over_texture = SDL_CreateTextureFromSurface(this->render, game_over_surface);
44+
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};
45+
SDL_FreeSurface(game_over_surface);
46+
47+
SDL_Surface* reset_surface = TTF_RenderText_Solid(this->font, "Press 'r' to reset", this->text_color);
48+
SDL_Texture* reset_texture = SDL_CreateTextureFromSurface(this->render, reset_surface);
49+
SDL_Rect reset_shape = SDL_Rect{(WIDTH/2)-(reset_surface->w/2), (HEIGHT/2)+(reset_surface->h)+20, reset_surface->w, reset_surface->h};
50+
SDL_FreeSurface(reset_surface);
51+
52+
SDL_Surface* back_surface = TTF_RenderText_Solid(this->font, "Press 'g' to back to the start screen", this->text_color);
53+
SDL_Texture* back_texture = SDL_CreateTextureFromSurface(this->render, back_surface);
54+
SDL_Rect back_shape = SDL_Rect{(WIDTH/2)-(back_surface->w/2), (HEIGHT/2)+(back_surface->h)+50, back_surface->w, back_surface->h};
55+
SDL_FreeSurface(back_surface);
56+
57+
SDL_SetRenderDrawColor(this->render, 0, 0, 0, 255);
58+
SDL_RenderCopy(this->render, game_over_texture, NULL, &game_over_shape);
59+
SDL_RenderCopy(this->render, reset_texture, NULL, &reset_shape);
60+
SDL_RenderCopy(this->render, back_texture, NULL, &back_shape);
61+
SDL_DestroyTexture(game_over_texture);
62+
SDL_DestroyTexture(back_texture);
63+
SDL_DestroyTexture(reset_texture);
64+
return;
65+
}
66+
this->player->next_mov(this->board.get_food());
67+
this->board.update_player_pos();
68+
this->render_board(&this->board);
69+
70+
SDL_SetRenderDrawColor(this->render, 0, 0, 0, 255);
71+
SDL_RenderCopy(this->render, this->score_text_texture, NULL, &this->score_text_shape);
72+
73+
if(this->score_texture != nullptr)
74+
SDL_DestroyTexture(this->score_texture);
75+
SDL_Surface* score_surface = TTF_RenderText_Solid(this->font, to_string(this->player->get_score()).c_str(), this->text_color);
76+
this->score_texture = SDL_CreateTextureFromSurface(this->render, score_surface);
77+
this->score_shape = SDL_Rect{20, 60, score_surface->w, score_surface->h};
78+
SDL_FreeSurface(score_surface);
79+
80+
SDL_RenderCopy(this->render, this->score_texture, NULL, &this->score_shape);
81+
}
82+
83+
Screen* QuboScreen::key_event(const SDL_Keycode& key){
84+
switch (key) {
85+
case SDLK_g:
86+
if(this->finished_game)
87+
return new StartScreen(this->render);
88+
89+
case SDLK_r:
90+
if(this->finished_game)
91+
this->reset();
92+
93+
default:
94+
break;
95+
}
96+
return nullptr;
97+
}
98+
99+
void QuboScreen::close_event(){
100+
}
101+
102+
void QuboScreen::reset(){
103+
this->finished_game = false;
104+
delete this->player;
105+
this->player = new QuboPlayer(this->board_w, this->board_h);
106+
this->board.add_player(this->player);
107+
this->board.random_food();
108+
}
109+
110+
QuboScreen::~QuboScreen(){
111+
delete this->player;
112+
SDL_DestroyTexture(this->score_texture);
113+
SDL_DestroyTexture(this->score_text_texture);
114+
}
115+
}

game/screens/qubo_screen.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#pragma once
2+
3+
#include <cstdint>
4+
#include <SDL2/SDL_render.h>
5+
#include <SDL2/SDL_ttf.h>
6+
#include "screens.h"
7+
#include "../board.h"
8+
#include "../players/qubo_player.h"
9+
10+
using Screens::Screen;
11+
using Game::Board;
12+
using Players::QuboPlayer;
13+
14+
namespace Screens{
15+
class QuboScreen: public Screen{
16+
public:
17+
QuboScreen(SDL_Renderer* render);
18+
~QuboScreen();
19+
void execute(bool& game_loop);
20+
Screen* key_event(const SDL_Keycode& key);
21+
void close_event();
22+
23+
private:
24+
uint8_t board_w = 45;
25+
uint8_t board_h = 30;
26+
Board board{board_w, board_h};
27+
28+
bool finished_game = false;
29+
30+
uint16_t max_score = 1000;
31+
QuboPlayer* player = new QuboPlayer(board_w, board_h);
32+
33+
TTF_Font* font = TTF_OpenFont("./assets/pressstart.ttf", 20);
34+
TTF_Font* title_font = TTF_OpenFont("./assets/pressstart.ttf", 40);
35+
36+
SDL_Rect score_text_shape;
37+
SDL_Rect score_shape;
38+
SDL_Texture* score_text_texture = nullptr;
39+
SDL_Texture* score_texture = nullptr;
40+
41+
void reset();
42+
};
43+
};

game/screens/start_screen.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "ai_screen_play.h"
1313
#include "player_screen.h"
1414
#include "ai_screen.h"
15+
#include "qubo_screen.h"
1516

1617
using std::exit;
1718
using std::cout;
@@ -58,7 +59,7 @@ namespace Screens {
5859
case SDLK_d:
5960
return new PlayerScreen(this->render);
6061
case SDLK_q:
61-
return nullptr;
62+
return new QuboScreen(this->render);
6263
default:
6364
break;
6465
}

qubo/qubo.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ namespace Qubo{
1010
}
1111

1212
double QuboFunc::evaluate(double q0, double q1, double q2, uint8_t x0, uint8_t x1, uint8_t x2){
13-
return (q0*x0 + q1*x1 + q2*x2) - P*(pow(x0 + x1 + x2 - 1, 2));
13+
return (q0*x0 + q1*x1 + q2*x2) + P*(pow(x0 + x1 + x2 - 1, 2));
1414
}
1515

1616
uint8_t* QuboFunc::minimize(double q0, double q1, double q2){
17-
//REMEMBER TO CLEAN IT AFTER USING!
17+
//REMEMBER TO CLEAN AFTER USING IT!
1818
uint8_t* result = new uint8_t[3];
1919
double best = 1000000000000;
2020

@@ -25,7 +25,7 @@ namespace Qubo{
2525

2626
double val = this->evaluate(q0, q1, q2, x0, x1, x2);
2727

28-
if(val < best){
28+
if(val <= best){
2929
best = val;
3030
result[0] = x0;
3131
result[1] = x1;

0 commit comments

Comments
 (0)