forked from tleyden/checkerlution
/
checkerlution_test.go
106 lines (75 loc) · 3.51 KB
/
checkerlution_test.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package checkerlution
import (
"github.com/couchbaselabs/go.assert"
"github.com/couchbaselabs/logg"
ng "github.com/tleyden/neurgo"
"testing"
)
func TestExtractPossibleMoves(t *testing.T) {
jsonString := FakeGameJson()
gameState := NewGameStateFromString(jsonString)
checkerlution := &Checkerlution{}
checkerlution.ourTeamId = RED_TEAM
possibleMoves := checkerlution.extractPossibleMoves(gameState)
possibleMove := possibleMoves[0]
assert.Equals(t, possibleMove.validMove.StartLocation, 7)
assert.Equals(t, possibleMove.validMove.PieceId, 6)
assert.Equals(t, len(possibleMoves), 8)
}
func TestExtractGameStateVector(t *testing.T) {
jsonString := FakeGameJson()
gameState := NewGameStateFromString(jsonString)
checkerlution := &Checkerlution{}
checkerlution.ourTeamId = RED_TEAM
gameStateVector := checkerlution.extractGameStateVector(gameState)
logg.LogTo("TEST", "gameStateVector: %v", gameStateVector)
// at location 1, which has index 0, is our (Team 0) king.
// so we expect to see a 1.0 there
assert.True(t, gameStateVector[0] == OUR_KING)
// the opponent has a normal piece on last square (location 32)
assert.True(t, gameStateVector[31] == OPPONENT_PIECE)
}
func TestChooseBestMove(t *testing.T) {
ng.SeedRandom()
logg.LogKeys["MAIN"] = true
checkerlution := &Checkerlution{}
checkerlution.ourTeamId = RED_TEAM
checkerlution.CreateNeurgoCortex()
cortex := checkerlution.cortex
cortex.Run()
gameState, possibleMoves := FakeGameDocument()
bestMove := checkerlution.chooseBestMove(gameState, possibleMoves)
logg.LogTo("TEST", "bestMove: %v", &bestMove)
found := false
for _, possibleMove := range possibleMoves {
logg.LogTo("TEST", "possibleMove: %v", &possibleMove)
if possibleMove.Equals(bestMove) {
found = true
}
}
assert.True(t, found)
cortex.Shutdown()
}
func FakeGameDocument() (gameState []float64, possibleMoves []ValidMoveCortexInput) {
gameState = NewGameStateVector()
possibleMove1 := ValidMoveCortexInput{
startLocation: 0,
isCurrentlyKing: -1,
endLocation: 1.0,
willBecomeKing: -0.5,
captureValue: 1,
}
possibleMove2 := ValidMoveCortexInput{
startLocation: 1,
isCurrentlyKing: -0.5,
endLocation: 0.0,
willBecomeKing: 0.5,
captureValue: 0,
}
possibleMoves = []ValidMoveCortexInput{possibleMove1, possibleMove2}
return
}
func FakeGameJson() string {
jsonString := `{"applicationUrl":"http://www.couchbase.com/checkers","applicationName":"Couchbase Checkers","revotingAllowed":false,"highlightPiecesWithMoves":true,"number":1,"startTime":"2013-08-26T16:05:30Z","moveDeadline":"2013-08-26T16:05:45Z","turn":1,"activeTeam":0,"winningTeam":0,"moves":[],"teams":[{"participantCount":117983,"score":11,"pieces":[{"location":1,"king":true},{"location":2},{"location":3},{"location":4},{"location":5},{"location":6},{"location":7,"validMoves":[{"locations":[11],"captures":[{"team":1,"piece":11}],"king":true}]},{"location":8,"validMoves":[{"locations":[11],"captures":[{"team":1,"piece":8},{"team":1,"piece":9},{"team":1,"piece":10}]},{"locations":[11,15]}]},{"location":9,"validMoves":[{"locations":[13]},{"locations":[14]}]},{"location":10,"validMoves":[{"locations":[14]},{"locations":[15]}]},{"location":11,"captured":true},{"location":12,"king":true,"validMoves":[{"locations":[16]}]}]},{"participantCount":109217,"score":12,"pieces":[{"location":21},{"location":22},{"location":23},{"location":24},{"location":25},{"location":26},{"location":27},{"location":28},{"location":29},{"location":30},{"location":31},{"location":32}]}]}`
return jsonString
}