-
Notifications
You must be signed in to change notification settings - Fork 0
/
King.cpp
66 lines (53 loc) · 1.84 KB
/
King.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include<SFML/Graphics.hpp>
#include<algorithm>
#include<vector>
#include<ranges>
#include "King.h"
#include "ChessBoardPos.h"
King::King(const std::string& filename,const ChessBoardPos pos, const int AssocNum)
{
if (!piece_texture.loadFromFile(filename))
{
// Handle the error
}
piece_sprite.setTexture(piece_texture);
sf::Vector2u position_pixel = pos.ToPixelPosition();
piece_sprite.setPosition(position_pixel.x, position_pixel.y);
position.x = pos.x;
position.y = pos.y;
AssociatedNum = AssocNum;
}
void King::draw(sf::RenderWindow& window)
{
if (IsAlive)
{
window.draw(piece_sprite);
}
}
auto King::GetAllPositionsAllowedToMoveTo() const noexcept -> std::vector<ChessBoardPos>
{
std::vector<ChessBoardPos> positions{};
positions.push_back(ChessBoardPos{ position.x + 1, position.y });
positions.push_back(ChessBoardPos{ position.x - 1, position.y });
positions.push_back(ChessBoardPos{ position.x, position.y + 1 });
positions.push_back(ChessBoardPos{ position.x, position.y - 1 });
return positions;
}
auto King::MoveTo(const ChessBoardPos& position_) noexcept -> bool
{
std::vector<ChessBoardPos> AllPositions = GetAllPositionsAllowedToMoveTo();
if (std::ranges::find_if(AllPositions, [&](const ChessBoardPos& pos) {return ((pos.x == position_.x) && (pos.y == position_.y)); }) != AllPositions.end())
{
position.x = position_.x;
position.y = position_.y;
const sf::Vector2u position_piece = position_.ToPixelPosition();
piece_sprite.setPosition(position_piece.x, position_piece.y);
return true;
}
return false;
}
auto King::CanMoveToPosition(const ChessBoardPos& position_) const noexcept -> bool
{
if ((position_.x == position.x) || (position_.y == position.y)) { return true; }
else { return false; }
}