|
| 1 | +#include <cstdint> |
| 2 | +#include "qubo_player.h" |
| 3 | +#include "../../qubo/qubo.h" |
| 4 | +#include "../../helpers/utils.h" |
| 5 | + |
| 6 | +using Qubo::QuboFunc; |
| 7 | +using Utils::vec2; |
| 8 | +using Utils::distance; |
| 9 | + |
| 10 | +namespace Players { |
| 11 | + QuboPlayer::QuboPlayer(uint8_t board_w, uint8_t board_h) : Player(board_w, board_h){ |
| 12 | + this->board_w = board_w; |
| 13 | + this->board_h = board_h; |
| 14 | + this->qubo = new QuboFunc((board_h*board_h) + 10); |
| 15 | + } |
| 16 | + |
| 17 | + bool QuboPlayer::will_collide_itself(int16_t x, int16_t y){ |
| 18 | + bool will_collide = false; |
| 19 | + Node* actual_bpart = this->head->next; |
| 20 | + while(actual_bpart != nullptr){ |
| 21 | + |
| 22 | + if(actual_bpart->value.x == x && |
| 23 | + actual_bpart->value.y == y){ |
| 24 | + will_collide = true; |
| 25 | + break; |
| 26 | + } |
| 27 | + actual_bpart = actual_bpart->next; |
| 28 | + } |
| 29 | + return will_collide; |
| 30 | + } |
| 31 | + |
| 32 | + bool QuboPlayer::will_collide_top_border(){ |
| 33 | + return this->get_x() <= 0; |
| 34 | + } |
| 35 | + |
| 36 | + bool QuboPlayer::will_collide_bottom_border(){ |
| 37 | + return this->get_x() >= this->board_h-1; |
| 38 | + } |
| 39 | + |
| 40 | + bool QuboPlayer::will_collide_left_border(){ |
| 41 | + return this->get_y() <= 0; |
| 42 | + } |
| 43 | + |
| 44 | + bool QuboPlayer::will_collide_right_border(){ |
| 45 | + return this->get_y() >= this->board_w-1; |
| 46 | + } |
| 47 | + |
| 48 | + void QuboPlayer::next_mov(const vec2& food){ |
| 49 | + double q0 = 10000.0; //distance forward |
| 50 | + double q1 = 10000.0; //distance down/right |
| 51 | + double q2 = 10000.0; //distance up/left |
| 52 | + |
| 53 | + switch (this->dir) { |
| 54 | + case RIGHT: |
| 55 | + this->get_distances_right(food.x, food.y, &q0, &q1, &q2); |
| 56 | + break; |
| 57 | + case LEFT: |
| 58 | + this->get_distances_left(food.x, food.y, &q0, &q1, &q2); |
| 59 | + break; |
| 60 | + case UP: |
| 61 | + this->get_distances_up(food.x, food.y, &q0, &q1, &q2); |
| 62 | + break; |
| 63 | + case DOWN: |
| 64 | + this->get_distances_up(food.x, food.y, &q0, &q1, &q2); |
| 65 | + break; |
| 66 | + default: |
| 67 | + break; |
| 68 | + } |
| 69 | + |
| 70 | + |
| 71 | + uint8_t* next_mov = this->qubo->minimize(q0, q1, q2); |
| 72 | + |
| 73 | + |
| 74 | + if(next_mov[0] == 0 && next_mov[1] == 0 && next_mov[2] == 1) |
| 75 | + this->move_up_left(); |
| 76 | + else if(next_mov[0] == 0 && next_mov[1] == 1 && next_mov[2] == 0) |
| 77 | + this->move_down_right(); |
| 78 | + |
| 79 | + delete next_mov; |
| 80 | + } |
| 81 | + |
| 82 | + void QuboPlayer::get_distances_right(int16_t fx, int16_t fy, double* q0, double* q1, double* q2){ |
| 83 | + if(!this->will_collide_itself(this->get_x(), this->get_y()+1) && !this->will_collide_right_border()) |
| 84 | + *q0 = distance(this->get_x(), this->get_y()+1, fx, fy); |
| 85 | + |
| 86 | + if(!this->will_collide_itself(this->get_x()+1, this->get_y()) && !this->will_collide_bottom_border()) |
| 87 | + *q1 = distance(this->get_x()+1, this->get_y(), fx, fy); |
| 88 | + |
| 89 | + if(!this->will_collide_itself(this->get_x()-1, this->get_y()) && !this->will_collide_top_border()) |
| 90 | + *q2 = distance(this->get_x()-1, this->get_y(), fx, fy); |
| 91 | + } |
| 92 | + |
| 93 | + |
| 94 | + void QuboPlayer::get_distances_left(int16_t fx, int16_t fy, double* q0, double* q1, double* q2){ |
| 95 | + if(!this->will_collide_itself(this->get_x(), this->get_y()-1) && !this->will_collide_left_border()) |
| 96 | + *q0 = distance(this->get_x(), this->get_y()-1, fx, fy); |
| 97 | + |
| 98 | + if(!this->will_collide_itself(this->get_x()+1, this->get_y()) && !this->will_collide_bottom_border()) |
| 99 | + *q1 = distance(this->get_x()+1, this->get_y(), fx, fy); |
| 100 | + |
| 101 | + if(!this->will_collide_itself(this->get_x()-1, this->get_y()) && !this->will_collide_top_border()) |
| 102 | + *q2 = distance(this->get_x()-1, this->get_y(), fx, fy); |
| 103 | + } |
| 104 | + |
| 105 | + void QuboPlayer::get_distances_up(int16_t fx, int16_t fy, double* q0, double* q1, double* q2){ |
| 106 | + if(!this->will_collide_itself(this->get_x()-1, this->get_y()) && !this->will_collide_top_border()) |
| 107 | + *q0 = distance(this->get_x()-1, this->get_y(), fx, fy); |
| 108 | + |
| 109 | + if(!this->will_collide_itself(this->get_x(), this->get_y()+1) && !this->will_collide_right_border()) |
| 110 | + *q1 = distance(this->get_x(), this->get_y()+1, fx, fy); |
| 111 | + |
| 112 | + if(!this->will_collide_itself(this->get_x(), this->get_y()-1) && !this->will_collide_left_border()) |
| 113 | + *q2 = distance(this->get_x(), this->get_y()-1, fx, fy); |
| 114 | + } |
| 115 | + |
| 116 | + void QuboPlayer::get_distances_down(int16_t fx, int16_t fy, double* q0, double* q1, double* q2){ |
| 117 | + if(!this->will_collide_itself(this->get_x()+1, this->get_y()) && !this->will_collide_bottom_border()) |
| 118 | + *q0 = distance(this->get_x()+1, this->get_y(), fx, fy); |
| 119 | + |
| 120 | + if(!this->will_collide_itself(this->get_x(), this->get_y()+1) && !this->will_collide_right_border()) |
| 121 | + *q1 = distance(this->get_x(), this->get_y()+1, fx, fy); |
| 122 | + |
| 123 | + if(!this->will_collide_itself(this->get_x(), this->get_y()-1) && !this->will_collide_left_border()) |
| 124 | + *q2 = distance(this->get_x(), this->get_y()-1, fx, fy); |
| 125 | + } |
| 126 | + |
| 127 | + |
| 128 | + void QuboPlayer::move_down_right(){ |
| 129 | + switch (this->dir) { |
| 130 | + case RIGHT: |
| 131 | + this->direction_down(); |
| 132 | + break; |
| 133 | + case LEFT: |
| 134 | + this->direction_down(); |
| 135 | + break; |
| 136 | + case UP: |
| 137 | + this->direction_right(); |
| 138 | + break; |
| 139 | + case DOWN: |
| 140 | + this->direction_right(); |
| 141 | + break; |
| 142 | + default: |
| 143 | + break; |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + void QuboPlayer::move_up_left(){ |
| 148 | + switch (this->dir) { |
| 149 | + case RIGHT: |
| 150 | + this->direction_up(); |
| 151 | + break; |
| 152 | + case LEFT: |
| 153 | + this->direction_up(); |
| 154 | + break; |
| 155 | + case UP: |
| 156 | + this->direction_left(); |
| 157 | + break; |
| 158 | + case DOWN: |
| 159 | + this->direction_left(); |
| 160 | + break; |
| 161 | + default: |
| 162 | + break; |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + QuboPlayer::~QuboPlayer(){ |
| 167 | + delete this->qubo; |
| 168 | + } |
| 169 | + |
| 170 | +} |
0 commit comments