-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpieceslogic.cpp
More file actions
199 lines (189 loc) · 4.73 KB
/
Copy pathpieceslogic.cpp
File metadata and controls
199 lines (189 loc) · 4.73 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#include "headers/pieceslogic.h"
#include<iostream>
#include<unordered_map>
#include<vector>
using namespace std;
Pieces::Pieces(){
unordered_map <int,char> pieces_map={
{0,'.'},
{1,'R'},
{2,'N'},
{3,'B'},
{4,'K'},
{5,'Q'},
{6,'P'},
{-1,'r'},
{-2,'n'},
{-3,'b'},
{-4,'k'},
{-5,'q'},
{-6,'p'}
};
unordered_map <int,int> pieces_value={
{0,0},
{1,4},
{2,3},
{3,3},
{4,200},
{5,9},
{6,1},
{-1,4},
{-2,3},
{-3,3},
{-4,200},
{-5,9},
{-6,1}
};
}
bool Pieces:: validate_move(
int &start_piece,
int &end_piece,
vector<vector<int>> &board,
vector<vector<int>> &positions,
bool p1sturn
){
int x1=positions[0][0];
int y1=positions[0][1];
int x2=positions[1][0];
int y2=positions[1][1];
vector<vector<int>> allowed_pos = {};
if((p1sturn==true && start_piece<0) || (p1sturn==false && start_piece>0)){
return false;
}
if(start_piece==6){
if(board[x1-1][y1]==0){
allowed_pos.push_back({x1-1,y1});
}
if(x1==6 && board[x1-2][y1]==0){ // starting rank
allowed_pos.push_back({x1-2,y1});
}
if(board[x1-1][y1-1]<0){
allowed_pos.push_back({x1-1,y1-1});
}
if(board[x1-1][y1+1]<0){
allowed_pos.push_back({x1-1,y1+1});
}
}else if(start_piece==-6){
if(board[x1+1][y1]==0){
allowed_pos.push_back({x1+1,y1});
}
if(x1==1 && board[x1+2][y1]==0){ // starting rank
allowed_pos.push_back({x1+2,y1});
}
if(board[x1+1][y1-1]>0){
allowed_pos.push_back({x1+1,y1-1});
}
if(board[x1+1][y1+1]>0){
allowed_pos.push_back({x1+1,y1+1});
}
}else if(start_piece==1){
//up
for(int i=x1-1; i>=0; i--){
if(board[i+1][y1]<0 || board[i][y1]>0){
break;
}
allowed_pos.push_back({i,y1});
cout<<i<<" ";
}
//down
for(int i=x1+1; i<8; i++){
if(board[i-1][y1]<0 || board[i][y1]>0){
break;
}
allowed_pos.push_back({i,y1});
cout<<i<<" ";
}
//right
for(int j=y1+1; j<8; j++){
if(board[x1][j-1]<0 || board[x1][j]>0){
break;
}
allowed_pos.push_back({x1,j});
cout<<j<<" ";
}
//left
for(int j=y1-1; j>=0; j--){
if(board[x1][j+1]<0 || board[x1][j]>0){
break;
}
allowed_pos.push_back({x1,j});
cout<<j<<" ";
}
}else if(start_piece==-1){
//up
for(int i=x1-1; i>=0; i--){
if(board[i+1][y1]>0 || board[i][y1]<0){
break;
}
allowed_pos.push_back({i,y1});
cout<<i<<" ";
}
//down
for(int i=x1+1; i<8; i++){
if(board[i-1][y1]>0 || board[i][y1]<0){
break;
}
allowed_pos.push_back({i,y1});
cout<<i<<" ";
}
//right
for(int j=y1+1; j<8; j++){
if(board[x1][j-1]>0 || board[x1][j]<0){
break;
}
allowed_pos.push_back({x1,j});
cout<<j<<" ";
}
//left
for(int j=y1-1; j>=0; j--){
if(board[x1][j+1]>0 || board[x1][j]<0){
break;
}
allowed_pos.push_back({x1,j});
cout<<j<<" ";
}
}else if(start_piece==2 || start_piece==-2){
//up
if(x1-2>=0){
if(y1-1>=0){
allowed_pos.push_back({x1-2,y1-1});
}
if(y1+1<8){
allowed_pos.push_back({x1-2,y1+1});
}
}
//down
if(x1+2<8){
if(y1-1>=0){
allowed_pos.push_back({x1+2,y1-1});
}
if(y1+1<8){
allowed_pos.push_back({x1+2,y1+1});
}
}
//left
if(y1-2>=0){
if(x1-1>=0){
allowed_pos.push_back({x1-1,y1-2});
}
if(x1+1<8){
allowed_pos.push_back({x1+1,y1-2});
}
}
//right
if(y1+2<8){
if(x1-1>=0){
allowed_pos.push_back({x1-1,y1+2});
}
if(x1+1<8){
allowed_pos.push_back({x1+1,y1+2});
}
}
}
for(vector<int> position : allowed_pos){
if(positions[1]==position){
return true;
}
}
return false;
}