My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Tron.DataStructures;
using Tron.Players.Simple;
using Tron.Players.Strategic;

namespace Tron.Players.Search
{
class IDSMiniMaxSearchPlayer : Player
{
#region Fields for situation when player is alone in connected component.
StrategicPlayer strategicPlayer;
StrategicPlayerHelper strategicPlayerHelper = new StrategicPlayerHelper();
bool opponentInSeperateComponent = false;
#endregion

public static readonly int EMPTY_BLOCK = Constants.EMPTY_BLOCK;
public static readonly int DEFAULT_TURN_TIME = 200;

int opponentIndex;
Vector2 bestMove;
int depthLimit = 3;

public override void GameOver(Percepts percepts, List<int> playerScores)
{
base.GameOver(percepts, playerScores);
opponentInSeperateComponent = false;
}

StrategicPlayerHelperHeuristic strategicPlayerHelperHeuristic = new StrategicPlayerHelperHeuristic();

public IDSMiniMaxSearchPlayer(int playerIndex)
: this(playerIndex, DEFAULT_TURN_TIME)
{
strategicPlayer = new StrategicPlayer(playerIndex);
}

/// <summary>
/// Constructs an iterative deepening alpha beta search player with the given player index and move time.
/// </summary>
/// <param name="moveTime">The time in milliseconds that the player is given to move</param>
public IDSMiniMaxSearchPlayer(int playerIndex, int moveTime)
: base(playerIndex) { }

public override void Update(Percepts percepts, Actions actions)
{
if (percepts.PlayerStates.Count != 2)
throw new NotImplementedException();

int[,] board = percepts.Board.GetCopyOfBoardArray();

opponentIndex = (PlayerIndex == 1) ? 0 : 1;

Vector2 playerPosition = percepts.PlayerStates[PlayerIndex].Position;
Vector2 opponentPosition = percepts.PlayerStates[opponentIndex].Position;

#region Handle situation when player is alone in connected component.
if (opponentInSeperateComponent || !percepts.PlayerStates[opponentIndex].Alive ||
strategicPlayerHelper.IsOpponentInSeperateComponent(board, playerPosition, opponentPosition))
{
opponentInSeperateComponent = true;
strategicPlayer.Update(percepts, actions);
return;
}
#endregion

actions.MoveDirection = minimaxSearch(board,playerPosition,opponentPosition);
}

Vector2 minimaxSearch(int[,] board, Vector2 playerPosition, Vector2 opponentPosition)
{
#region Null Checks (Commented)
//if (board == null) throw new NullReferenceException("percepts is null");
//if (agentLoc == null) throw new NullReferenceException("agentLoc is null");
//if (opponentLoc == null) throw new NullReferenceException("opponentLoc is null");
#endregion

#region Console Output (Commented)
//Console.WriteLine("MinimaxSearch:");
#endregion Console Output (Commented)

maxValue(board, 0, playerPosition, opponentPosition);

#region Console Output (Commented)
//Console.WriteLine(" agent location: {0}", playerPosition.ToString());
//Console.WriteLine(" opponent location: {0}", opponentPosition.ToString());
//Console.WriteLine(" SELECTED MOVE: {0}\n\n", bestMoves[savedIndex]);
#endregion

return bestMove;
}

double maxValue(int[,] board, int depth, Vector2 playerPosition, Vector2 opponentPosition)
{
double largestMin = Double.NegativeInfinity;
bool isTerminal = true;
if (depth < depthLimit)
{
List<Vector2> possibleMoveDirections = SimplePlayerHelper.GetPossibleDirectionsFromPosition(board, playerPosition);
if (possibleMoveDirections.Count > 0)
{
isTerminal = false;
foreach (Vector2 possibleMoveDirection in possibleMoveDirections)
{
Vector2 possibleNextPosition = playerPosition + possibleMoveDirection;

board[(int)possibleNextPosition.X, (int)possibleNextPosition.Y] = PlayerIndex;

double min = minValue(board, depth, possibleNextPosition, opponentPosition);

board[(int)possibleNextPosition.X, (int)possibleNextPosition.Y] = EMPTY_BLOCK;

if (min > largestMin)
{
largestMin = min;
if (depth == 0)
{
bestMove = possibleMoveDirection;
}
}
}
}
else
{
// MAX has no options. The utility here depends on whether MIN has any options (since MIN must go next).
if (SimplePlayerHelper.GetPossibleDirectionsFromPosition(board, opponentPosition).Count == 0)
{
return 0;
}
else
{
return -1;
}
}
}

if (isTerminal)
{
largestMin = GetUtilityOfPosition(board, playerPosition, opponentPosition, depth + 1, true);
}

return largestMin;
}

double minValue(int[,] board, int depth, Vector2 playerPosition, Vector2 opponentPosition)
{
double smallestMax = Double.PositiveInfinity;
bool isTerminal = true;
if (depth < depthLimit)
{
// Make sure to consider moving onto the other player if he is adjacent (since MIN moves second in the search).
List<Vector2> possibleMoveDirections = SimplePlayerHelper.GetPossibleDirectionsFromPositionIncludingInputPositionEvenIfItIsAWall(
board, opponentPosition, playerPosition);
if (possibleMoveDirections.Count > 0)
{
isTerminal = false;
foreach (Vector2 possibleMoveDirection in possibleMoveDirections)
{
Vector2 possibleNextPosition = opponentPosition + possibleMoveDirection;

if (possibleNextPosition == playerPosition)
{
// MIN moved onto MAX.
return 0;
}

board[(int)possibleNextPosition.X, (int)possibleNextPosition.Y] = opponentIndex;

double max = maxValue(board, depth + 1, playerPosition, possibleNextPosition);

board[(int)possibleNextPosition.X, (int)possibleNextPosition.Y] = EMPTY_BLOCK;

if (max < smallestMax)
{
smallestMax = max;
}
}
}
else
{
// MAX moved but MIN has no options.
return 1;
}
}

if (isTerminal)
{
smallestMax = GetUtilityOfPosition(board, playerPosition, opponentPosition, depth, false);
}

return smallestMax;
}

/// <summary>
/// Returns a value between -1 and 1 that estimates how good a state is for a player.
/// -1 must be returned if the player has lost.
/// 0 must be returned if the player has tied.
/// 1 must be returned if the player has won.
/// </summary>
/// <param name="calledFromMax">Decides whether a player collision is a -1 (from min) or a 0 (from max)</param>
/// <returns></returns>
private double GetUtilityOfPosition(int[,] board, Vector2 playerPosition, Vector2 opponentPosition, int depth, bool calledFromMax)
{
return strategicPlayerHelperHeuristic.Evaluate(board, depth, playerPosition, opponentPosition, true);
}
}
}

Change log

r100 by shieldshock on Dec 11, 2008   Diff
[No log message]
Go to: 
Project members, sign in to write a code review

Older revisions

r97 by shieldshock on Dec 9, 2008   Diff
[No log message]
r93 by shieldshock on Dec 9, 2008   Diff
[No log message]
r90 by shieldshock on Dec 9, 2008   Diff
[No log message]
All revisions of this file

File info

Size: 8737 bytes, 210 lines
Powered by Google Project Hosting