/
game.go
86 lines (72 loc) · 1.54 KB
/
game.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package main
import (
"fmt"
"github.com/umisama/procon26/materials"
"github.com/umisama/procon26/plan"
"sync"
)
type Game interface {
Run() *plan.Plan
}
func readGameMaterials(lines []string) (*materials.Field, []*materials.StoneBase, error) {
field, err := materials.NewField(lines[0:32])
if err != nil {
return nil, nil, err
}
numStone := 0
fmt.Sscanf(lines[33], "%d", &numStone)
stoneBase := make([]*materials.StoneBase, numStone)
for i := 0; i < numStone; i++ {
newStone, err := materials.NewStoneBase(i, lines[34+9*i:34+9*i+8])
if err != nil {
return nil, nil, err
}
stoneBase[i] = newStone
}
return field, stoneBase, nil
}
type BestMgr struct {
best *plan.Plan
mu sync.Mutex
// cache
score int
numOfPiece int
}
func (b *BestMgr) Score() int {
b.mu.Lock()
defer b.mu.Unlock()
if b.best == nil {
return 0x8fffffff
}
return b.score
}
func (b *BestMgr) Set(candidate *plan.Plan) {
if candidate == nil {
return
}
b.mu.Lock()
defer b.mu.Unlock()
if comparePlan(b.best, candidate) == '<' {
b.set(candidate)
}
}
func (b *BestMgr) set(candidate *plan.Plan) {
b.best = candidate
b.score = b.best.Score()
b.numOfPiece = b.best.NumberOfPiece()
}
func (b *BestMgr) Get() *plan.Plan {
b.mu.Lock()
defer b.mu.Unlock()
return b.best
}
func comparePlan(f, s *plan.Plan) rune {
if f.Score() < s.Score() {
return '>'
} else if f.Score() == s.Score() && f.NumberOfPiece() < s.NumberOfPiece() {
return '>'
} else if f.Score() == s.Score() && f.NumberOfPiece() == s.NumberOfPiece() {
return '='
}
return '<'
}