-
Notifications
You must be signed in to change notification settings - Fork 0
/
piece.cc
152 lines (132 loc) · 3.73 KB
/
piece.cc
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
#include <iostream>
#include <vector>
#include "piece.h"
#include "cell.h"
#include "grid.h"
Piece::Piece(char type, int priority): priority{priority}, type{type}, colour{type <= 90}{}
void Piece::setCord(int xCoord, int yCoord) {
x = xCoord;
y = yCoord;;
}
int Piece::getX() const { return x; }
int Piece::getY() const { return y; }
char Piece::getType() const { return type; }
//bool Piece::getState() const { return beingCaptured;}
bool Piece::getColour() const { return colour; }
int Piece::getPriority() const { return priority; }
int Piece::getStep() const { return step;}
void Piece::addStep() { ++step; }
void Piece::subStep() { --step; }
int Piece::getTakenOut() const { return takenOut; }
void Piece::setTakenOut(int n) { takenOut = n; }
void Piece::setForward(int n) { forwardTwoTurn = n; }
int Piece::getForward() const { return forwardTwoTurn; }
void Piece::attach(Cell *c) {
c->alertOn(getColour());
attackRange.push_back(c);
}
void Piece::legalMovesOfQRB(Grid *g, int dir) {
if (dir == 1) { // north
for (int i = y-1; i >= 0; --i) {
Cell *c = g->getCellAddress(x,i);
attach(c);
if (c->getOccupiedState() != 0) {
break;
}
}
} else if (dir == 2) { // north-east
int i = x+1, j = y-1;
while (i < 8 and j >= 0) {
Cell *c = g->getCellAddress(i,j);
attach(c);
if (c->getOccupiedState() != 0) {
break;
}
++i;
--j;
}
} else if (dir == 3) { // east
for (int i = x+1; i < 8; ++i) {
Cell *c = g->getCellAddress(i,y);
attach(c);
if (c->getOccupiedState() != 0) {
break;
}
}
} else if (dir == 4) { // south-east
int i = x+1, j = y+1;
while (i < 8 and j < 8) {
Cell *c = g->getCellAddress(i,j);
attach(c);
if (c->getOccupiedState() != 0) {
break;
}
++i;
++j;
}
} else if (dir == 5) { // south
for (int i = y+1; i < 8; ++i) {
Cell *c = g->getCellAddress(x,i);
attach(c);
if (c->getOccupiedState() != 0) {
break;
}
}
} else if (dir == 6) { // south-west
int i = x-1, j = y+1;
while (j < 8 and i >= 0) {
Cell *c = g->getCellAddress(i,j);
attach(c);
if (c->getOccupiedState() != 0) {
break;
}
--i;
++j;
}
} else if (dir == 7) { // west
for (int i = x-1; i >= 0; --i) {
Cell *c = g->getCellAddress(i,y);
attach(c);
if (c->getOccupiedState() != 0) {
break;
}
}
} else if (dir == 8) { // north-west
int i = x-1, j = y-1;
while (i >= 0 and j >= 0) {
Cell *c = g->getCellAddress(i,j);
attach(c);
if (c->getOccupiedState() != 0) {
break;
}
--i;
--j;
}
}
}
void Piece::detachMoves() {
for ( int i = attackRange.size()-1; i >= 0; --i) {
attackRange[i]->alertOff(getColour());
}
attackRange.clear();
legalMoves.clear();
}
void Piece::reAttach(Grid *g) {
detachMoves();
attachAttackRange(g);
attachLegalMoves(g);
}
bool Piece::moveInRange(int xCoord, int yCoord) {
int size = legalMoves.size();
for ( int i = 0; i < size; ++i ) {
if ( legalMoves[i]->getX() == xCoord and legalMoves[i]->getY() == yCoord ) return true;
}
return false;
}
bool Piece::cantMove(Grid *g) {
int size= legalMoves.size();
for ( int i = 0; i < size; ++i) {
if (!g->badMove(getX(), getY(), legalMoves[i]->getX(), legalMoves[i]->getY())) return false;
}
return true;
}