-
Notifications
You must be signed in to change notification settings - Fork 0
/
ai.cpp
139 lines (111 loc) · 2.94 KB
/
ai.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include "ai.h"
AI::AI(Board *currentBoard)
{
m_currentBoard = currentBoard;
}
Square* AI::makeMove(Board *currentBoard)
{
QVector<Square* > *legalMoves = new QVector<Square* >;
currentBoard->getLegalMoves(legalMoves);
Square *square;
if (!legalMoves->isEmpty())
{
square = legalMoves->first();
}
else
{
square = NULL;
}
currentBoard->makeMove(square->m_x, square->m_y);
return square;
}
//int max(Board *board, int depth)
//{
// QVector<Square*> *legalMoves;
// if (board->getLegalMoves(legalMoves) == false || depth == 0)
// {
// return evaluateBoard();
// }
// int maxWert = -10000;
// int length = legalMoves->length();
// foreach (Square *currentMove, legalMoves) {
// board->makeMove(currentMove->m_x, currentMove->m_y);
// int wert = min(-spieler, depth-1);
// macheZugRueckgaengig();
// }
// while (legalMoves->) {
// int wert = min(-spieler, depth-1);
// macheZugRueckgaengig();
// if (wert > maxWert) {
// maxWert = wert;
// if (depth == gewuenschteTiefe)
// gespeicherterZug = Zug;
// }
// }
// return maxWert;
//}
//int min(int spieler, int tiefe) {
// if (tiefe == 0 or keineZuegeMehr(spieler))
// return bewerten();
// int minWert = unendlich;
// generiereMoeglicheZuege(spieler);
// while (noch Zug da) {
// fuehreNaechstenZugAus();
// int wert = max(-spieler, tiefe-1);
// macheZugRueckgaengig();
// if (wert < minWert) {
// minWert = wert;
// }
// }
// return minWert;
//}
int AI::alphaBeta(Board *board, int depth, int alpha, int beta, bool maximizingPlayer)
{
// TODO time parameter ...
if (depth = 0 || board->m_gameOver)
{
int boardValue = evaluateBoard(board);
//if (boardValue > maxBoardValue)
//return ;
}
QVector<Board *> possibleBoards = board->makeLegalMoves();
if (maximizingPlayer == true)
{
foreach (Board *board, possibleBoards)
{
int newAlpha = alphaBeta(board, depth-1, alpha, beta, false);
if (newAlpha >= alpha)
{
alpha = newAlpha;
}
if (alpha >= beta)
{
break;
}
}
return alpha;
}
else if (maximizingPlayer == false)
{
foreach (Board *board, possibleBoards)
{
int newBeta = alphaBeta(board, depth-1, alpha, beta, false);
if (newBeta <= beta)
{
beta = newBeta;
}
if (alpha >= beta)
{
break;
}
}
return alpha;
}
}
int AI::evaluateBoard(Board *currentBoard)
{
QVector<Square*> *legalMoves;
currentBoard->getLegalMoves(legalMoves);
int numberOfLegalMoves = legalMoves->count();
currentBoard->countDisks();
}