-
Notifications
You must be signed in to change notification settings - Fork 2
/
human.cc
90 lines (84 loc) · 3.06 KB
/
human.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
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
#include "human.h"
#include <iostream>
#include <string>
using namespace std;
Human::Human(bool side) : side{side} {}
PlayerType Human::getPlayerType(){ return human; }
bool Human::checkMove(Board* b, ChessMove nextmove){
vector<PossibleMoves> pm = b->getAllAvailableMoves(side);
for(auto m : pm){
if(m.start == nextmove.first){
for(auto d: m.destination){
if(d == nextmove.second){
return true;
}
}
}
}
return false;
}
ChessMove Human::getNextMove(Board* b){
Position pos1, pos2;
string src, target;
ChessMove result;
while(cin>>src>>target){
if((src.size() != 2 || target.size() != 2 ) ||
!('@' < toupper(src.at(0)) && toupper(src.at(0)) < 'I' &&
'@' < toupper(target.at(0)) && toupper(target.at(0)) < 'I')){
if(!cin){return result;}
cout<<"Invalid command. Should be of format 'move e2 e4'."<<endl;
cin >> src;
continue;
}
char cinx = toupper(src[0]), coutx = toupper(target[0]);
locationx inx = charToX(cinx), outx = charToX(coutx);
int iny = src[1] - '0', outy = target[1] - '0';
pos1 = make_pair(inx, iny);
pos2 = make_pair(outx, outy);
result = make_pair(pos1, pos2);
if(checkMove(b, result)){
b->testMove(result, true);
if(b->inCheck(this->side)){
cout<<"You cannot move that piece, your king is currently in check!"<<endl;
ChessMove original = make_pair(pos2, pos1);
b->badMove(original, true);
cin>>src;
continue;
}
if(pos2.second == 1 || pos2.second == 8){
if(b->getType(pos2).first == PieceType::Pawn){
while(cin >> src){
if((src == "N" || src == "B" || src == "R" || src == "Q")){
b->promote(pos2, toupper(src[0]));
break;
}
else{
std::cout << "Invalid command, input should be N, B, R, or Q" << std::endl;
}
}
}
}
break;
}
cout << "Invalid move, try again\n";
cin >> src;
}
if(!cin.eof()){
//check if your opponent is currently in check after the move
if(b->inCheck(!(this->side))){
side ? cout<<"White is in check"<<endl : cout<<"Black is in check"<<endl;
}
//if its checkmate
if(checkMate(b, this->side)){
//but nothing can actually capture the king, its stalemate
if(checkStale(b, this->side)){
ChessMove staleGame = make_pair(make_pair(A, -2), make_pair(A, -2));
return staleGame;
}
//else you just won
ChessMove endGame = make_pair(make_pair(A, -1), make_pair(A, -1));
return endGame;
}
}
return result;
}