-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPawn.cpp
More file actions
111 lines (97 loc) · 2.58 KB
/
Pawn.cpp
File metadata and controls
111 lines (97 loc) · 2.58 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
//
// Pawn.cpp
// Chess
//
// Created by mustafa tok on 12.12.2011.
//
#include <iostream>
#include "Component.h"
void Pawn::draw(){
if(this->isBlack())
cout << "p";
else
cout << "P";
}
int Pawn::canmove(Point &p1, Point &p2){
int dx;
int dy;
if(this->isWhite()){
dx = p1.getX() - p2.getX();
dy = p2.getY() - p1.getY();
}else{
dx = p2.getX() - p1.getX();
dy = p1.getY() - p2.getY();
}
if(p2.getX() < 0 || p2.getY() < 0 || p2.getX() >= SIZE || p2.getY() >= SIZE){
return CANTMOVE;
}
if(dx == 1){
}else if(dx == 2){
if(this->isTouched())
return CANTMOVE;
if(this->isBlack())
if(parent->getComponent(p2.getX()-1, p2.getY())!=NULL){
return CANTMOVE;
}
else if(this->isWhite())
if(parent->getComponent(p2.getX()+1, p2.getY())!=NULL){
return CANTMOVE;
}
}else{
return CANTMOVE;
}
Component* source = this;
Component* target;
target = parent->getComponent(p2.getX(), p2.getY());
if(dy == 0){
if(target != NULL){
return CANTMOVE;
}else{
return EMPTYMOVE;
}
}else if((dy == 1 || dy == -1) && dx == 1){
if(target != NULL){
if(target->isSameColor(source)){
return CANTMOVE;
}else{
return ENEMYMOVE;
}
}else{
return CANTMOVE;
}
}
return CANTMOVE;
}
BoardQueue* Pawn::successor(Point &p){
BoardQueue *q = new BoardQueue();
Point* np[4];
if(this->isWhite()){
np[0] = new Point(p.getX()-2,p.getY());
np[1] = new Point(p.getX()-1,p.getY());
np[2] = new Point(p.getX()-1,p.getY()-1);
np[3] = new Point(p.getX()-1,p.getY()+1);
}else if(this->isBlack()){
np[0] = new Point(p.getX()+2,p.getY());
np[1] = new Point(p.getX()+1,p.getY());
np[2] = new Point(p.getX()+1,p.getY()-1);
np[3] = new Point(p.getX()+1,p.getY()+1);
}else{
return NULL;
}
for(int i = 0; i < 4; i++){
if(this->canmove(p, *(np[i]))!=CANTMOVE){
Board *b = parent->clone();
b->moveComponent(p, *(np[i]));
q->enqueue(b);
}
delete np[i];//checkkkk
}
return q;
}
Component* Pawn::clone(Board* parent){
Pawn *newPawn = new Pawn(parent, color);
if(this->isTouched()){
newPawn->setTouched();
}
return newPawn;
}