-
Notifications
You must be signed in to change notification settings - Fork 1
/
monte.go
63 lines (53 loc) · 1.29 KB
/
monte.go
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
package main
type PlayoutResult struct {
score float64
hand Hand
}
type MonteCarloPlayer struct {
State GameState
tryCount int
}
func calcScore(sim Simulator) float64 {
return float64(sim.Score() + sim.GetMaxTile()*200)
}
func (p *MonteCarloPlayer) SetState(s GameState) {
p.State = s
}
func (p *MonteCarloPlayer) Playout(firstHand Hand, res chan PlayoutResult, gen func() uint32) {
avg := 0.0
for cnt := 0; cnt < p.tryCount; cnt++ {
var sim Simulator = &Kanna{p.State.Grid, p.State.Score, p.State.Over, gen}
if !sim.Move(firstHand) {
avg -= 100
break
}
sim.AddRandomCell()
for sim.Move(intToHand(int(gen() % 4))) {
if !sim.AddRandomCell() {
break
}
}
avg += calcScore(sim)
}
res <- PlayoutResult{avg / float64(p.tryCount), firstHand}
}
func (p *MonteCarloPlayer) NextHand(gen func() uint32) Hand {
res := make(chan PlayoutResult)
go p.Playout(Up, res, gen)
go p.Playout(Right, res, gen)
go p.Playout(Down, res, gen)
go p.Playout(Left, res, gen)
bestAvg, ret := 0.0, Up
for i := 0; i < 4; i++ {
r := <-res
if bestAvg < r.score {
bestAvg = r.score
ret = r.hand
}
}
// fmt.Printf("after best move %v:\n", ret)
// var sim Midori = Midori{encode2(&p.State.Grid), 0, p.State.Over, r}
// sim.Move(ret)
// fmt.Printf("%v\n", sim.GetState())
return ret
}