This repository was archived by the owner on Apr 29, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChessPiece.cpp
More file actions
115 lines (83 loc) · 2.35 KB
/
ChessPiece.cpp
File metadata and controls
115 lines (83 loc) · 2.35 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
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include "ChessPiece.h"
#include <iostream>
using namespace std;
//Provide method to give legal moves
ChessPiece::ChessPiece(char type, int side, int posX, int posY) {
this->type = type;
this->side = side;
this->onBoard = true;
this->Selected = false;
this->hasMoved = false;
this->posX = posX;
this->posY = posY;
this->dangerWhite = false;
this->dangerBlack = false;
this->kingSpecial = false;
}
ChessPiece::ChessPiece() {
this->type = 'n';
this->side = -1;
this->onBoard = false;
this->Selected = false;
this->dangerWhite = false;
this->dangerBlack = false;
this->kingSpecial = false;
}
char ChessPiece::getType() {
return this->type;
}
int ChessPiece::getSide() {
return this->side;
}
void ChessPiece::setCastling(bool kingSpecial) {
this->kingSpecial = kingSpecial;
}
void ChessPiece::setType(char type) {
this->type = type;
}
void ChessPiece::move(ChessPiece board[8][8], int posX, int posY, float boardX[8], float boardY[8], int &turn) {
if (posX != this->posX || posY != this->posY) {
int ix, iy, fx, fy;
ix = this->posX;
iy = this->posY;
ChessPiece temp = board[posX][posY];
ChessPiece temp2 = *this;
//Pawn Promotion.
if (this->getType() == 'p') {
if (this->getSide() == 0 && posY == 0) {
this->setType('Q');
}else if(this->getSide() == 1 && posY == 7){
this->setType('Q');
}
}
this->hasMoved = true;
board[posX][posY] = *this;
board[posX][posY].posX = posX;
board[posX][posY].posY = posY;
board[ix][iy] = ChessPiece();
fx = posX;
fy = posY;
//Castling
if (this->kingSpecial) {
board[posX][posY].kingSpecial = false;
if (board[posX + 1][posY].getType() == 'r') {
temp = board[posX + 1][posY]; //rook
temp2 = board[posX - 1][posY];
temp.hasMoved = true;
board[posX - 1][posY] = temp;
board[posX - 1][posY].posX = posX - 1;
board[posX + 1][posY] = ChessPiece();
}else if (board[posX - 2][posY].getType() == 'r') {
temp = board[posX - 2][posY]; //rook
temp2 = board[posX + 1][posY];
temp.hasMoved = true;
board[posX + 1][posY] = temp;
board[posX + 1][posY].posX = posX + 1;
board[posX -2][posY] = ChessPiece();
}
}
board[ix][iy].Selected = true;
board[fx][fy].Selected = true;
//turn = !turn;
}
}