-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReversi.go
279 lines (240 loc) · 6.32 KB
/
Reversi.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
)
// Constants
const (
BoardSize = 8
EmptyCell = " . "
PlayerBlack = " ● "
PlayerWhite = " ○ "
)
// Game represents the state of the game.
type Game struct {
board [][]string
currentTurn string
}
// NewGame initializes a new game.
func NewGame() *Game {
board := make([][]string, BoardSize)
for i := range board {
board[i] = make([]string, BoardSize)
for j := range board[i] {
board[i][j] = EmptyCell
}
}
// Starting pieces
board[3][3] = PlayerWhite
board[3][4] = PlayerBlack
board[4][3] = PlayerBlack
board[4][4] = PlayerWhite
return &Game{
board: board,
currentTurn: PlayerBlack,
}
}
// PrintBoard prints the current state of the game board.
func (g *Game) PrintBoard() {
fmt.Println(" A B C D E F G H")
for i, row := range g.board {
fmt.Printf("%d ", i+1)
for _, cell := range row {
fmt.Printf("%s", cell)
}
fmt.Println()
}
}
// PlayMove plays a move on the game board.
func (g *Game) PlayMove(row, col int) bool {
if g.isValidMove(row, col) {
g.board[row][col] = g.currentTurn
g.flipPieces(row, col)
// Check if the game is over after the current move
if g.isGameOver() {
return true
}
// If the other player doesn't have a valid move, the current player continues the turn
if !g.hasValidMove(g.getOtherPlayer()) {
fmt.Printf("%s cannot make a valid move. %s continues the turn.\n", g.getOtherPlayer(), g.currentTurn)
return true
}
// Switch the turn to the other player
g.switchTurn()
return true
}
return false
}
// isValidMove checks if a move is valid.
func (g *Game) isValidMove(row, col int) bool {
if row < 0 || row >= BoardSize || col < 0 || col >= BoardSize || g.board[row][col] != EmptyCell {
return false
}
// Check if at least one piece can be flipped in any direction
for dr := -1; dr <= 1; dr++ {
for dc := -1; dc <= 1; dc++ {
if dr == 0 && dc == 0 {
continue
}
if g.checkDirection(row, col, dr, dc) {
return true
}
}
}
return false
}
// checkDirection checks if there are pieces to flip in a specific direction.
func (g *Game) checkDirection(row, col, dr, dc int) bool {
otherPlayer := g.getOtherPlayer()
r, c := row+dr, col+dc
// Check if the first piece in the direction is the other player's piece
if r >= 0 && r < BoardSize && c >= 0 && c < BoardSize && g.board[r][c] == otherPlayer {
// Keep moving in the direction until an empty cell or the current player's piece is found
for r >= 0 && r < BoardSize && c >= 0 && c < BoardSize {
if g.board[r][c] == EmptyCell {
return false
} else if g.board[r][c] == g.currentTurn {
return true
}
r += dr
c += dc
}
}
return false
}
// flipPieces flips the opponent's pieces in all valid directions after playing a move.
func (g *Game) flipPieces(row, col int) {
for dr := -1; dr <= 1; dr++ {
for dc := -1; dc <= 1; dc++ {
if dr == 0 && dc == 0 {
continue
}
if g.checkDirection(row, col, dr, dc) {
g.flipDirection(row, col, dr, dc)
}
}
}
}
// flipDirection flips the opponent's pieces in a specific direction.
func (g *Game) flipDirection(row, col, dr, dc int) {
r, c := row+dr, col+dc
for r >= 0 && r < BoardSize && c >= 0 && c < BoardSize {
if g.board[r][c] == g.currentTurn {
return
}
g.board[r][c] = g.currentTurn
r += dr
c += dc
}
}
// switchTurn switches the current turn to the other player.
func (g *Game) switchTurn() {
if g.currentTurn == PlayerBlack {
g.currentTurn = PlayerWhite
} else {
g.currentTurn = PlayerBlack
}
}
// getOtherPlayer returns the player symbol of the other player.
func (g *Game) getOtherPlayer() string {
if g.currentTurn == PlayerBlack {
return PlayerWhite
}
return PlayerBlack
}
func (g *Game) isGameOver() bool {
return !g.hasValidMove(PlayerBlack) && !g.hasValidMove(PlayerWhite)
}
// hasValidMove checks if the current player has a valid move.
func (g *Game) hasValidMove(player string) bool {
for row := 0; row < BoardSize; row++ {
for col := 0; col < BoardSize; col++ {
if g.isValidMove(row, col) && g.board[row][col] == EmptyCell {
return true
}
}
}
return false
}
// getWinner returns the winner of the game.
func (g *Game) getWinner() string {
blackCount, whiteCount := 0, 0
for row := 0; row < BoardSize; row++ {
for col := 0; col < BoardSize; col++ {
if g.board[row][col] == PlayerBlack {
blackCount++
} else if g.board[row][col] == PlayerWhite {
whiteCount++
}
}
}
if blackCount > whiteCount {
return PlayerBlack
} else if whiteCount > blackCount {
return PlayerWhite
} else {
return "Draw"
}
}
func parseInput(input string) (int, int, error) {
parts := strings.Fields(input)
if len(parts) != 2 {
return 0, 0, fmt.Errorf("invalid input format")
}
row, err := strconv.Atoi(parts[0])
if err != nil || row < 1 || row > 8 {
return 0, 0, fmt.Errorf("invalid row value")
}
col := int(parts[1][0] - 'A')
if col < 0 || col >= BoardSize {
return 0, 0, fmt.Errorf("invalid column value")
}
return row, col, nil
}
// getPieceCounts returns the counts of black and white pieces on the board.
func (g *Game) getPieceCounts() (int, int) {
blackCount, whiteCount := 0, 0
for row := 0; row < BoardSize; row++ {
for col := 0; col < BoardSize; col++ {
if g.board[row][col] == PlayerBlack {
blackCount++
} else if g.board[row][col] == PlayerWhite {
whiteCount++
}
}
}
return blackCount, whiteCount
}
func main() {
game := NewGame()
scanner := bufio.NewScanner(os.Stdin)
for !game.isGameOver() {
game.PrintBoard()
blackCount, whiteCount := game.getPieceCounts()
fmt.Printf("Current Turn: %s\n", game.currentTurn)
fmt.Printf("Black Pieces: %d\n", blackCount)
fmt.Printf("White Pieces: %d\n", whiteCount)
fmt.Print("Enter row (1-8) and column (A-H) separated by space: ")
if scanner.Scan() {
input := scanner.Text()
row, col, err := parseInput(input)
if err != nil {
fmt.Println("Invalid input. Please try again.")
continue
}
if game.PlayMove(row-1, col) {
// The player switch is now handled within the PlayMove method.
} else {
fmt.Println("Invalid move. Please try again.")
}
}
}
game.PrintBoard()
blackCount, whiteCount := game.getPieceCounts()
fmt.Printf("Game Over! Winner: %s\n", game.getWinner())
fmt.Printf("Black Pieces: %d\n", blackCount)
fmt.Printf("White Pieces: %d\n", whiteCount)
}