/
tictactoe.go
177 lines (152 loc) · 4.73 KB
/
tictactoe.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
// Package tictactoe defines the game of tic-tac-toe.
package tictactoe
import (
"bytes"
"context"
"fmt"
"html/template"
)
// Player of tic-tac-toe.
type Player interface {
// Name of player.
Name() string
// Play takes a tic-tac-toe board b and returns the next move
// for this player. Its mark is either X or O.
// ctx is expected to have a deadline set, and Play may take time
// to "think" until deadline is reached before returning.
Play(ctx context.Context, b Board, mark State) (Move, error)
}
// Imager is an optional interface implemented by players
// that have an image that represents them.
type Imager interface {
// Image returns the URL of the player's image.
// Optimal size is 100 by 100 pixels (or higher for high DPI screens).
Image() template.URL
}
// CellClicker is an optional interface implemented by players
// that wish to be notified about cell clicks.
type CellClicker interface {
// CellClick is called when cell with
// specified index is clicked.
CellClick(index int)
}
// Move is the board cell index where to place one's mark, a value in range [0, 9).
//
// A move is valid if it's in the range [0, 9).
// A move is legal if it can be applied to a given board configuration.
type Move int
// Valid reports if the move is valid.
// A valid move may not be legal depending on the board configuration.
func (m Move) Valid() error {
ok := m >= 0 && m < 9
if !ok {
return fmt.Errorf("move %v is out of range [0, 9)", m)
}
return nil
}
// State of a board cell.
type State uint8
// States of a board cell.
const (
F State = iota // Free cell.
X // Cell with an X mark.
O // Cell with an O mark.
)
func (s State) String() string {
switch s {
case F:
return " "
case X:
return "X"
case O:
return "O"
default:
panic("unreachable")
}
}
// Condition of the board configuration.
type Condition uint8
// Conditions of the board configuration.
const (
NotEnd Condition = iota
XWon
OWon
Tie
)
func (c Condition) String() string {
switch c {
case NotEnd:
return "in progress"
case XWon:
return "player X won"
case OWon:
return "player O won"
case Tie:
return "tie"
default:
panic("unreachable")
}
}
// Board for tic-tac-toe.
type Board struct {
// Cells is a 3x3 matrix in row major order.
// Cells[3*r + c] is the cell in the r'th row and c'th column.
// Move m will affect Cells[m].
Cells [9]State
}
// Apply a move to this board. Mark is either X or O.
// If the move is not valid or not legal, the board is not modified and an error is returned.
func (b *Board) Apply(move Move, mark State) error {
if err := move.Valid(); err != nil {
return err
}
// Check if the move is legal for this board configuration.
if b.Cells[move] != F {
return fmt.Errorf("that cell is already occupied")
}
b.Cells[move] = mark
return nil
}
// Condition returns the condition of the board.
func (b Board) Condition() Condition {
var (
x = (b.Cells[0] == X && b.Cells[1] == X && b.Cells[2] == X) || // Check all rows.
(b.Cells[3] == X && b.Cells[4] == X && b.Cells[5] == X) ||
(b.Cells[6] == X && b.Cells[7] == X && b.Cells[8] == X) ||
(b.Cells[0] == X && b.Cells[3] == X && b.Cells[6] == X) || // Check all columns.
(b.Cells[1] == X && b.Cells[4] == X && b.Cells[7] == X) ||
(b.Cells[2] == X && b.Cells[5] == X && b.Cells[8] == X) ||
(b.Cells[0] == X && b.Cells[4] == X && b.Cells[8] == X) || // Check all diagonals.
(b.Cells[2] == X && b.Cells[4] == X && b.Cells[6] == X)
o = (b.Cells[0] == O && b.Cells[1] == O && b.Cells[2] == O) || // Check all rows.
(b.Cells[3] == O && b.Cells[4] == O && b.Cells[5] == O) ||
(b.Cells[6] == O && b.Cells[7] == O && b.Cells[8] == O) ||
(b.Cells[0] == O && b.Cells[3] == O && b.Cells[6] == O) || // Check all columns.
(b.Cells[1] == O && b.Cells[4] == O && b.Cells[7] == O) ||
(b.Cells[2] == O && b.Cells[5] == O && b.Cells[8] == O) ||
(b.Cells[0] == O && b.Cells[4] == O && b.Cells[8] == O) || // Check all diagonals.
(b.Cells[2] == O && b.Cells[4] == O && b.Cells[6] == O)
freeCellsLeft = b.Cells[0] == F || b.Cells[1] == F || b.Cells[2] == F ||
b.Cells[3] == F || b.Cells[4] == F || b.Cells[5] == F ||
b.Cells[6] == F || b.Cells[7] == F || b.Cells[8] == F
)
switch {
case x && !o:
return XWon
case o && !x:
return OWon
case !freeCellsLeft:
return Tie
default:
return NotEnd
}
}
func (b Board) String() string {
var buf bytes.Buffer
fmt.Fprintf(&buf, " %v │ %v │ %v \n", b.Cells[0], b.Cells[1], b.Cells[2])
fmt.Fprintln(&buf, "───┼───┼───")
fmt.Fprintf(&buf, " %v │ %v │ %v \n", b.Cells[3], b.Cells[4], b.Cells[5])
fmt.Fprintln(&buf, "───┼───┼───")
fmt.Fprintf(&buf, " %v │ %v │ %v ", b.Cells[6], b.Cells[7], b.Cells[8])
return buf.String()
}