-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
204 lines (163 loc) · 3.83 KB
/
main.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
package main
import (
"fmt"
"math/rand"
"time"
)
type Tile struct {
number int
open bool
}
type Move struct{
numbers []int
}
type Dice struct {
die []int
}
func main() {
tiles := SetupTiles()
gameOver := false
for !gameOver {
dice := Roll(2)
PrintBox(tiles, dice)
moves := AvailableMoves(tiles, dice)
moves = LegalMoves(tiles, moves)
if(len(moves) == 0) {
gameOver = true
break
}
PrintLegalMoves(moves)
choice := GetChoice()
CloseTiles(tiles, moves, choice)
}
DoGameOver(tiles)
}
func DoGameOver(tiles []*Tile) {
fmt.Println("")
fmt.Println(" ++-- GAME OVER --++")
fmt.Println("")
}
func GetChoice() int {
var selection int
fmt.Printf("Pick move >")
fmt.Scan(&selection)
return selection
}
func SetupTiles() []*Tile {
tiles := make([]*Tile, 0, 10)
for i := 1; i < 10; i++ {
tile := new(Tile)
tile.number = i
tile.open = true
tiles = append(tiles, tile)
}
return tiles
}
func AvailableMoves(tiles []*Tile, dice *Dice) []*Move {
total := Total(dice)
moves := Partition(total, nil)
move := new(Move)
move.numbers = []int{total}
moves = append(moves, move)
return moves
}
func Total(dice *Dice) int {
total := 0
for _,num := range dice.die {
total += num
}
return total
}
func Partition(num int, prepend []int) []*Move {
moves := make([]*Move, 0, 10)
start := 1
if prepend != nil {
start = prepend[len(prepend) - 1] + 1
}
for x := start; x <= ((num - 1)/ 2); x++ {
move := new(Move)
if prepend != nil {
move.numbers = append(move.numbers, prepend...)
}
move.numbers = append(move.numbers, x)
move.numbers = append(move.numbers, num - x)
moves = append(moves, move)
moves = append(moves, Partition(num - x, move.numbers[0:(len(move.numbers)-1)])...)
}
return moves
}
func LegalMoves(tiles []*Tile, moves []*Move) []*Move {
outMoves := make([]*Move, 0, 10)
for _, move := range moves {
if (LegalMove(tiles, move)) {
outMoves = append(outMoves, move)
}
}
return outMoves;
}
func LegalMove(tiles []*Tile, move *Move) bool {
for _, num := range move.numbers {
if (num > 9) {
return false;
} else if (!tiles[num-1].open) {
return false;
}
}
return true;
}
func PrintLegalMoves(moves []*Move) {
for idx, move := range moves {
PrintMove(idx, move)
}
}
func PrintMove(idx int, move *Move) {
fmt.Printf("%v> ", idx)
for _, num := range move.numbers {
fmt.Printf("%v ", num)
}
fmt.Printf("\n")
}
func Roll(howMany int) *Dice {
rand.Seed( time.Now().UTC().UnixNano())
dice := new(Dice)
dice.die = make([]int, 0, 5)
for x:=0; x<howMany; x++ {
dice.die = append(dice.die, int(rand.Int31n(6) + 1))
}
return dice
}
func CloseTiles(tiles []*Tile, moves []*Move, choice int) {
move := moves[choice]
for _, num := range move.numbers {
tiles[num-1].open = false;
}
}
func PrintBox(tiles []*Tile, dice *Dice) {
PrintTiles(tiles)
PrintRoll(dice)
}
func PrintRoll(dice *Dice) {
fmt.Print (" | |\n")
fmt.Print (" | ")
fmt.Printf (" ")
for _,die := range dice.die {
fmt.Printf ("[%v] ", die)
}
fmt.Print (" |\n")
fmt.Print (" | |\n")
fmt.Println(" +-------------------------------------+")
}
func PrintTiles(tiles []*Tile) {
fmt.Println(" +-------------------------------------+")
fmt.Print (" | ")
for i := 0; i < 9; i++ {
tile := tiles[i]
if (tile.open) {
fmt.Printf("[%v] ", tile.number)
} else {
fmt.Printf("[-] ")
}
}
fmt.Print ("|\n")
fmt.Println(" +-------------------------------------+")
}