-
Notifications
You must be signed in to change notification settings - Fork 1
/
game.go
171 lines (149 loc) · 3.22 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
package main
import (
"bytes"
"fmt"
"math/rand"
"github.com/jaysinco/Tools/core"
)
func NewGame() *State {
return &State{new([_row][_col]Symbol), nil}
}
type State struct {
chessBoard *[_row][_col]Symbol
lastMove *Pos
}
func (s *State) Nextp(move *Pos) {
if !s.IsValidMove(move) {
core.Fatal("invalid move: %v", move)
}
nowPlayer := s.LastPlayer().Flip()
s.chessBoard[move.Row][move.Col] = nowPlayer
s.lastMove = move
}
func (s *State) Next(row, col int) {
s.Nextp(&Pos{row, col})
}
func (s *State) NextRandom() {
s.Nextp(s.GetMoves()[0])
}
func (s *State) IsValidMove(move *Pos) bool {
r, c := move.Row, move.Col
return r < _row && r >= 0 && c < _col && c >= 0 && s.Get(r, c) == ZERO
}
func (s *State) Clone() *State {
cloned, board, last := new(State), new([_row][_col]Symbol), (*Pos)(nil)
*board = *(s.chessBoard)
if s.lastMove != nil {
last = new(Pos)
*last = *(s.lastMove)
}
cloned.chessBoard, cloned.lastMove = board, last
return cloned
}
func (s *State) Get(row, col int) Symbol {
return s.chessBoard[row][col]
}
func (s *State) Getp(p *Pos) Symbol {
return s.chessBoard[p.Row][p.Col]
}
func (s *State) LastPlayer() Symbol {
if s.lastMove == nil {
return TWO
}
return s.Getp(s.lastMove)
}
func (s *State) LastMove() *Pos {
return s.lastMove
}
func (s *State) GetMoves() (options []*Pos) {
for r := 0; r < _row; r++ {
for c := 0; c < _col; c++ {
if s.Get(r, c) == ZERO {
options = append(options, &Pos{r, c})
}
}
}
for i := 0; i < len(options); i++ {
a := rand.Intn(len(options))
b := rand.Intn(len(options))
options[a], options[b] = options[b], options[a]
}
return
}
func (s *State) Winner() (player Symbol, over bool) {
round := 0
for rx := 0; rx < _row; rx++ {
for cy := 0; cy < _col; cy++ {
sym := s.Get(rx, cy)
if sym != ZERO {
round++
for _, dir := range [4][2]int{{0, 1}, {1, 0}, {-1, 1}, {1, 1}} {
total, dr, dc := 0, dir[0], dir[1]
for _, m := range [2]int{1, -1} {
for r, c := rx, cy; r < _row && r >= 0 && c < _col && c >= 0 && s.Get(r, c) == sym; {
total++
r += dr * m
c += dc * m
}
}
if total-1 >= _suc {
return sym, true
}
}
}
}
}
if round >= _row*_col {
return ZERO, true
}
return ZERO, false
}
func (s *State) String() string {
var buf bytes.Buffer
for r := 0; r < _row; r++ {
buf.WriteString(fmt.Sprintf("%2d|", (r+1)%10))
for c := 0; c < _col; c++ {
buf.WriteString(s.Get(r, c).String() + "|")
}
buf.WriteRune('\n')
}
buf.WriteString(" ")
for c := 0; c < _col; c++ {
buf.WriteString(fmt.Sprintf("%-2d", (c+1)%10))
}
buf.WriteRune('\n')
buf.WriteString(fmt.Sprintf("last move: %s%v", s.LastPlayer(), s.LastMove()))
return buf.String()
}
type Pos struct {
Row int
Col int
}
func (p *Pos) String() string {
return fmt.Sprintf("(%02d, %02d)", p.Row+1, p.Col+1)
}
const (
ZERO Symbol = iota
ONE
TWO
)
type Symbol int8
func (s Symbol) Flip() Symbol {
if s == ZERO {
core.Fatal("can not flip non-player chess peice")
}
return Symbol(3 - s)
}
func (s Symbol) String() string {
switch s {
case ZERO:
return " "
case ONE:
return "●"
case TWO:
return "○"
default:
core.Fatal("impossible chess piece symbol: %v", s)
}
return "can not reach here"
}