-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMiniMax.cpp
More file actions
99 lines (90 loc) · 2.15 KB
/
MiniMax.cpp
File metadata and controls
99 lines (90 loc) · 2.15 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
//
// MiniMax.cpp
// Chess
//
// Created by mustafa tok on 10.12.2011.
//
#include <iostream>
#include "MiniMax.h"
#include "Board.h"
bool CutOffTest(Board* state, clock_t st){
clock_t ft = clock();
float diff = ((float)ft - (float)st) / 1000000.0F;
if(state->getLevel()==4 || state->isFinished() || diff > 99){
return true;
}
return false;
}
int Min(int x, int y);
int changeTurn(int turn);
int Max(int x,int y){
return (x > y)? x : y;
}
int Min(int x, int y){
return (x < y)? x : y;
}
int changeTurn(int turn){
if(turn == BLACK)
return WHITE;
else
return BLACK;
}
int MaxValue(Board* state, int turn, clock_t st){
int alpha = -99999;
if(CutOffTest(state, st)){
return state->getValue();
}
BoardQueue *q;
q = state->successor(turn);
// cout << endl;
while(!q->isEmpty()){
Board* s = q->dequeue();
alpha = Max(alpha, MinValue(s,changeTurn(turn), st));
delete s;
}
delete q;
return alpha;
}
int MinValue(Board* state, int turn, clock_t st){
int beta = 99999;
if(CutOffTest(state, st)){
return state->getValue();
}
BoardQueue *q;
q = state->successor(turn);
// cout << endl;
while(!q->isEmpty()){
Board* s = q->dequeue();
beta = Min(beta, MaxValue(s,changeTurn(turn), st));
delete s;
}
delete q;
return beta;
}
Board* MinimaxDecision(Board* board, int turn, clock_t st){
BoardQueue *q;
q = board->successor(turn);
cout << endl;
Board* tmpboard = NULL;
int tmpvalue = -99999;
int min;
while(!q->isEmpty()){
Board* b = q->dequeue();
min = MinValue(b, changeTurn(turn),st);
if(min > tmpvalue){
tmpvalue = min;
if(tmpboard != NULL){
Board* tmmp = tmpboard;
tmpboard = b;
delete tmmp;
}else{
tmpboard = b;
}
}else{
delete b;/////// Burayı control et hacıııı
}
}
tmpboard->resetLevel();
cout << "Min : " << tmpvalue << endl;
return tmpboard;
}