-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrobot.go
130 lines (108 loc) · 3.69 KB
/
robot.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// robot.go
// Copyright (C) 2024 Vilhjálmur Þorsteinsson / Miðeind ehf.
// This file implements robots that play a crossword game
// in the SCRABBLE(tm) genre.
/*
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package skrafl
import (
"math/rand"
"sort"
)
// Robot is an interface for automatic players that implement
// a playing strategy to pick a move given a list of legal tile
// moves.
type Robot interface {
PickMove(state *GameState, moves []Move) Move
}
// RobotWrapper wraps a Robot implementation
type RobotWrapper struct {
Robot
}
// GenerateMove generates a list of legal tile moves, then
// asks the wrapped robot to pick one of them to play
func (rw *RobotWrapper) GenerateMove(state *GameState) Move {
moves := state.GenerateMoves()
return rw.PickMove(state, moves)
}
// HighScoreRobot implements a simple strategy: it always picks
// the highest-scoring move available, or exchanges all tiles
// if there is no valid tile move, or passes if exchange is not
// allowed.
type HighScoreRobot struct {
}
// OneOfNBestRobot picks one of the N highest-scoring moves at random.
type OneOfNBestRobot struct {
N int
}
// Implement a strategy for sorting move lists by score
type byScore struct {
state *GameState
moves []Move
}
func (list byScore) Len() int {
return len(list.moves)
}
func (list byScore) Swap(i, j int) {
list.moves[i], list.moves[j] = list.moves[j], list.moves[i]
}
func (list byScore) Less(i, j int) bool {
// We want descending order, so we reverse the comparison
return list.moves[i].Score(list.state) > list.moves[j].Score(list.state)
}
// PickMove for a HighScoreRobot picks the highest scoring move available,
// or an exchange move, or a pass move as a last resort
func (robot *HighScoreRobot) PickMove(state *GameState, moves []Move) Move {
if len(moves) > 0 {
// Sort by score and return the highest scoring move
sort.Sort(byScore{state, moves})
return moves[0]
}
// No valid tile moves
if !state.exchangeForbidden {
// Exchange all tiles, since that is allowed
return NewExchangeMove(state.Rack.AsString())
}
// Exchange forbidden: Return a pass move
return NewPassMove()
}
// NewHighScoreRobot returns a fresh instance of a HighestScoreRobot
func NewHighScoreRobot() *RobotWrapper {
return &RobotWrapper{&HighScoreRobot{}}
}
// PickMove for OneOfNBestRobot selects one of the N highest-scoring
// moves at random, or an exchange move, or a pass move as a last resort
func (robot *OneOfNBestRobot) PickMove(state *GameState, moves []Move) Move {
if len(moves) > 0 {
// Sort by score
sort.Sort(byScore{state, moves})
// Cut the list down to N, if it is longer than that
if len(moves) > robot.N {
moves = moves[:robot.N]
}
// Pick a move by random from the remaining list
pick := rand.Intn(len(moves))
return moves[pick]
}
// No valid tile moves
if !state.exchangeForbidden {
// Exchange all tiles, since that is allowed
return NewExchangeMove(state.Rack.AsString())
}
// Exchange forbidden: Return a pass move
return NewPassMove()
}
// NewOneOfNBestRobot returns a fresh instance of a OneOfNBestRobot
func NewOneOfNBestRobot(n int) *RobotWrapper {
return &RobotWrapper{&OneOfNBestRobot{N: n}}
}