-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChessGame.cpp
More file actions
136 lines (122 loc) · 3.73 KB
/
Copy pathChessGame.cpp
File metadata and controls
136 lines (122 loc) · 3.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
#include "ChessGame.h"
ChessGame::ChessGame(const std::string &fen)
{
std::vector<char> piece = {'r','b','n','k','q','p','R','B','N','K','Q','P'};
std::string pieces_fen = fen.substr(0,fen.find(' '));
int current_index = 0;
Bitboard empty = Bitboard('e',{});
for (auto &pieces : pieces_fen)
{
if (std::isalpha(pieces))
{
if(this->m_BoardStates.find(pieces) == this->m_BoardStates.end())
{
Bitboard curr = Bitboard(pieces,{current_index});
this->m_BoardStates.insert(std::make_pair(pieces, curr));
}else
{
this->m_BoardStates.at(pieces).setPiece(current_index);
}
empty |= this->m_BoardStates.at(pieces);
current_index++;
}
if(std::isdigit(pieces))
{
current_index += int(pieces)-48;
}
}
empty = ~empty;
this->m_BoardStates.insert(std::make_pair('0',empty));
for (char &pie : piece)
{
std::vector<Bitboard> moves(64);
for (int i = 0 ; i <64 ; i++)
{
Bitboard curr(pie, {i});
switch(pie)
{
case 'P':
curr >>= 8;
moves[i] = curr;
break;
case 'p':
curr <<= 8;
moves[i] = curr;
break;
case 'R':
case 'r':
moves[i]= curr.get_slides(i);
break;
case 'B':
case 'b':
moves[i] = curr.get_diagonals(i);
break;
case 'Q':
case 'q':
moves[i] = curr.get_diagonals(i) | curr.get_slides(i);
break;
case 'N':
case 'n':
curr = curr.get_Ls(i);
std::cout << " INDEX : " << i <<std::endl;
curr.printBoard();
std::cout<<std::endl;
break;
default:
break;
}
}
this->m_Precomp.insert(std::make_pair(pie,moves));
}
}
char ChessGame::get_piece_from_index(int index)
{
for(auto &board : this->m_BoardStates)
{
if (board.second.getBoard().test(index))
{
return board.first;
}
}
return ' ';
}
void ChessGame::make_move(int source_index, int destination_index)
{
if (source_index == destination_index)
{
return;
}
char piece = get_piece_from_index(source_index);
if (this->m_BoardStates.find(piece) == this->m_BoardStates.end())
{
return;
}
bool condition = this->m_Precomp.at(piece)[source_index].test(destination_index) && (this->m_BoardStates.at('0').test(destination_index));
if (condition) {
this->m_BoardStates.at(piece).removePiece(source_index);
this->m_BoardStates.at(piece).setPiece(destination_index);
this->m_BoardStates.at('0').setPiece(source_index);
this->m_BoardStates.at('0').removePiece(destination_index);
}
}
const std::unordered_map<char,Bitboard> &ChessGame::getBoardStates() const
{
return this->m_BoardStates;
}
void ChessGame::printGameState()
{
int num_of_boards=0;
for (auto &boards : this->m_BoardStates)
{
std::cout<<boards.first<<std::endl;
boards.second.printBoard();
std::vector<int> index = boards.second.getPieceIndexes();
for(auto &indexes:index)
{
std::cout<<std::endl <<indexes<<std::endl;
}
std::cout<<std::endl;
num_of_boards++;
}
std::cout<<"Number of boards : " << num_of_boards << std::endl;
}