-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenvironment.go
More file actions
248 lines (232 loc) · 5.19 KB
/
environment.go
File metadata and controls
248 lines (232 loc) · 5.19 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package main
import (
"fmt"
"math"
)
type location [2]int
type board [][]string
type environment struct {
board board
winner string
gameOver bool
}
// report the summary of the episode
func (env *environment) summarizeEpisode(p1, p2 *player) {
printBoard(&env.board, true)
if env.gameOver {
fmt.Print("Game Over - ")
}
if env.winner != "" { // there's a winner
if env.winner == p1.symbol {
fmt.Printf("%v is the winner \n\n", p1.name)
} else {
fmt.Printf("%v is the winner \n\n", p2.name)
}
} else {
fmt.Print("draw \n\n")
}
return
}
// initialize environment
func (env *environment) initializeEnvironment() {
board := make(board, boardSize)
for irow := range board {
board[irow] = make([]string, boardSize)
}
env.board = board
env.winner = ""
env.gameOver = false
return
}
// encode the game board into an integer (state id)
// NOTE: For each player, each location's status is viewed only as occupied either by him/herself or
// by the opponent, regardless of the actual symbol ("x" or "o") there.
// NOTE: For the same board and the same user, the player plays next or the opponent plays next makes
// different states.
func boardToState(b *board, symbol string) int64 {
var k, h, v, r int64
// encode board
for _, row := range *b {
for _, element := range row {
if element == symbol { // occupied by current player
v = 0
} else if element == "" { // empty
v = 1
} else { // occupied by opponent
v = 2
}
h += int64(math.Pow(3, float64(k))) * v
k++
}
}
// encode player symbol
if symbol == "x" {
r = 0
} else {
r = 1
}
h += int64(math.Pow(3, float64(k))) * r
return h
}
// decode the state id to reconstruct the board in player's perspective
func stateToBoard(h int64) (board, string) {
var symbol string
k := boardSize * boardSize
// decode player symbol
base := int64(math.Pow(3, float64(k)))
v := h / base
if v == 0 {
symbol = "x"
} else {
symbol = "o"
}
h -= v * base
k--
// decode board
b := make(board, boardSize)
for irow := boardSize - 1; irow >= 0; irow-- {
r := make([]string, boardSize)
for ielement := boardSize - 1; ielement >= 0; ielement-- {
base = int64(math.Pow(3, float64(k)))
v = h / base
if v == 0 {
r[ielement] = "P" // the player
} else if v == 1 {
r[ielement] = "" // empty
} else {
r[ielement] = "-" // the opponent
}
h -= v * base
k--
}
b[irow] = r
}
return b, symbol
}
// examine the board following a move and updates the winner and the game-over
func (env *environment) updateGameStatus(loc location, symbol string) {
// add new move on the board
env.board[loc[0]][loc[1]] = symbol
// update status
env.winner = getWinner(env.board)
if env.winner != "" || getEmpties(env.board) == 0 {
env.gameOver = true
} else {
env.gameOver = false
}
return
}
// pad symbol of a location to prepare for printing
func padSymbol(s string) string {
if len(s) == 0 {
s = " "
} else if len(s) == 1 {
s = " " + s + " "
} else if len(s) == 2 {
s = " " + s + " "
} else if len(s) == 3 {
s = " " + s + " "
} else if len(s) == 4 {
s = " " + s
}
s += "|"
return s
}
// print the board with players on it
func printBoard(b *board, toScreen bool) string {
var content string
for _, row := range *b {
content += "------------------- \n"
//fmt.Println("-------------------")
rowPrint := "|"
for _, element := range row {
rowPrint += padSymbol(element)
}
content += rowPrint
content += "\n"
//fmt.Println(rowPrint)
}
content += "------------------- \n"
//fmt.Println("-------------------")
if toScreen {
fmt.Print(content)
}
return content
}
// check whether all elements in a string array are equal to a certain string
func rowFilled(array []string, s string) bool {
for _, element := range array {
if element != s {
return false
}
}
return true
}
// check the current board and find the winner
func getWinner(b board) string {
symbols := [2]string{"x", "o"} // player symbols on the board
// rows
for _, row := range b {
for _, p := range symbols {
if rowFilled(row, p) {
return p
}
}
}
// columns
for icol := range b[0] {
// collection is an array composed by elements of this column
collection := []string{}
for irow := range b {
collection = append(collection, b[irow][icol])
}
for _, p := range symbols {
if rowFilled(collection, p) {
return p
}
}
}
// top-left to bottom-right
var targetArray []string
for i := range b {
targetArray = append(targetArray, b[i][i])
}
for _, p := range symbols {
if rowFilled(targetArray, p) {
return p
}
}
// top-right to bottom-left
targetArray = []string{}
for i := range b {
targetArray = append(targetArray, b[i][boardSize-1-i])
}
for _, p := range symbols {
if rowFilled(targetArray, p) {
return p
}
}
// no winner found
return ""
}
// check number of empty spots
func getEmpties(b board) int {
n := 0
for _, row := range b {
for _, element := range row {
if element == "" {
n++
}
}
}
return n
}
// get reward for a certain player by knowing the winner
func getReward(w, s string) float64 {
if w == s { // this player wins
return winReward
} else if w == "" { // draw game
return drawReward
}
return loseReward
}