-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtictactoe.go
215 lines (215 loc) · 5.36 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
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
package main
import
(
"strings"
"fmt"
)
func mapper(letter string)int{
if strings.TrimRight(letter, "\n") == "a" { return 0 }
if strings.TrimRight(letter, "\n") == "b" { return 1 }
if strings.TrimRight(letter, "\n") == "c" { return 2 }
if strings.TrimRight(letter, "\n") == "d" { return 3 }
if strings.TrimRight(letter, "\n") == "e" { return 4 }
if strings.TrimRight(letter, "\n") == "f" { return 5 }
if strings.TrimRight(letter, "\n") == "g" { return 6 }
if strings.TrimRight(letter, "\n") == "h" { return 7 }
if strings.TrimRight(letter, "\n") == "i" { return 8 }
return 99
}
func bprinter (board [9]string){
fmt.Println("The board now looks like this:")
fmt.Println("-------")
fmt.Println("|" + board[0] + "|" + board[1] + "|" + board[2] + "|")
fmt.Println("|" + board[3] + "|" + board[4] + "|" + board[5] + "|")
fmt.Println("|" + board[6] + "|" + board[7] + "|" + board[8] + "|")
fmt.Println("-------")
fmt.Println("")
}
func legendprinter (){
fmt.Println("Here is the legend for you to make a mark:")
fmt.Println("_______")
fmt.Println("|a|b|c|\n|d|e|f|\n|g|h|i|")
fmt.Println("_______")
}
func step1(board [9]string, player string) [9]string{
boardpos := requestor(player)
newboard := marker(board, player, boardpos)
tflag := islistsame(board, newboard)
if (tflag){
fmt.Println("No change was made. The same player must make a valid selection. Player " + player + " please try again.")
newboard = step1(board, player)
}
return newboard
}
func boardwinchecker(board [9]string) string{
lastmessage := "nowinner"
if board[0] == board[4] {
if board[4] == board[8] {
if board[4] != " " {
lastmessage = "Player " + board[0] + " wins!"
}
}
}
if board[2] == board[4] {
if board[4] == board[6] {
if board[6] != " " {
lastmessage = "Player " + board[2] + " wins!"
}
}
}
if board[0] == board[3] {
if board[3] == board[6] {
if board[6] != " " {
lastmessage = "Player " + board[0] + " wins!"
}
}
}
if board[0] == board[1] {
if board[1] == board[2] {
if board[0] != " " {
lastmessage = "Player " + board[0] + " wins!"
}
}
}
if board[2] == board[5] {
if board[5] == board[8] {
if board[2] != " " {
lastmessage = "Player " + board[2] + " wins!"
}
}
}
if board[6] == board[7] {
if board[7] == board[8] {
if board[8] != " " {
lastmessage = "Player " + board[6] + " wins!"
}
}
}
if board[1] == board[4] {
if board[4] == board[7] {
if board[1] != " " {
lastmessage = "Player " + board[1] + " wins!"
}
}
}
if board[3] == board[4] {
if board[4] == board[5] {
if board[3] != " " {
lastmessage = "Player " + board[3] + " wins!"
}
}
}
return lastmessage
}
func marker(board [9]string, player string, position string) [9]string{
x := mapper(position)
if (board[x] != " "){
fmt.Println("That square has already been marked. Please try again.")
}else{
board[x] = player
}
return board
}
func requestor(player string) string{
fmt.Println("Player", player, "it is your turn.")
fmt.Println("Please enter a letter for the square you want to mark (a through i):")
var text string
fmt.Scanln(&text)
if strings.TrimRight(text, "\n") != "a"{
if strings.TrimRight(text, "\n") != "b"{
if strings.TrimRight(text, "\n") != "c"{
if strings.TrimRight(text, "\n") != "d"{
if strings.TrimRight(text, "\n") != "e"{
if strings.TrimRight(text, "\n") != "f"{
if strings.TrimRight(text, "\n") != "g"{
if strings.TrimRight(text, "\n") != "h"{
if strings.TrimRight(text, "\n") != "i"{
fmt.Println("*** WARNING ***")
fmt.Println("That was an invalid entry. Please try again player " + player)
fmt.Println(" ")
text = requestor(player)
}
}
}
}
}
}
}
}
}
return text
}
func islistsame(board1 [9]string, board2 [9]string) bool{
var j = true
for i := 0; i < 9; i++{
if board1[i] == board2[i]{
}else{
j = false
}
}
return j
}
func main(){
var tttb [9]string
tttb[0] = " "
tttb[1] = " "
tttb[2] = " "
tttb[3] = " "
tttb[4] = " "
tttb[5] = " "
tttb[6] = " "
tttb[7] = " "
tttb[8] = " "
legendprinter()
a := step1(tttb, "X")
bprinter(a)
b := step1(a, "O")
bprinter(b)
c := step1(b, "X")
bprinter(c)
d := step1(c, "O")
bprinter(d)
e := step1(d, "X")
bprinter(e)
legendprinter()
be := boardwinchecker(e)
if be == "nowinner"{
f := step1(e, "O")
bprinter(f)
bf := boardwinchecker(f)
if bf == "nowinner"{
bprinter(f)
g := step1(f, "X")
bg := boardwinchecker(g)
if bg == "nowinner"{
bprinter(g)
h := step1(g, "O")
bh := boardwinchecker(h)
if bh == "nowinner"{
bprinter(h)
i := step1(h, "X")
bi := boardwinchecker(i)
if bi == "nowinner"{
legendprinter()
lastmessage := "CAT's GAME. (TIE GAME.)"
bprinter(i)
fmt.Println(lastmessage)
}else{
bprinter(i)
fmt.Println(bi)
}
}else{
bprinter(h)
fmt.Println(bh)
}}else{
bprinter(g)
fmt.Println(bg)
}}else{
bprinter(f)
fmt.Println(bf)
}
}else{
bprinter(e)
fmt.Println(be)
}
}