-
Notifications
You must be signed in to change notification settings - Fork 0
/
MCEngine.cs
98 lines (89 loc) · 2.84 KB
/
MCEngine.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace FourInARow
{
class MCEngine : Player
{
private int simulationsCount;
private Game g;
private Dictionary<int, double> scores;
private Dictionary<int, int> visitedCounts;
private static Random r = new Random();
public override int selectMove()
{
scores.Clear();
visitedCounts.Clear();
foreach (var item in g.possibleMoves)
{
scores.Add(item, 0);
visitedCounts.Add(item, 0);
}
for (int i = 0; i < simulationsCount; i++)
{
Game simulGame = g.clone();
int move = simulGame.possibleMoves[r.Next(simulGame.possibleMoves.Count)];
simulGame.playMove(simulGame.playerToMove, move);
while (!simulGame.hasFinished)
{
simulGame.playRandomMove();
}
switch(simulGame.winner)
{
case null:
scores[move] += 0.5;
break;
case true:
scores[move] += 1;
break;
case false: //nothing here
break;
}
visitedCounts[move]++;
}
double bestScore = -1;
int bestMove = -1;
string s = "";
foreach (var item in scores.Keys)
{
double score = scores[item] / visitedCounts[item];
s += " cislo: " + item + " score: " + score;
if (score > bestScore)
{
bestScore = score;
bestMove = item;
}
}
//MessageBox.Show(s);
return bestMove;
}
public override void playMove(bool isThisPlayer, int column)
{
g.playMove(isThisPlayer, column);
}
public override string getName()
{
return "MC engine. " + simulationsCount + " simulations.";
}
public MCEngine(int simulationsCount, int sizeX, int sizeY)
{
this.simulationsCount = simulationsCount;
this.visitedCounts = new Dictionary<int, int>();
this.scores = new Dictionary<int, double>();
this.g = new Game(sizeX, sizeY);
}
public override void Reset()
{
g.Reset();
this.visitedCounts = new Dictionary<int, int>();
this.scores = new Dictionary<int, double>();
base.Reset();
}
public override void handleClick(int column)
{
//nothing here
}
}
}