-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgo.go
More file actions
234 lines (196 loc) · 4.33 KB
/
go.go
File metadata and controls
234 lines (196 loc) · 4.33 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
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
)
type Stone int
const (
Empty Stone = iota
Black
White
)
func (s Stone) String() string {
switch s {
case Black:
return "●"
case White:
return "○"
default:
return "+"
}
}
type Board struct {
size int
grid [][]Stone
turn Stone
passes int
}
func NewBoard(size int) *Board {
grid := make([][]Stone, size)
for i := range grid {
grid[i] = make([]Stone, size)
}
return &Board{
size: size,
grid: grid,
turn: Black,
}
}
func (b *Board) Display() {
fmt.Println()
// Column headers
fmt.Print(" ")
for i := 0; i < b.size; i++ {
fmt.Printf("%2d", i)
}
fmt.Println()
// Board with row headers
for i := 0; i < b.size; i++ {
fmt.Printf("%2d", i)
for j := 0; j < b.size; j++ {
fmt.Printf(" %s", b.grid[i][j])
}
fmt.Println()
}
fmt.Printf("\nCurrent turn: %s\n", b.turn)
}
func (b *Board) IsValidMove(row, col int) bool {
if row < 0 || row >= b.size || col < 0 || col >= b.size {
return false
}
return b.grid[row][col] == Empty
}
func (b *Board) PlaceStone(row, col int) bool {
if !b.IsValidMove(row, col) {
return false
}
b.grid[row][col] = b.turn
// Remove captured opponent stones
opponent := White
if b.turn == White {
opponent = Black
}
// Check all adjacent positions for captures
directions := [][2]int{{-1, 0}, {1, 0}, {0, -1}, {0, 1}}
for _, dir := range directions {
newRow, newCol := row+dir[0], col+dir[1]
if b.isInBounds(newRow, newCol) && b.grid[newRow][newCol] == opponent {
if !b.hasLiberties(newRow, newCol, make(map[[2]int]bool)) {
b.removeGroup(newRow, newCol)
}
}
}
// Check if the placed stone group has liberties (suicide rule)
if !b.hasLiberties(row, col, make(map[[2]int]bool)) {
b.grid[row][col] = Empty // Remove the stone
return false
}
b.passes = 0
b.nextTurn()
return true
}
func (b *Board) isInBounds(row, col int) bool {
return row >= 0 && row < b.size && col >= 0 && col < b.size
}
func (b *Board) hasLiberties(row, col int, visited map[[2]int]bool) bool {
if visited[[2]int{row, col}] {
return false
}
visited[[2]int{row, col}] = true
stone := b.grid[row][col]
directions := [][2]int{{-1, 0}, {1, 0}, {0, -1}, {0, 1}}
for _, dir := range directions {
newRow, newCol := row+dir[0], col+dir[1]
if !b.isInBounds(newRow, newCol) {
continue
}
if b.grid[newRow][newCol] == Empty {
return true // Found a liberty
}
if b.grid[newRow][newCol] == stone {
if b.hasLiberties(newRow, newCol, visited) {
return true
}
}
}
return false
}
func (b *Board) removeGroup(row, col int) {
stone := b.grid[row][col]
if stone == Empty {
return
}
b.grid[row][col] = Empty
directions := [][2]int{{-1, 0}, {1, 0}, {0, -1}, {0, 1}}
for _, dir := range directions {
newRow, newCol := row+dir[0], col+dir[1]
if b.isInBounds(newRow, newCol) && b.grid[newRow][newCol] == stone {
b.removeGroup(newRow, newCol)
}
}
}
func (b *Board) Pass() {
b.passes++
b.nextTurn()
}
func (b *Board) nextTurn() {
if b.turn == Black {
b.turn = White
} else {
b.turn = Black
}
}
func (b *Board) IsGameOver() bool {
return b.passes >= 2
}
func main() {
fmt.Println("Welcome to Go!")
fmt.Println("Enter moves as 'row col' (e.g., '3 4')")
fmt.Println("Enter 'pass' to pass your turn")
fmt.Println("Enter 'quit' to exit")
fmt.Println("Starting with 9x9 board...")
board := NewBoard(9)
scanner := bufio.NewScanner(os.Stdin)
for !board.IsGameOver() {
board.Display()
fmt.Printf("Enter move for %s: ", board.turn)
if !scanner.Scan() {
break
}
input := strings.TrimSpace(scanner.Text())
switch input {
case "quit":
fmt.Println("Thanks for playing!")
return
case "pass":
board.Pass()
fmt.Printf("%s passes\n", func() Stone {
if board.turn == Black {
return White
}
return Black
}())
default:
parts := strings.Fields(input)
if len(parts) != 2 {
fmt.Println("Invalid input. Use format: row col")
continue
}
row, err1 := strconv.Atoi(parts[0])
col, err2 := strconv.Atoi(parts[1])
if err1 != nil || err2 != nil {
fmt.Println("Invalid numbers. Use format: row col")
continue
}
if !board.PlaceStone(row, col) {
fmt.Println("Invalid move! Try again.")
}
}
}
board.Display()
fmt.Println("Game over! Both players passed.")
fmt.Println("Thanks for playing!")
}