-
Notifications
You must be signed in to change notification settings - Fork 0
/
MiniMaxNode.cs
185 lines (169 loc) · 6.06 KB
/
MiniMaxNode.cs
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;
namespace TronBot
{
public class MiniMaxNode
{
private int player = 0;
public GameState State;
private Evaluator e;
private List<MiniMaxNode> Children;
private MiniMaxNode Parent;
public int score;
private double time;
//private volatile bool timesUp;
//private double time;
//public void timedSearch(int depth)
//{
// timesUp = false;
// var timer = new Timer(800 - _start);
// //timer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
// timer.Elapsed += timer_Elapsed;
// timer.Start();
// expand(depth);
// timer.Stop();
//}
public MiniMaxNode(GameState g, Evaluator e, int player)
{
this.State = g;
this.e = e;
this.player = player;
Parent = this;
}
public MiniMaxNode(GameState g, Evaluator e, int player, MiniMaxNode parent)
{
this.State = g;
this.e = e;
this.player = player;
this.Parent = parent;
}
//public int evaluateAlone(int depth)
//{
// if (Children == null || Children.Count == 0)
// {
// this.score = e.evaluation(State, 0) + depth;
// //Console.Error.WriteLine("MinNode {0}{1} - Depth: {2} Children: {3} - Eval:{4}\n", n.State.Player.X, n.State.Player.Y, Depth, Childen.Count, evaluation);
// return this.score;
// }
// foreach (MiniMaxNode n in Children)
// {
// this.score = n.evaluateAlone(depth+1);
// return this.score;
// }
// return this.score;
//}
public int evaluate(int alpha, int beta)
{
//player is always 0
if (Children == null || Children.Count == 0)
{
if (State == null) return 0;
this.score = e.evaluation(State, 0);
//Console.Error.WriteLine("MinNode {0}{1} - Depth: {2} Children: {3} - Eval:{4}\n", n.State.Player.X, n.State.Player.Y, Depth, Childen.Count, evaluation);
return this.score;
}
//if (alone)
//{
// foreach (MiniMaxNode n in Children)
// {
// this.score = n.evaluateAlone(0);
// return this.score;
// //Console.Error.WriteLine("MinNode {0}{1} - Eval:{2}\n", n.State.Player.X, n.State.Player.Y, evaluation);
// }
// //System.err.println("Min: " + min);
// return beta;
//}
//opponent playing, minimize outcome
if(player == 1)
{
foreach (MiniMaxNode n in Children)
{
this.score = n.evaluate(alpha, beta);
if (this.score <= alpha) return alpha;
if (this.score < beta) beta = this.score;
//Console.Error.WriteLine("MinNode {0}{1} - Eval:{2}\n", n.State.Player.X, n.State.Player.Y, evaluation);
}
//System.err.println("Min: " + min);
return beta;
}
//player playing, maximize outcome
if(player == 0)
{
foreach(MiniMaxNode n in Children)
{
this.score = n.evaluate(alpha, beta);
if (this.score >= beta) return beta;
if (this.score > alpha) alpha = this.score;
// Console.Error.WriteLine("MaxNode {0}{1} - Eval:{2}\n", n.State.Player.X, n.State.Player.Y, evaluation);
}
return alpha;
}
//we should not get here
return 0;
}
public void expand()
{
List<GameState> childStates = new List<GameState>();
Children = new List<MiniMaxNode>();
//if (!alone)
//{
if (player == 0)
{
childStates = Parent.State.getSuccessorStates((player + 1) % 2);
}
else
{
childStates = State.getSuccessorStates((player + 1) % 2);
}
foreach (GameState g in childStates)
{
Children.Add(new MiniMaxNode(g, e, (player + 1) % 2, this));
}
//}
//else
//{
// childStates = State.getSuccessorStates(0);
// foreach (GameState g in childStates)
// {
// Children.Add(new MiniMaxNode(g, e, 0, this, this.alone));
// }
//}
}
private void expand(int depth, double usedTime)
{
this.time = usedTime;
//Console.Error.WriteLine("Timer: "+time);
if (time <= 500 && depth >0)
{
HiPerfTimer timer = new HiPerfTimer();
timer.Start();
expand();
foreach (MiniMaxNode n in Children)
{
expand(depth - 1, timer.Duration * 1000);
}
timer.Stop();
time += timer.Duration * 1000;
}
}
public static MiniMaxNode getGameTree(GameState g, Evaluator e, int depth, double usedTime)
{
MiniMaxNode n = new MiniMaxNode(g, e, 0);
n.expand(depth, usedTime);
return n;
}
public override string ToString()
{
string output = "";
if (Children != null && Children.Count > 0)
foreach (MiniMaxNode n in Children)
{
output += "\t" + n.ToString() + "";
}
return this.State.ToString() + ", Score: " + this.score;
}
}
}