-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMoveValidator.cpp
More file actions
170 lines (139 loc) · 5.32 KB
/
Copy pathMoveValidator.cpp
File metadata and controls
170 lines (139 loc) · 5.32 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
//
// Created by sahoo on 23-06-2026.
//
#include "MoveValidator.h"
#include <iostream>
#include "CastlingValidator.h"
#include "PieceOwnership.h"
#include "MovementPatterns.h"
#include "PawnValidator.h"
#include "kingValidator.h"
#include "PathValidator.h"
// returns true if the input position is valid
bool MoveValidator::isValidPosition(const std::string &position) {
// returns false if input length is not 2
if (position.length() != 2) {
std::cout << "Invalid position length!!" << std::endl;
return false;
}
// returns false if first character is something other than a...h
if (position[0] < 'a' || position[0] > 'h') {
std::cout << "Invalid position format!!"<< std::endl;
return false;
}
// returns false if first character is something other than 1...8
if (position[1] < '1' || position[1] > '8') {
std::cout << "Invalid position format!!"<< std::endl;
return false;
}
return true;
}
// returns true if the move is a valid knight move
bool MoveValidator::isKnightMove(int startPos, int endPos) {
int startRank = startPos >> 3;
int startFile = startPos & 7;
int endRank = endPos >> 3;
int endFile = endPos & 7;
int rankDiff = abs(startRank - endRank);
int fileDiff = abs(startFile - endFile);
return (rankDiff == 1 && fileDiff == 2) || (rankDiff == 2 && fileDiff == 1);
}
// master move validator function: returns true if the move is legal for the selected piece
bool MoveValidator::isValidMove(ChessBoard &board, int startPos, int endPos) {
// enters same position twice
if (startPos == endPos) {
std::cout << "Same position!!" << std::endl;
return false;
}
Piece movingPiece(board.getPieceAt(startPos));
Piece capturedPiece(board.getPieceAt(endPos));
// cannot capture the opponent's king
if (capturedPiece == whiteKing || capturedPiece == blackKing) {
std::cout << "Can,t capture king!!" << std::endl;
return false;
}
// cannot capture your own piece
if (PieceOwnership::isOwnPiece(board, endPos)) {
std::cout << "Can't capture own piece!!" << std::endl;
return false;
}
// checks move validity for pawns
if (movingPiece == whitePawn || movingPiece == blackPawn) {
if (!PawnValidator::isPawnMove(board, startPos, endPos)) {
std::cout << "Not a valid pawn move!!" << std::endl;
return false;
}
}
// checks move validity for rooks
if (movingPiece == whiteRook || movingPiece == blackRook) {
if (!MovementPatterns::isHorizontalMove(startPos, endPos) &&
!MovementPatterns::isVerticalMove(startPos, endPos)) {
std::cout << "Not a valid rook move!!" << std::endl;
return false;
}
if (PathValidator::isPathBlocked(board, startPos, endPos)) {
std::cout << "Path is blocked!!" << std::endl;
return false;
}
}
// checks move validity for bishops
if (movingPiece == whiteBishop || movingPiece == blackBishop) {
if (!MovementPatterns::isDiagonalMove(startPos, endPos)) {
std::cout << "Not a valid bishop move!!" << std::endl;
return false;
}
if (PathValidator::isPathBlocked(board, startPos, endPos)) {
std::cout << "Path is blocked!!" << std::endl;
return false;
}
}
// checks move validity for queens
if (movingPiece == whiteQueen || movingPiece == blackQueen) {
if (!MovementPatterns::isDiagonalMove(startPos, endPos) &&
!MovementPatterns::isHorizontalMove(startPos, endPos) &&
!MovementPatterns::isVerticalMove(startPos, endPos)) {
std::cout << "Not a valid queen move!!" << std::endl;
return false;
}
if (PathValidator::isPathBlocked(board, startPos, endPos)) {
std::cout << "Path is blocked!!" << std::endl;
return false;
}
}
// checks move validity for knights
if (movingPiece == whiteKnight || movingPiece == blackKnight) {
if (!isKnightMove(startPos, endPos)) {
std::cout << "Not a valid knight move!!" << std::endl;
return false;
}
}
// checks move validity for kings
if (movingPiece == whiteKing || movingPiece == blackKing) {
int startFile = startPos & 7;
int endFile = endPos & 7;
if (std::abs(startFile - endFile) == 2) {
bool isWhitePlayer = (movingPiece == whiteKing);
if (endFile > startFile) { // kingside
if (CastlingValidator::canCastleKingside(board,isWhitePlayer)) {
return true;
}
} else { // queenside
if (CastlingValidator::canCastleQueenside(board,isWhitePlayer)) {
return true;
}
}
std::cout << "Illegal castling attempt!!" << std::endl;
return false;
}
if (!KingValidator::isKingMove(startPos, endPos)) {
std::cout << "Not a valid king move!!" << std::endl;
return false;
}
}
// checks if move leaves king in check
if (KingValidator::leavesKingInCheck(board, startPos, endPos)) {
std::cout << "Leaves king in check!! Try another move: ";
return false;
}
return true;
}