-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.go
120 lines (102 loc) · 2.29 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
package main
import (
"net"
)
// Data structure to represent the current state of the game.
type game struct {
gameState int
stateFrame int
grid [globalNumTilesX][globalNumTilesY]int
p1Color int
p2Color int
turn int
tokenPosition int
result int
readyToStart bool
canPlay bool
serverConn net.Conn
startTimer int
startChan chan bool
chosenColor chan bool
p2Position chan int
p2ColorChan chan int
debug bool
}
// Constants to represent the current game sequence (title screen,
// color selection screen, game, results screen).
const (
titleState int = iota
colorSelectState
displayColorState
playState
resultState
)
// Constants to represent tokens in the Connect 4 grid
// (no token, player 1's token, player 2's token).
const (
noToken int = iota
p1Token
p2Token
)
// Constants to represent the turn of the game (player 1 or player 2).
const (
p1Turn int = iota
p2Turn
)
// Constants to represent the result of a game
// (draw if the grid is filled without a player winning,
// player 1 winner, or player 2 winner).
const (
equality int = iota
p1wins
p2wins
)
// Resets the game to start a new round.
// The player who lost the last game starts.
func (g *game) reset() {
for x := 0; x < globalNumTilesX; x++ {
for y := 0; y < globalNumTilesY; y++ {
g.grid[x][y] = noToken
}
}
}
// Checks if both players have pressed enter and are ready to choose their colors.
// If there is a value in the channel, use it; otherwise, return the previous one.
func (g *game) isReady() bool {
if g.readyToStart {
return true
}
select {
case <-g.startChan:
g.readyToStart = true
return true
default:
return false
}
}
// Checks if both players have chosen their colors.
// If there is a value in the channel, use it; otherwise, return the previous one.
func (g *game) hasChosenColor() bool {
if g.canPlay {
return true
}
select {
case <-g.chosenColor:
g.canPlay = true
return true
default:
return false
}
}
// Retrieves the color of Player 2.
// If there is a value in the channel, use it; otherwise, return the previous one.
func (g *game) getP2Color() int {
color := g.p2Color
select {
case color = <-g.p2ColorChan:
g.p2Color = color
default:
// No new value, do not make any changes.
}
return g.p2Color
}