forked from sts10/tic-tac-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.go
122 lines (108 loc) · 2.43 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
121
122
package main
import ("fmt")
func main(){
gameOver := false
board := [9]int{0,0,0,0,0,0,0,0,0}
turnNumber := 1
for gameOver != true{
presentBoard(board)
player := 0
if turnNumber % 2 == 1{
fmt.Println("Player 1's turn")
player = 1
} else {
fmt.Println("Player 2's turn")
player = 2
}
currentMove := askForPlay()
// Quit with '9'
if currentMove == 9 {
return
}
board = executePlayerMove(currentMove, player, board)
result := checkForWin(board)
if result > 0{
fmt.Printf("Player %d wins!\n\n", result)
gameOver = true
} else if turnNumber == 9 {
// Tie game example: 0 2 1 3 4 7 5 8 6
fmt.Printf("Tie game!\n\n")
gameOver = true
} else {
turnNumber++
}
}
}
func askForPlay() int{
fmt.Println("Select a move")
var moveInt int
fmt.Scan(&moveInt)
// fmt.Println("moveInt is", moveInt)
return moveInt
}
func executePlayerMove(moveInt int, player int, b [9]int) [9]int {
// Check for occupied spaces
if b[moveInt] != 0 {
fmt.Println("Please pick an unoccupied space.")
moveInt = askForPlay()
b = executePlayerMove(moveInt, player, b)
} else {
if player == 1 {
b[moveInt] = 1
} else if player == 2 {
b[moveInt] = 10
}
}
// Check for out-of-bounds
for moveInt > 9 {
fmt.Println("Please enter a number under 10.")
moveInt = askForPlay()
}
if player == 1{
b[moveInt] = 1
}else if player == 2{
b[moveInt] = 10
}
return b
}
func presentBoard(b [9]int) {
for i, v := range b {
if v == 0{
// empty space. Display number
fmt.Printf("%d", i)
} else if v == 1{
fmt.Printf("X")
} else if v == 10{
fmt.Printf("O")
}
// And now the decorations
if i> 0 && (i+1) % 3 == 0{
fmt.Printf("\n")
} else {
fmt.Printf(" | ")
}
}
}
func checkForWin(b [9]int) int {
// re-calculate sums Array
sums := [8] int {0,0,0,0,0,0,0,0}
// for _, v := range b[0:2] { sums[7] += v }
// for _, v := range b[3:5] { sums[6] += v }
// for _, v := range b[6:8] { sums[5] += v }
sums[0] = b[2]+b[4]+b[6]
sums[1] = b[0]+b[3]+b[6]
sums[2] = b[1]+b[4]+b[7]
sums[3] = b[2]+b[5]+b[8]
sums[4] = b[0]+b[4]+b[8]
sums[5] = b[6]+b[7]+b[8]
sums[6] = b[3]+b[4]+b[5]
sums[7] = b[0]+b[1]+b[2]
for _, v := range sums {
if v == 3{
return 1
} else if v == 30{
return 2
}
}
return 0
}