-
Notifications
You must be signed in to change notification settings - Fork 0
/
MoveController.cpp
170 lines (146 loc) · 5.92 KB
/
MoveController.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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include "MoveController.h"
bool MoveController::isSimpleMovingUp(int x1,int y1,int x2,int y2){
if(checkboard.isNotInBoard(x1,y1) || checkboard.isNotInBoard(x2,y2))
return false;
Checker cell1 = checkboard.getChecker(x1,y1);
Checker cell2 = checkboard.getChecker(x2,y2);
if(cell1 == Checker::empty || cell2 != Checker::empty)
return false;
if(isMyChecker(whiteOnTurn,cell1) == false){
return false;
}
if(abs(x2-x1) == 1 && y2-y1 == -1)
return true;
return false;
}
bool MoveController::isSimpleMovingDown(int x1,int y1,int x2,int y2){
if(checkboard.isNotInBoard(x1,y1) || checkboard.isNotInBoard(x2,y2))
return false;
Checker cell1 = checkboard.getChecker(x1,y1);
Checker cell2 = checkboard.getChecker(x2,y2);
if(cell1 == Checker::empty || cell2 != Checker::empty)
return false;
if(isMyChecker(whiteOnTurn,cell1) == false)
return false;
if(abs(x2-x1) == 1 && y2-y1 == 1)
return true;
return false;
}
bool MoveController::isSimpleMoving(int x1,int y1,int x2,int y2){
if(MoveController::isSimpleMovingUp(x1,y1,x2,y2) || MoveController::isSimpleMovingDown(x1,y1,x2,y2))
return true;
return false;
}
bool MoveController::isSimpleJumpingUp(int x1,int y1,int x2,int y2){
if(checkboard.isNotInBoard(x1,y1) || checkboard.isNotInBoard(x2,y2))
return false;
Checker cell1 = checkboard.getChecker(x1,y1);
Checker cell2 = checkboard.getChecker(x2,y2);
if(cell1 == Checker::empty || cell2 != Checker::empty)
return false;
if(isMyChecker(whiteOnTurn,cell1) == false)
return false;
if(abs(x2-x1) == 2 && y2-y1 == -2 && isEnemyChecker(whiteOnTurn,checkboard.getChecker((x1+x2)/2,(y1+y2)/2)))
return true;
return false;
}
bool MoveController::isSimpleJumpingDown(int x1,int y1,int x2,int y2){
if(checkboard.isNotInBoard(x1,y1) || checkboard.isNotInBoard(x2,y2))
return false;
Checker cell1 = checkboard.getChecker(x1,y1);
Checker cell2 = checkboard.getChecker(x2,y2);
if(cell1 == Checker::empty || cell2 != Checker::empty)
return false;
if(isMyChecker(whiteOnTurn,cell1) == false)
return false;
if(abs(x2-x1) == 2 && y2-y1 == 2 && isEnemyChecker(whiteOnTurn,checkboard.getChecker((x1+x2)/2,(y1+y2)/2)))
return true;
return false;
}
bool MoveController::isSimpleJumping(int x1,int y1,int x2,int y2){
if(MoveController::isSimpleJumpingUp(x1,y1,x2,y2) || MoveController::isSimpleJumpingDown(x1,y1,x2,y2))
return true;
return false;
}
bool MoveController::isLongMoving(int x1,int y1,int x2,int y2){
if(checkboard.isNotInBoard(x1,y1) || checkboard.isNotInBoard(x2,y2))
return false;
Checker cell1 = checkboard.getChecker(x1,y1);
Checker cell2 = checkboard.getChecker(x2,y2);
if(cell1 == Checker::empty || cell2 != Checker::empty)
return false;
if(isMyChecker(whiteOnTurn,cell1) == false)
return false;
if(abs(x2 - x1) != abs(y2 - y1)) //is not diagonal
return false;
sf::Vector2i step = sf::Vector2i((x2-x1) / abs(x2-x1), (y2-y1) / abs(y2-y1));
do{
x1 += step.x;
y1 += step.y;
if(checkboard.getChecker(x1,y1) != Checker::empty)
return false;
}while(x1 != x2 && y1 != y2);
return true;
}
bool MoveController::isLongJumping(int x1,int y1,int x2,int y2){
if(checkboard.isNotInBoard(x1,y1) || checkboard.isNotInBoard(x2,y2))
return false;
Checker cell1 = checkboard.getChecker(x1,y1);
Checker cell2 = checkboard.getChecker(x2,y2);
if(cell1 == Checker::empty || cell2 != Checker::empty)
return false;
if(isMyChecker(whiteOnTurn,cell1) == false)
return false;
if(abs(x2 - x1) != abs(y2 - y1)) //is not diagonal
return false;
sf::Vector2i step = sf::Vector2i((x2-x1) / abs(x2-x1), (y2-y1) / abs(y2-y1));
int enemyCheckersCounter = 0;
do{
x1 += step.x;
y1 += step.y;
Checker checker = checkboard.getChecker(x1,y1);
if(isMyChecker(whiteOnTurn,checker) || checker == Checker::jumped_over)
return false;
if(isEnemyChecker(whiteOnTurn,checker))
enemyCheckersCounter++;
}while(x1 != x2 && y1 != y2);
if(enemyCheckersCounter == 1)
return true;
return false;
}
bool MoveController::isBlockedMovingUp(int x,int y){
if(x-1 >= 0 && y-1 >= 0 && checkboard.getChecker(x-1,y-1) == Checker::empty)
return false;
if(x+1 < checkboard.getWidth() && y-1 >= 0 && checkboard.getChecker(x+1,y-1) == Checker::empty)
return false;
return true;
}
bool MoveController::isBlockedMovingDown(int x,int y){
if(x-1 >= 0 && y+1 < checkboard.getHeight() && checkboard.getChecker(x-1,y+1) == Checker::empty)
return false;
if(x+1 < checkboard.getWidth() && y+1 < checkboard.getHeight() && checkboard.getChecker(x+1,y+1) == Checker::empty)
return false;
return true;
}
bool MoveController::isBlockedMoving(int x,int y){
return (isBlockedMovingUp(x,y) && isBlockedMovingDown(x,y) );
}
void MoveController::move(int x1,int y1,int x2,int y2){
if(jumpingCheckerStartPos == sf::Vector2i(-1,-1))
jumpingCheckerStartPos = sf::Vector2i(x1,y1);
checkboard.moveChecker(x1,y1,x2,y2);
sf::Vector2i step = sf::Vector2i((x2-x1) / abs(x2-x1), (y2-y1) / abs(y2-y1));
while(x1 != x2 && y1 != y2){
if(checkboard.getChecker(x1,y1) != Checker::empty)
checkboard.markPawnAsJumpedOver(x1,y1);
x1 += step.x;
y1 += step.y;
}
if(checkboard.jumpedOverCheckers.size() == 0 ||
mjsf.getMaxiSequenceAfter(x2,y2,jumpingCheckerStartPos.x,jumpingCheckerStartPos.y,isPawn(checkboard.getChecker(x2,y2)),checkboard.jumpedOverCheckers) == 0){
moveCheckersType.push_back(checkboard.getChecker(x2,y2));
if((whiteOnTurn == true && y2 == 0) || (whiteOnTurn == false && y2 == checkboard.getHeight()-1))
promote(x2,y2);
nextTurn();
}
}