-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKnight.cpp
More file actions
87 lines (69 loc) · 1.97 KB
/
Knight.cpp
File metadata and controls
87 lines (69 loc) · 1.97 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
// Bryan Liu (chl312), Dept. of Computing, Imperial College London
// Knight.cpp - implementation of Knight extending Piece (info in Knight.hpp)
#include "pch.h"
#include "Knight.hpp"
Knight::Knight(bool isWhitePlayer) : Piece(isWhitePlayer)
{
}
Knight::~Knight()
{
}
Knight* Knight::clone()
{
return new Knight(this->isWhitePlayer());
}
/* A Knight's move is valid if:
- It moves in "L"-pattern
- The (possibly) existing piece on destination square is not a friendly
(or destination is empty)
Knight.isValidMove() post-cond: retrun 0 if move is valid as above
respective error code otherwise
*/
int Knight::isValidMove(wstring sourceFileRank, wstring destFileRank, map<wstring, Piece*>* board)
{
if (isSameFile(sourceFileRank, destFileRank) &&
isSameRank(sourceFileRank, destFileRank))
{
return ChessErrHandler::DEST_EQ_SOURCE;
}
if (!movesInLShape(sourceFileRank, destFileRank))
{
return ChessErrHandler::ILLEGAL_MOVE_PATTERN;
}
if (destExistFriendlyPiece(destFileRank, board))
{
return ChessErrHandler::FRIENDLY_AT_DEST;
}
return ChessErrHandler::CHESS_NO_ERROR;
}
wstring Knight::toString()
{
wstring name(playerToString());
name.append(_T(" Knight"));
return name;
}
wstring Knight::toGraphics()
{
if (_isWhitePlayer) {
// return wstring("♘");
return wstring(_T("\x2658"));
}
// return wstring("♞");
return wstring(_T("\x265E"));
}
/* Knight.movesInLShape()
pre-cond: args are valid file & rank representations of a different square
post-cond: return true if abs file diff = 2(1) & abs rank diff = 1(2)
*/
bool Knight::movesInLShape(wstring sourceFileRank, wstring destFileRank)
{
TCHAR sourceFile = sourceFileRank.at(ChessInfo::FILE_INDEX);
TCHAR sourceRank = sourceFileRank.at(ChessInfo::RANK_INDEX);
TCHAR destFile = destFileRank.at(ChessInfo::FILE_INDEX);
TCHAR destRank = destFileRank.at(ChessInfo::RANK_INDEX);
return abs(sourceFile - destFile) * abs(sourceRank - destRank) == 2;
}
int Knight::Score()
{
return 10;
}