-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMove.cpp
More file actions
124 lines (102 loc) · 2.83 KB
/
Move.cpp
File metadata and controls
124 lines (102 loc) · 2.83 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
116
117
118
119
120
121
122
123
124
//
// Created by Lukas Bos on 11/12/2017.
//
#include "Move.h"
Move::Move() = default;
Move::Move(Board *board, Square *startOfMove, Square *endOfMove)
: board(board), startOfMove(startOfMove), endOfMove(endOfMove) {
identifyPieces();
generateName();
}
Move::Move(Board *board, Square *startOfMove, Square *endOfMove, bool simulated)
: board(board), startOfMove(startOfMove), endOfMove(endOfMove), simulated(simulated) {
identifyPieces();
if (!isSimulated()) generateName();
}
Move::Move(Board *board,
Square *startOfMove,
Square *endOfMove,
bool simulated,
CastlingSide castlingSide,
ChessPiece *castlingRook,
Square *rookTargetSquare,
Square *initalRookSquare)
: board(board),
startOfMove(startOfMove),
endOfMove(endOfMove),
simulated(simulated),
castlingSide(castlingSide),
castlingRook(castlingRook),
rookTargetSquare(rookTargetSquare),
initalRookSquare(initalRookSquare) {
identifyPieces();
if (!isSimulated()) generateName();
}
// Simulate move to look if this causes a check(mate).
void Move::generateName() {
if (castlingRook) {
name = castlingSide==CastlingSide::QUEENSIDE ? "0-0-0" : "0-0";
return;
}
char shortNames[] = "KQRBNP";
name = shortNames[initialPiece->getType()];
if (name=="P") name = ""; // pawn is usually not written down.
if (this->getTakenPiece()) name += 'x';
name += endOfMove->getName();
bool tempIsSimulated = simulated;
simulated = true;
board->doMove(this);
if (board->checkMate()) {
name += '#';
} else if (board->isInCheck(inverse(initialPiece->getColor()))) {
name += '+';
}
board->undoMove();
simulated = tempIsSimulated;
}
void Move::identifyPieces() {
initialPiece = getStartOfMove()->getChessPiece();
takenPiece = getEndOfMove()->getChessPiece();
}
Square *Move::getStartOfMove() const {
return startOfMove;
}
Square *Move::getEndOfMove() const {
return endOfMove;
}
ChessPiece *Move::getInitialPiece() const {
return initialPiece;
}
ChessPiece *Move::getTakenPiece() const {
return takenPiece;
}
bool Move::isSimulated() const {
return simulated;
}
void Move::setSimulated(bool simulated) {
Move::simulated = simulated;
}
const string &Move::getName() const {
return name;
}
ChessPiece *Move::getCastlingRook() const {
return castlingRook;
}
Square *Move::getRookTargetSquare() const {
return rookTargetSquare;
}
Square *Move::getInitalRookSquare() const {
return initalRookSquare;
}
bool Move::isPromoting() const {
return promoting;
}
void Move::setPromoting(bool promoting) {
Move::promoting = promoting;
}
ChessPiece *Move::getEnPassantTakenPiece() const {
return enPassantTakenPiece;
}
void Move::setEnPassantTakenPiece(ChessPiece *enPassantTakenPiece) {
Move::enPassantTakenPiece = enPassantTakenPiece;
}