-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKnight.cpp
More file actions
150 lines (135 loc) · 3.76 KB
/
Knight.cpp
File metadata and controls
150 lines (135 loc) · 3.76 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
//
// Knight.cpp
// Chess
//
// Created by mustafa tok on 12.12.2011.
//
#include <iostream>
#include "Component.h"
void Knight::draw(){
if(this->isBlack())
cout << "n";
else
cout << "N";
}
int Knight::canmove(Point &p1, Point &p2){
int dx = p2.getX() - p1.getX();
int dy = p2.getY() - p1.getY();
if(dx == -1 && dy == -2){}
else if(dx == -1 && dy == 2){}
else if(dx == 1 && dy == -2){}
else if(dx == 1 && dy == 2){}
else if(dx == -2 && dy == -1){}
else if(dx == -2 && dy == 1){}
else if(dx == 2 && dy == -1){}
else if(dx == 2 && dy == 1){}
else{
return CANTMOVE;
}
Component* source = this;
Component* target;
target = parent->getComponent(p2.getX(), p2.getY());
if(target != NULL){
if(target->isSameColor(source)){
return CANTMOVE;
}else{
return ENEMYMOVE;
}
}else{
return EMPTYMOVE;
}
return CANTMOVE;
}
BoardQueue* Knight::successor(Point &p){
BoardQueue *q = new BoardQueue();
int x = p.getX();
int y = p.getY();
Component* com;
if(x - 2 >= 0){
if(y - 1 >= 0){
com = parent->getComponent(x-2, y-1);
if(!this->isSameColor(com)){
Point tp(x-2, y-1);
Board *b = parent->clone();
b->moveComponent(p, tp);
q->enqueue(b);
}
}
if(y+1 < SIZE){
com = parent->getComponent(x-2, y+1);
if(!this->isSameColor(com)){
Point tp(x-2, y+1);
Board *b = parent->clone();
b->moveComponent(p, tp);
q->enqueue(b);
}
}
}
if(x - 1 >= 0){
if(y - 2 >= 0){
com = parent->getComponent(x-1, y-2);
if(!this->isSameColor(com)){
Point tp(x-1, y-2);
Board *b = parent->clone();
b->moveComponent(p, tp);
q->enqueue(b);
}
}
if(y+2 < SIZE){
com = parent->getComponent(x-1, y+2);
if(!this->isSameColor(com)){
Point tp(x-1, y+2);
Board *b = parent->clone();
b->moveComponent(p, tp);
q->enqueue(b);
}
}
}
if(x + 1 < SIZE){
if(y - 2 >= 0){
com = parent->getComponent(x+1, y-2);
if(!this->isSameColor(com)){
Point tp(x+1, y-2);
Board *b = parent->clone();
b->moveComponent(p, tp);
q->enqueue(b);
}
}
if(y+2 < SIZE){
com = parent->getComponent(x+1, y+2);
if(!this->isSameColor(com)){
Point tp(x+1, y+2);
Board *b = parent->clone();
b->moveComponent(p, tp);
q->enqueue(b);
}
}
}
if(x + 2 < SIZE){
if(y - 1 >= 0){
com = parent->getComponent(x+2, y-1);
if(!this->isSameColor(com)){
Point tp(x+2, y-1);
Board *b = parent->clone();
b->moveComponent(p, tp);
q->enqueue(b);
}}
if(y+1 < SIZE){
com = parent->getComponent(x+2, y+1);
if(!this->isSameColor(com)){
Point tp(x+2, y+1);
Board *b = parent->clone();
b->moveComponent(p, tp);
q->enqueue(b);
}
}
}
return q;
}
Component* Knight::clone(Board* parent){
Knight *newClone = new Knight(parent, color);
if(this->isTouched()){
newClone->setTouched();
}
return newClone;
}