-
Notifications
You must be signed in to change notification settings - Fork 10
/
game.go
252 lines (195 loc) · 4.46 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
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
249
250
251
252
package main
import (
"bytes"
"encoding/gob"
"encoding/json"
"io/ioutil"
"strconv"
)
const EMPTY = "EMPTY"
type bottlesArray [][]string
type Game struct {
Bottles bottlesArray `json:"bottles"`
}
type Flow struct {
Bottles bottlesArray
Moves []string
}
func newFlow(bottles bottlesArray) Flow {
moves := make([]string, 0)
return Flow{
Moves: moves,
Bottles: bottles,
}
}
func (game Game) solve() Flow {
queueState := make([]Flow, 0)
queueState = append(queueState, newFlow(game.Bottles))
visited := make(map[string]bool)
var solvedState Flow
for len(queueState) != 0 {
currentState := queueState[len(queueState)-1]
queueState = queueState[:len(queueState)-1]
if visited[hashBottles(currentState.Bottles)] {
continue
}
visited[hashBottles(currentState.Bottles)] = true
if isDone(currentState.Bottles) {
solvedState = currentState
break
}
for i := 0; i < len(currentState.Bottles); i++ {
bottle1 := currentState.Bottles[i]
if isBottleDone(bottle1) || bottle1[0] == EMPTY {
continue
}
for j := 0; j < len(currentState.Bottles); j++ {
if i == j {
continue
}
bottle2 := currentState.Bottles[j]
if bottle2[len(bottle2)-1] != EMPTY {
continue
}
if isMovePossible(bottle1, bottle2) {
cs := getCopyOfSituation(currentState.Bottles)
makeMove(&cs, i, j)
flow := newFlow(getCopyOfSituation(cs))
newMovesArr := make([]string, len(currentState.Moves))
copy(newMovesArr, currentState.Moves)
flow.Moves = append(newMovesArr, strconv.Itoa(i)+" -> "+strconv.Itoa(j))
if !isAlreadySolved(queueState, cs) {
queueState = append(queueState, flow)
}
}
}
}
}
return solvedState
}
func isBottleDone(bottle []string) bool {
color := bottle[0]
for i := 1; i < len(bottle); i++ {
if color == EMPTY {
return false
}
if bottle[i] != color {
return false
}
}
return true
}
func getCopyOfSituation(situation bottlesArray) bottlesArray {
copySlice := make(bottlesArray, len(situation))
for index, val := range situation {
newArr := make([]string, len(val))
copy(newArr, val)
copySlice[index] = newArr
}
return copySlice
}
func isAlreadySolved(solutions []Flow, bottles bottlesArray) bool {
for _, val := range solutions {
s1 := hashBottles(val.Bottles)
s2 := hashBottles(bottles)
if s1 == s2 {
return true
}
}
return false
}
func hashBottles(bottles bottlesArray) string {
var b bytes.Buffer
gob.NewEncoder(&b).Encode(bottles)
return string(b.Bytes())
}
func makeMove(currentSituation *bottlesArray, i, j int) {
color, index := getTopColorOfBottle((*currentSituation)[i])
emptyIndex := getBottomEmptyPlace((*currentSituation)[j])
(*currentSituation)[i][index] = EMPTY
(*currentSituation)[j][emptyIndex] = color
if isMovePossible((*currentSituation)[i], (*currentSituation)[j]) {
makeMove(currentSituation, i, j)
}
}
func isMovePossible(bottle1, bottle2 []string) bool {
topColorBottle1, _ := getTopColorOfBottle(bottle1)
topColorBottle2, _ := getTopColorOfBottle(bottle2)
emptySpacesBottle2 := 0
for i := 0; i < len(bottle2); i++ {
if bottle2[i] == EMPTY {
emptySpacesBottle2++
}
}
liquidBottle1 := 0
for i := 0; i < len(bottle1); i++ {
if bottle1[i] == topColorBottle1 {
liquidBottle1++
}
}
if liquidBottle1 > emptySpacesBottle2 {
return false
}
if bottle2[len(bottle2)-1] != EMPTY {
return false
}
if topColorBottle1 == bottle1[0] && bottle2[0] == EMPTY {
return false
}
if topColorBottle1 != topColorBottle2 && topColorBottle2 != EMPTY {
return false
}
return true
}
func getBottomEmptyPlace(bottle []string) (index int) {
for i := range bottle {
if bottle[i] == EMPTY {
index = i
return
}
}
return
}
func getTopColorOfBottle(bottle []string) (color string, index int) {
color = EMPTY
for i := len(bottle) - 1; i >= 0; i-- {
if bottle[i] != EMPTY {
color = bottle[i]
index = i
return
}
}
return
}
func isDone(bottles bottlesArray) bool {
isDone := true
for _, bottle := range bottles {
var previousColor string
for _, color := range bottle {
if previousColor == "" {
previousColor = color
continue
}
if color != previousColor {
isDone = false
break
}
previousColor = color
}
if isDone == false {
break
}
}
return isDone
}
func NewGame(filename string) (game Game) {
byt, err := ioutil.ReadFile(filename)
if err != nil {
panic(err)
}
err = json.Unmarshal(byt, &game)
if err != nil {
panic(err)
}
return
}