-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExpectimax.cs
More file actions
130 lines (109 loc) · 4.49 KB
/
Expectimax.cs
File metadata and controls
130 lines (109 loc) · 4.49 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
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
using System;
using System.Threading;
using System.Collections.Generic;
namespace GameOf2048
{
public class Expectimax
{
private Engine engine;
private int maxDepth;
private double[,] TileWeights = new double[,] {{16,11,10,9},
{1,2,3,4},
{0.8,0.7,0.6,0.5},
{0.1,0.2,0.3,0.4}};
public Expectimax(int maxDepth)
{
this.engine = new Engine();
this.maxDepth = maxDepth;
}
public void TopLevelMove(Moves move, int[][] board, ref double score)
{
// Wrapper method to start the search tree for a single move.
// This way we can use it together with mutlithreading.
int[][] newBoard = Engine.CopyBoard(board);
int mergedValue = engine.ExecuteMove(move, newBoard);
if (!Engine.BoardEquals(newBoard, board))
score = Math.Max(score, ChanceNode(newBoard, 0));
else
score = 0;
}
private double ChanceNode(int[][] board, int depth)
{
double score = 0;
// Stop if the state of the board has not changed or
// the maximum depth has been reached.
if (depth >= maxDepth)
return CalculateScore(board);
// Go futher down the tree. Each empty field has a chance of spawing a number,
// so we have to create a new branch for every possibility.
// For performance reasons we ignore the chance factor of individual tiles.
// e.g. 1/emptyTiles
for (int i = 0; i < board.Length; i++)
{
for (int j = 0; j < board[i].Length; j++)
{
if (board[i][j] == 0)
{
// The number 2 has a chance of 90% to appear next, while 4 has only 10%
board[i][j] = 2;
score += MoveNode(Engine.CopyBoard(board), depth) * 0.9;
board[i][j] = 4;
score += MoveNode(Engine.CopyBoard(board), depth) * 0.1;
// Reset the field
board[i][j] = 0;
}
}
}
return score;
}
private double MoveNode(int[][] board, int depth)
{
double score = 0;
depth++;
// Execute all possible moves and give the new state to the chance node.
// The biggest score will be returned or 0 if no moves are possible.
for (int i = 0; i < 4; i++)
{
int[][] newBoard = Engine.CopyBoard(board);
int mergedValue = engine.ExecuteMove((Moves)i, newBoard);
if (!Engine.BoardEquals(newBoard, board))
score = Math.Max(score, ChanceNode(newBoard, depth));
}
return score;
}
private double CalculateScore(int[][] board)
{
double score = 0;
double value = 0;
int emptyTiles = 0;
// Calculate the heuristic value of the given board.
for (int i = 0; i < board.Length; i++)
{
for (int j = 0; j < board[i].Length; j++)
{
value = 0;
if (board[i][j] == 0)
{
emptyTiles++;
}
else
{
value = board[i][j];
// bonus if prev > current > next
if (j > 0 && board[i][j - 1] > value)
value *= 2;
if (j < board[i].Length - 1 && board[i][j + 1] < value)
value *= board[i][j + 1] == board[i][j] / 2 ? 4 : 2;
// bonus if above > current > below
if (i > 0 && board[i - 1][j] > value)
value *= 2;
if (i < board.Length - 1 && board[i + 1][j] < value)
value *= 2;
score += value * TileWeights[i, j] * TileWeights[i, j];
}
}
}
return score * emptyTiles * emptyTiles;
}
}
}