-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomputer.cpp
More file actions
87 lines (72 loc) · 2.43 KB
/
computer.cpp
File metadata and controls
87 lines (72 loc) · 2.43 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
#include "mainwindow.h"
#include <QGridLayout>
#include <QDebug>
#include <QPixmap>
#include <QLabel>
#include <QRect>
#include <QString>
#include <QPushButton>
#include <map>
#include <QRandomGenerator>
int MainWindow::CalculatePosition()
{
int totalValue = 0;
if (Draw(0) || Draw(1))
return 0;
if (Mate(0))
return 100;
if (Mate(1))
return -100;
for (int i = 0; i < 8; ++i)
for (int j = 0; j < 8; ++j)
totalValue += value[piece[i][j]];
return totalValue;
}
MainWindow::ChessMove MainWindow::GetBestMove(bool color)
{
int bestValue = (color ? 101 : -101);
std::vector<ChessMove> bestMoves;
ChessPiece oldPiece[8][8];
for (int i = 0; i < 8; ++i)
for (int j = 0; j < 8; ++j)
oldPiece[i][j] = piece[i][j];
for (int i = 0; i < 8; ++i)
for (int j = 0; j < 8; ++j)
{
if (IsWhite(piece[i][j]) == color || piece[i][j] == blank)
continue;
std::vector<std::pair<int,int>> moves = GetLegalMoves(i, j);
for (const auto& curr : moves)
{
Move(i, j, curr.first, curr.second, false);
ChessMove wMove;
if (color)
{
wMove = GetBestMove(0);
Move(wMove.ii, wMove.ij, wMove.fi, wMove.fj, false);
}
int currValue = CalculatePosition();
if ((color == 0 ? -1 : 1) * bestValue > (color == 0 ? -1 : 1) * currValue)
{
bestValue = currValue;
bestMoves.clear();
}
if (bestValue == currValue)
bestMoves.push_back(ChessMove(i, j, curr.first, curr.second));
SetPiece(i, j, oldPiece[i][j], false);
SetPiece(curr.first, curr.second, oldPiece[curr.first][curr.second], false);
if (color)
{
SetPiece(wMove.ii, wMove.ij, oldPiece[wMove.ii][wMove.ij], false);
SetPiece(wMove.fi, wMove.fj, oldPiece[wMove.fi][wMove.fj], false);
}
}
}
for (int i = 0; i < 8; ++i)
for (int j = 0; j < 8; ++j)
SetPiece(i, j, oldPiece[i][j], true);
if (bestMoves.empty())
return ChessMove(0, 0, 0, 0);
int randIndx = QRandomGenerator::global()->generate() % bestMoves.size();
return bestMoves[randIndx];
}