-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtic_tac_toe.go
More file actions
53 lines (45 loc) · 924 Bytes
/
tic_tac_toe.go
File metadata and controls
53 lines (45 loc) · 924 Bytes
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
package ebitest
// TicTacToe is a synchronizer of 3 channesl
type TicTacToe struct {
tic chan struct{}
tac chan struct{}
toe chan struct{}
}
// NewTicTacToe returns a new TicTacToe
func NewTicTacToe() *TicTacToe {
return &TicTacToe{
tic: make(chan struct{}, 1),
tac: make(chan struct{}, 1),
toe: make(chan struct{}, 1),
}
}
// Tic first
func (ttt *TicTacToe) Tic() {
ttt.tic <- struct{}{}
}
// HasTic checks if Tic has content
func (ttt *TicTacToe) HasTic() bool {
return len(ttt.tic) != 0
}
// Tac second
func (ttt *TicTacToe) Tac() {
if ttt.HasTic() {
<-ttt.tic
ttt.tac <- struct{}{}
}
}
// HasTac checks if Tac has content
func (ttt *TicTacToe) HasTac() bool {
return len(ttt.tac) != 0
}
// Toe third
func (ttt *TicTacToe) Toe() {
if ttt.HasTac() {
<-ttt.tac
ttt.toe <- struct{}{}
}
}
// HasToe checks if Toe has content
func (ttt *TicTacToe) HasToe() bool {
return len(ttt.toe) != 0
}