-
Notifications
You must be signed in to change notification settings - Fork 0
/
CPawn.cpp
112 lines (74 loc) · 2.35 KB
/
CPawn.cpp
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
#include "CPawn.h"
#include "CQueen.h"
#include "CGameSession.h"
CPawn::CPawn() {
name = PAWN;
moveList.reserve(15);
value = 1;
}
CPawn::~CPawn() {
}
CPawn::CPawn(COLOR clr , int row, int col) : CPiece(clr,row,col) {
name = PAWN;
moveList.reserve(15);
value = 1;
}
CMoveList & CPawn::getLegalMovesDown(const CBoard & board) {
moveList.clear();
int newRow = getRow()-1;
int newCol = getCol();
int firststep = this->checkField(newRow, newCol, board, false);
this->checkField(getRow() - 1, getCol() - 1, board, true);
this->checkField(getRow() - 1, getCol() + 1, board, true);
if (getRow() == board.INIT_ROW_UP && firststep != 2) { //initial row, pawn not moved yet
newRow = getRow()-2;
this->checkField(newRow, newCol, board, false);
}
return moveList;
}
CMoveList & CPawn::getLegalMovesUp(const CBoard & board) {
moveList.clear();
int newRow = getRow()+1;
int newCol = getCol();
int firststep = this->checkField(newRow, newCol, board, false);
this->checkField(getRow() + 1, getCol() - 1, board, true);
this->checkField(getRow() + 1, getCol() + 1, board, true);
if (getRow() == board.INIT_ROW_DOWN && firststep != 2) { //initial row, pawn not moved yet
newRow = getRow()+2;
this->checkField(newRow, newCol, board, false);
}
return moveList;
}
CMoveList & CPawn::getLegalMoves(const CGameSession & gS) {
if(gS.currPlayerPtr == gS.player1)
return getLegalMovesUp(gS.getBoard());
else
return getLegalMovesDown(gS.getBoard());
}
int CPawn::checkField(int x, int y, const CBoard& board, bool sidestep) {
if (board.outOfBoard(x, y))
return 1;
if (x == getRow() && y == getCol())
return 0;
CPiece * tmp = board.getPiece(x, y);
if (!sidestep) {
if (tmp != NULL) { // nekdo stoji primo pred pawnem
return 2;
} else {
moveList.add(x, y,rowPos,colPos,NULL);
return 0;
}
}
// is sidestep
if (tmp == NULL)
return 1;
else if (isFriendPiece(tmp)) {
return 1;
} else {
moveList.add(x, y,rowPos,colPos,tmp);
return 0;
}
}
CPiece* CPawn::copyPiece(const CPiece * pcs) const{
return new CPawn(pcs->getColor(),pcs->getRow(),pcs->getCol());
}