-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.go
257 lines (214 loc) · 6.02 KB
/
cli.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
253
254
255
256
257
package main
import (
"bufio"
"fmt"
"os"
"regexp"
"strconv"
"strings"
"github.com/fatih/color"
)
type colorPair struct {
fg color.Attribute
bg color.Attribute
}
var colors map[string]colorPair = map[string]colorPair{
"orange": colorPair{color.FgHiRed, color.BgHiRed},
"blue": colorPair{color.FgCyan, color.BgCyan},
"purple": colorPair{color.FgBlue, color.BgBlue},
"pink": colorPair{color.FgHiMagenta, color.BgHiMagenta},
"yellow": colorPair{color.FgHiYellow, color.BgHiYellow},
"red": colorPair{color.FgRed, color.BgRed},
"green": colorPair{color.FgGreen, color.BgGreen},
"brown": colorPair{color.FgHiBlack, color.BgHiBlack},
}
func (state *state) printBoard() {
board := state.board
fmt.Println()
fmt.Print(" ")
for _, c := range "abcdefgh" {
fmt.Print(fmt.Sprintf("%c ", c))
}
fmt.Println()
for i := 0; i < N; i++ {
for k := 0; k < 3; k++ {
if k == 1 {
fmt.Print(fmt.Sprintf(" %d", N-i))
} else {
fmt.Print(" ")
}
for j := 0; j < N; j++ {
cp := colors[boardColors[i][j]]
color.Set(cp.bg)
fmt.Print(" ")
piece := board[i][j]
if k == 1 && piece != nil {
color.Unset()
color.Set(colors[piece.Color].fg)
if piece.Player == humanPlayer {
fmt.Print("X")
} else {
fmt.Print("O")
}
} else {
fmt.Print(" ")
}
color.Set(cp.bg)
fmt.Print(" ")
}
color.Unset()
if k == 1 {
fmt.Print(N - i)
}
fmt.Println()
}
}
fmt.Print(" ")
for _, c := range "abcdefgh" {
fmt.Print(fmt.Sprintf("%c ", c))
}
fmt.Println()
fmt.Println()
}
func main() {
if len(os.Args) != 2 {
fmt.Println("usage: kamisado-go <depth>")
os.Exit(0)
}
depth, ok := strconv.Atoi(os.Args[1])
if ok != nil {
fmt.Println("usage: kamisado-go <depth>")
os.Exit(0)
}
board := [8][8]*piece{}
playerPieceCoords := [2]map[string]coord{}
// Note that the 16 Piece objects created here will be the only
// ones used throughout the entire program.
for player, i := range []int{7, 0} {
playerPieceCoords[player] = make(map[string]coord)
for j := 0; j < 8; j++ {
color := boardColors[i][j]
board[i][j] = &piece{player, color}
playerPieceCoords[player][color] = coord{i, j}
}
}
state := state{board, playerPieceCoords}
// boardJ, _ := json.Marshal(board)
// fmt.Println(string(boardJ))
// os.Exit(0)
/////////////////////////////////////////////////////
// state.MovePiece(0, "brown", coord{5, 0})
// state.MovePiece(1, "blue", coord{6, 1})
// state.MovePiece(0, "brown", coord{3, 2})
// //state.PrintBoard()
// //fmt.Println(state.GetPossibleMoveCoords(1, "blue"))
// fmt.Println(state.FindBestMoveCoord(1, "blue", depth))
// os.Exit(0)
/////////////////////////////////////////////////////
fmt.Println("Welcome to Kamisado! You play White (X)")
state.printBoard()
var humanSrcCoord, humanDstCoord, cpuDstCoord coord
var humanSrcColor, cpuSrcColor string
var text string
var a [][]string
var reader *bufio.Reader
var r *regexp.Regexp
var err error
for {
reader = bufio.NewReader(os.Stdin)
fmt.Print("Enter your move (e.g. a1 d4): ")
text, _ = reader.ReadString('\n')
text = strings.ToLower(text)
if strings.HasPrefix(text, "q") {
os.Exit(0)
}
r, _ = regexp.Compile(`.*([a-h]).*([1-8]).*([a-h]).*([1-8]).*`)
a = r.FindAllStringSubmatch(text, -1)
if len(a) == 0 {
fmt.Println("Invalid move!")
continue
}
humanSrcCoord, err = toCoord(a[0][1:3])
if err != nil {
fmt.Println("Invalid move!")
continue
}
humanSrcColor = boardColors[humanSrcCoord.i][humanSrcCoord.j]
humanDstCoord, err = toCoord(a[0][3:])
if err != nil {
fmt.Println("Invalid move!")
continue
}
if !state.isLegalMove(humanPlayer, humanSrcColor, humanDstCoord) {
fmt.Println("Illegal move!")
continue
}
break
}
state.movePiece(humanPlayer, humanSrcColor, humanDstCoord)
cpuSrcColor = boardColors[humanDstCoord.i][humanDstCoord.j]
for {
// --> cpuSrcColor must be defined at this point
cpuDstCoord = state.findBestMoveCoord(cpuPlayer, cpuSrcColor, depth)
state.movePiece(cpuPlayer, cpuSrcColor, cpuDstCoord)
if state.isWinning(cpuPlayer) {
state.printBoard()
fmt.Println("CPU won")
os.Exit(0)
}
state.printBoard()
humanSrcColor = boardColors[cpuDstCoord.i][cpuDstCoord.j]
fmt.Println("CPU has played on " + humanSrcColor)
// Replay right away is human is blocked
if state.isBlocked(humanPlayer, humanSrcColor) {
fmt.Println("You are blocked, CPU will play again")
// This can be considered as a zero-length move by the human
// player, so the CPU src color will be the color of the cell
// on which the target human piece is on.
humanDstCoord = state.playerPieceCoords[humanPlayer][humanSrcColor]
cpuSrcColor = boardColors[humanDstCoord.i][humanDstCoord.j]
continue
}
for {
// --> humanSrcColor must be defined at this point
fmt.Print("Enter your move (e.g. d3): ")
text, _ = reader.ReadString('\n')
text = strings.ToLower(text)
if strings.HasPrefix(text, "q") {
os.Exit(0)
}
r, _ = regexp.Compile(`.*([a-h]).*([1-8]).*`)
a = r.FindAllStringSubmatch(text, -1)
if len(a) == 0 {
fmt.Println("Invalid move!")
continue
}
humanDstCoord, err = toCoord(a[0][1:])
if err != nil {
fmt.Println("Invalid move!")
continue
}
if !state.isLegalMove(humanPlayer, humanSrcColor, humanDstCoord) {
fmt.Println("Illegal move!")
continue
}
state.movePiece(humanPlayer, humanSrcColor, humanDstCoord)
cpuSrcColor = boardColors[humanDstCoord.i][humanDstCoord.j]
if state.isBlocked(cpuPlayer, cpuSrcColor) {
fmt.Println("CPU is blocked, you get to play again")
// This can be considered as a zero-length move by the CPU player,
// so the human src color will be the color of the cell on which
// the target CPU piece is on.
cpuDstCoord = state.playerPieceCoords[1][cpuSrcColor]
humanSrcColor = boardColors[cpuDstCoord.i][cpuDstCoord.j]
continue
}
break // let CPU play
}
if state.isWinning(humanPlayer) {
state.printBoard()
fmt.Println("You won!")
os.Exit(0)
}
}
}