-
Notifications
You must be signed in to change notification settings - Fork 0
/
piece.cpp
98 lines (87 loc) · 2.61 KB
/
piece.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
This code file contains member functions of piece.h
*/
#include "piece.h"
void Piece::setPiece(char type, bool player, int pos, bool moved){
setType(type);
setPlayer(player);
setPosition(pos); //m_moved true
setMoved(moved); // m_moved false
}
std::string Piece::toString(){
std::string str;
str += m_player?"White ":"Black ";
switch (m_type)
{
case 'K':
str += "King ";
break;
case 'Q':
str += "Queen ";
break;
case 'R':
str += "Rook ";
break;
case 'B':
str += "Bishop ";
break;
case 'N':
str += "Knight ";
break;
case 'P':
str += "Pawn ";
break;
default:
str += "??? ";
break;
}
str += "\nto position\nX: ";
str += std::to_string((m_position%8)+1);
str += " Y: ";
str += std::to_string((m_position/8)+1);
str += '\n';
return str;
}
void Piece::move(){
if(m_position<=-1 || 64<=m_position){
m_position = -1;
m_sprite.setColor(sf::Color(0x00000000));
m_sprite.setPosition(sf::Vector2f((m_position % 8) * 64.f + 32.f, (m_position / 8) * 64.f + 32.f));
possibleMoves.clear();
m_moved = true;
}
else{
m_sprite.setPosition(sf::Vector2f((m_position % 8) * 64.f + 32.f, (m_position / 8) * 64.f + 32.f));
m_moved = true;
}
return;
}
void Piece::setTexture(){
m_sprite = sf::Sprite();
switch (m_type)
{
case 'K':
m_sprite.setTexture(m_player ? PieceTextures::whiteKing : PieceTextures::blackKing);
break;
case 'Q':
m_sprite.setTexture(m_player ? PieceTextures::whiteQueen : PieceTextures::blackQueen);
break;
case 'R':
m_sprite.setTexture(m_player ? PieceTextures::whiteRook : PieceTextures::blackRook);
break;
case 'B':
m_sprite.setTexture(m_player ? PieceTextures::whiteBishop : PieceTextures::blackBishop);
break;
case 'N':
m_sprite.setTexture(m_player ? PieceTextures::whiteKnight : PieceTextures::blackKnight);
break;
case 'P':
m_sprite.setTexture(m_player ? PieceTextures::whitePawn : PieceTextures::blackPawn);
break;
default:
std::cerr << "Error piece type does not exist.\n";
break;
}
m_sprite.setOrigin(sf::Vector2f(m_sprite.getTexture()->getSize().x/2 , m_sprite.getTexture()->getSize().y/2));
m_sprite.setScale(sf::Vector2f(0.375f,0.375f));
}