-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKnight.cpp
More file actions
53 lines (45 loc) · 1.24 KB
/
Copy pathKnight.cpp
File metadata and controls
53 lines (45 loc) · 1.24 KB
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
#include "Knight.h"
Knight::Knight(int color, int type, int x, int y, sf::Texture& texture):Piece(color, type, x, y){
sprite.setTexture(texture);
if (color == 1) {
sprite.setTextureRect(sf::IntRect(1016, 20, 300, 300));
} else {
sprite.setTextureRect(sf::IntRect(1016, 351, 300, 300));
}
sprite.scale(0.38, 0.38);
}
void Knight::move(int x, int y){
if(isLegal(x,y)){
this->updatePosition(x,y);
}
else{
cout<<"illegal move: "<< x <<", "<< y <<"Is not a valid move"<<endl;
}
}
bool Knight::isLegal(int xf,int yf){
int xi = get<0>(this->currPosition);
int yi = get<1>(this->currPosition);
int xDiff = abs( xf - xi );
int yDiff = abs( yf - yi);
//knights must move in the x or y direction 2 spaces
//and then in the x or y direction 1 space
//Behavior of knight not taking into account other pieces and such
if(xDiff == 2){
if(yDiff == 1){
return true;
}
else{
return false;
}
}else if (yDiff == 2){
if(xDiff == 1){
return true;
}
else{
return false;
}
}else{
cout<<"Knights don't move like that"<<endl;
return false;
}
}