-
Notifications
You must be signed in to change notification settings - Fork 0
/
knight.cc
31 lines (27 loc) · 1.33 KB
/
knight.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
#include <iostream>
#include <vector>
#include "piece.h"
#include "knight.h"
#include "grid.h"
Knight::Knight(char type): Piece(type, 2){}
void Knight::attachAttackRange(Grid *g) {
const int xCoord = getX();
const int yCoord = getY();
int x = xCoord; // mutable instance of x-coord
int y = yCoord; // mutable instance of y-coord
if (x+2 >= 0 and x+2 < 8 and y+1 >= 0 and y+1 < 8) attach(g->getCellAddress(x+2,y+1));
if (x+2 >= 0 and x+2 < 8 and y-1 >= 0 and y-1 < 8) attach(g->getCellAddress(x+2,y-1));
if (x-2 >= 0 and x-2 < 8 and y+1 >= 0 and y+1 < 8) attach(g->getCellAddress(x-2,y+1));
if (x-2 >= 0 and x-2 < 8 and y-1 >= 0 and y-1 < 8) attach(g->getCellAddress(x-2,y-1));
if (x-1 >= 0 and x-1 < 8 and y+2 >= 0 and y+2 < 8) attach(g->getCellAddress(x-1,y+2));
if (x-1 >= 0 and x-1 < 8 and y-2 >= 0 and y-2 < 8) attach(g->getCellAddress(x-1,y-2));
if (x+1 >= 0 and x+1 < 8 and y+2 >= 0 and y+2 < 8) attach(g->getCellAddress(x+1,y+2));
if (x+1 >= 0 and x+1 < 8 and y-2 >= 0 and y-2 < 8) attach(g->getCellAddress(x+1,y-2));
}
void Knight::attachLegalMoves(Grid *g) {
int size = attackRange.size();
for ( int i = 0; i < size; ++i) {
int oState = attackRange[i]->getOccupiedState();
if (oState == 0 or (oState-1 == getColour())) legalMoves.push_back(attackRange[i]);
}
}