Repository navigation
Expand file tree
/
Copy pathtictactoe.go
More file actions
283 lines (267 loc) · 8.54 KB
/
Copy pathtictactoe.go
File metadata and controls
283 lines (267 loc) · 8.54 KB
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
280
281
282
283
package main
import (
"fmt"
"math/rand"
"strings"
dg "github.com/bwmarrin/discordgo"
)
func inGame(player string, channelID string) bool {
return getGame(player, channelID) != ""
}
func getGame(player string, channelID string) string {
for k, v := range mem {
params := strings.Split(k, ",")
if params[0] == "game" && params[3] == channelID && (params[1] == player || params[2] == player) {
return v
}
}
return ""
}
func setGame(player string, channelID string, game string) {
for k := range mem {
params := strings.Split(k, ",")
if params[0] == "game" && params[3] == channelID && (params[1] == player || params[2] == player) {
mem[k] = game
return
}
}
fmt.Println("Error setting game")
}
func getOpponent(player string, channelID string) string {
for k := range mem {
params := strings.Split(k, ",")
if params[0] == "game" && params[3] == channelID {
if params[1] == player {
return params[2]
} else if params[2] == player {
return params[1]
}
}
}
return ""
}
func sendBoard(s *dg.Session, m *dg.MessageCreate, board string) {
rows := strings.Split(board, ",")
if len(rows) == 5 {
out := "Current board:\n```"
for i := 0; i < 3; i++ {
row := rows[i]
for j := 0; j < 3; j++ {
pos := row[j]
out += string(pos)
if j < 2 {
out += "|"
}
}
if i < 2 {
out += "\n-+-+-\n"
}
}
out += "```"
if player, err := s.User(rows[3]); err == nil {
out += player.Username + "'s turn"
} else {
fmt.Println("Couldn't find player ID")
fmt.Println(err)
}
s.ChannelMessageSend(m.ChannelID, out)
} else {
fmt.Println("Board format incorrect")
}
}
func checkWin(s *dg.Session, m *dg.MessageCreate, board string) bool {
player, err := s.User(m.Author.ID)
if err == nil {
rows := strings.Split(board, ",")
for i := 0; i < 3; i++ {
row := rows[i]
if row == "XXX" || row == "OOO" {
if opponent := getOpponent(m.Author.ID, m.ChannelID); opponent != "" {
s.ChannelMessageSend(m.ChannelID, "Congratulations "+player.Username+" wins!")
delete(mem, "game,"+m.Author.ID+","+opponent+","+m.ChannelID)
delete(mem, "game,"+opponent+","+m.Author.ID+","+m.ChannelID)
} else {
fmt.Println("Game won but no opponent found")
}
return true
}
}
for i := 0; i < 3; i++ {
if rows[0][i] == rows[1][i] && rows[1][i] == rows[2][i] && string(rows[0][i]) != " " {
if opponent := getOpponent(m.Author.ID, m.ChannelID); opponent != "" {
s.ChannelMessageSend(m.ChannelID, "Congratulations "+player.Username+" wins!")
delete(mem, "game,"+m.Author.ID+","+opponent+","+m.ChannelID)
delete(mem, "game,"+opponent+","+m.Author.ID+","+m.ChannelID)
} else {
fmt.Println("Game won but no opponent found")
}
return true
}
}
if (rows[0][0] == rows[1][1] && rows[1][1] == rows[2][2]) || (rows[0][2] == rows[1][1] && rows[1][1] == rows[2][0]) && string(rows[1][1]) != " " {
if opponent := getOpponent(m.Author.ID, m.ChannelID); opponent != "" {
s.ChannelMessageSend(m.ChannelID, "Congratulations "+player.Username+" wins!")
delete(mem, "game,"+m.Author.ID+","+opponent+","+m.ChannelID)
delete(mem, "game,"+opponent+","+m.Author.ID+","+m.ChannelID)
} else {
fmt.Println("Game won but no opponent found")
}
return true
}
for x := 0; x < 3; x++ {
for y := 0; y < 3; y++ {
if string(rows[x][y]) == " " {
return false
}
}
}
if opponent := getOpponent(m.Author.ID, m.ChannelID); opponent != "" {
s.ChannelMessageSend(m.ChannelID, "It's a draw")
delete(mem, "game,"+m.Author.ID+","+opponent+","+m.ChannelID)
delete(mem, "game,"+opponent+","+m.Author.ID+","+m.ChannelID)
} else {
fmt.Println("Game drawn but no opponent found")
}
return true
}
fmt.Println("Couldn't find player ID")
fmt.Println(err)
return false
}
func tictactoe(s *dg.Session, m *dg.MessageCreate, args []string) {
out := "To use tictactoe use one of the following subCommands:\n"
for k := range newMessageCommands["tictactoe"].subCommands {
out += k + "\n"
}
s.ChannelMessageSend(m.ChannelID, out)
}
func start(s *dg.Session, m *dg.MessageCreate, args []string) {
if len(args) > 0 {
if !inGame(m.Author.ID, m.ChannelID) {
opponent := idFromTag(args[0])
if opponent != m.Author.ID {
if _, ok := mem["request,"+m.Author.ID+","+m.ChannelID]; !ok {
mem["request,"+m.Author.ID+","+m.ChannelID] = opponent
s.ChannelMessageSend(m.ChannelID, "Request sent\nOpponent must accept by doing $tictactoe accept <@"+m.Author.ID+">")
} else {
s.ChannelMessageSend(m.ChannelID, "Cannot make multiple requests on the same channel")
}
} else {
s.ChannelMessageSend(m.ChannelID, "Cannot send request to yourself")
}
} else {
s.ChannelMessageSend(m.ChannelID, "Cannot start a game while you are already in a game")
}
} else {
s.ChannelMessageSend(m.ChannelID, "Please specify a person to start a game with")
}
}
func cancelRequest(s *dg.Session, m *dg.MessageCreate, args []string) {
if _, ok := mem["request,"+m.Author.ID+","+m.ChannelID]; ok {
delete(mem, "request,"+m.Author.ID+","+m.ChannelID)
s.ChannelMessageSend(m.ChannelID, "Request canceled")
}
}
func accept(s *dg.Session, m *dg.MessageCreate, args []string) {
if len(args) > 0 {
if !inGame(m.Author.ID, m.ChannelID) {
opponent := idFromTag(args[0])
if v, ok := mem["request,"+opponent+","+m.ChannelID]; ok {
if v == m.Author.ID {
delete(mem, "request,"+opponent+","+m.ChannelID)
start := rand.Intn(2)
emptyBoard := " , , ,"
if start == 0 {
emptyBoard += m.Author.ID + ",O"
} else {
emptyBoard += opponent + ",X"
}
mem["game,"+m.Author.ID+","+opponent+","+m.ChannelID] = emptyBoard
s.ChannelMessageSend(m.ChannelID, "Game started")
if op, err := s.User(opponent); err == nil {
if us, err1 := s.User(m.Author.ID); err1 == nil {
s.ChannelMessageSend(m.ChannelID, op.Username+" is X's and "+us.Username+" is O's")
sendBoard(s, m, emptyBoard)
} else {
fmt.Println("Couldn't find player ID")
fmt.Println(err1)
}
} else {
fmt.Println("Couldn't find player ID")
fmt.Println(err)
}
} else {
s.ChannelMessageSend(m.ChannelID, "That person has not sent you a request")
}
} else {
s.ChannelMessageSend(m.ChannelID, "That person has not sent you a request")
}
} else {
s.ChannelMessageSend(m.ChannelID, "Cannot accept a game while you are already in a game")
}
} else {
s.ChannelMessageSend(m.ChannelID, "Please specify whose request to accept")
}
}
func concede(s *dg.Session, m *dg.MessageCreate, args []string) {
if opponent := getOpponent(m.Author.ID, m.ChannelID); opponent != "" {
if player, err := s.User(opponent); err == nil {
s.ChannelMessageSend(m.ChannelID, "Congratulations "+player.Username+" wins!")
delete(mem, "game,"+m.Author.ID+","+opponent+","+m.ChannelID)
delete(mem, "game,"+opponent+","+m.Author.ID+","+m.ChannelID)
} else {
fmt.Println("Player conceded but opponent's ID not found")
fmt.Println(err)
}
} else {
s.ChannelMessageSend(m.ChannelID, "Cannot concede when not in a game")
}
}
var rows = map[string]int{
"top": 0,
"middle": 1,
"bottom": 2,
}
var cols = map[string]int{
"left": 0,
"middle": 1,
"right": 2,
}
func playttt(s *dg.Session, m *dg.MessageCreate, args []string) {
if len(args) >= 2 {
if game := getGame(m.Author.ID, m.ChannelID); game != "" {
if params := strings.Split(game, ","); params[3] == m.Author.ID {
if row, ok := rows[strings.ToLower(args[0])]; ok {
if col, ok := cols[strings.ToLower(args[1])]; ok {
if string(params[row][col]) == " " {
params[row] = params[row][:col] + params[4] + params[row][col+1:]
params[3] = getOpponent(m.Author.ID, m.ChannelID)
if params[4] == "X" {
params[4] = "O"
} else {
params[4] = "X"
}
game = strings.Join(params, ",")
if !checkWin(s, m, game) {
sendBoard(s, m, game)
setGame(m.Author.ID, m.ChannelID, game)
}
} else {
s.ChannelMessageSend(m.ChannelID, "That space is not empty")
}
} else {
s.ChannelMessageSend(m.ChannelID, "Usage:\n$tictactoe play [top|middle|bottom] [left|middle|right]")
}
} else {
s.ChannelMessageSend(m.ChannelID, "Usage:\n$tictactoe play [top|middle|bottom] [left|middle|right]")
}
} else {
s.ChannelMessageSend(m.ChannelID, "It is not your turn")
}
} else {
s.ChannelMessageSend(m.ChannelID, "You are not in a game")
}
} else {
s.ChannelMessageSend(m.ChannelID, "Usage:\n$tictactoe play [top|middle|bottom] [left|middle|right]")
}
}