-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameGothicCheckersAI.cpp
116 lines (93 loc) · 3.4 KB
/
GameGothicCheckersAI.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
#include <QTime>
#include "GameGothicCheckersAI.h"
Turn GameGothicCheckersAI::ComputeBestTurn(Player* player, GameBoard* board, Game* game)
{
Turn bestTurn;
int depth = _depthByDifficulty[player->GetGameDifficulty()];
Turns turns = game->GetAllValidTurns(player, board, true);
int bestRating = -MAX_VALUE;
for (Turn& turn : turns)
{
board->DoTurn(turn, false);
int rating = MiniMax(false, sGameManager->GetOponentOf(player), game, board, depth - 1);
board->UndoTurn(turn);
if (rating > bestRating)
{
bestRating = rating;
bestTurn = turn;
}
}
return bestTurn;
}
int GameGothicCheckersAI::Evaluate(Player* player, GameBoard* board)
{
QTime time = QTime::currentTime();
qsrand((uint)time.msec());
int whiteScore = 0;
int blackScore = 0;
std::vector<ChessPiece*> wPieces = board->GetAllChessPiecesByColor(PIECE_COLOR_WHITE);
std::vector<ChessPiece*> bPieces = board->GetAllChessPiecesByColor(PIECE_COLOR_BLACK);
for (ChessPiece* piece : wPieces)
{
// Břičtení bonusu za umístění figurek
Position pos = piece->GetPiecePosition();
if (piece->GetPieceType() == PIECE_TYPE_PAWN)
{
whiteScore += _whitePawnBonus[pos.row][pos.col] + PAWN_SCORE;
// Blíže k povýšení na dámu, více bodů a malá náhodná složka simulující chybu člověka
int promotionPoints = 1 + 7 - pos.row;
whiteScore += promotionPoints * (qrand() % promotionPoints);
}
else
whiteScore += _whiteKingBonus[pos.row][pos.col] + QUEEN_SCORE;
}
for (ChessPiece* piece : bPieces)
{
// Břičtení bonusu za umístění figurek
Position pos = piece->GetPiecePosition();
if (piece->GetPieceType() == PIECE_TYPE_PAWN)
{
blackScore += _blackPawnBonus[pos.row][pos.col] + PAWN_SCORE;
// Blíže k povýšení na dámu, více bodů a malá náhodná složka simulující chybu člověka
int promotionPoints = 1 + pos.row;
blackScore += promotionPoints * (qrand() % promotionPoints);
}
else
blackScore += _blackKingBonus[pos.row][pos.col] + QUEEN_SCORE;
}
if (sGameManager->GetOponentOf(player)->GetPlayerColor() == PIECE_COLOR_WHITE)
return blackScore - whiteScore;
else
return whiteScore - blackScore;
return 0;
}
int GameGothicCheckersAI::MiniMax(bool maximizing, Player* player, Game* game, GameBoard* board, int depth)
{
if (depth == 0)
return Evaluate(player, board);
Turns turns = game->GetAllValidTurns(player, board, true);
if (turns.empty())
return Evaluate(player, board);
if (maximizing)
{
int bestValue = -MAX_VALUE;
for (Turn& turn : turns)
{
board->DoTurn(turn, false);
bestValue = std::max(bestValue, MiniMax(!maximizing, sGameManager->GetOponentOf(player), game, board, depth - 1));
board->UndoTurn(turn);
}
return bestValue;
}
else
{
int bestValue = MAX_VALUE;
for (Turn& turn : turns)
{
board->DoTurn(turn, false);
bestValue = std::min(bestValue, MiniMax(!maximizing, sGameManager->GetOponentOf(player), game, board, depth - 1));
board->UndoTurn(turn);
}
return bestValue;
}
}