-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
241 lines (219 loc) · 5.34 KB
/
main.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
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
package main
import (
"fmt"
"os"
)
type Board [3][3]int
type Coordinate [2]int
var maxRecursionLevel int = 3
func main() {
currentBoard := Board{
{0, 0, 0},
{0, 0, 0},
{0, 0, 0},
}
//track current player. Set to 1 for the computer to start playing
player := -1
currentBoard.render(Coordinate{5, 5})
for currentBoard.victory() == 0 {
var newX, newY int
if player == -1 {
//
// 👨💻 user plays
//
fmt.Println("👨💻User turn (✗):")
newX, newY = receiveInput(currentBoard)
currentBoard[newX][newY] = -1
player *= -1 //invert player
} else {
//
// 🤖computer plays
//
fmt.Println("🤖 Computer turn (○):")
// list possible moves by ranking
count := 8
moves := currentBoard.rankMoves(1, &count)
//get best ranked coordinate
var maxRank float32 = -500000
var bestCoord Coordinate
for coord, rank := range moves {
if rank > maxRank {
maxRank = rank
bestCoord = coord
}
}
newX, newY = bestCoord[0], bestCoord[1]
currentBoard[newX][newY] = 1
fmt.Printf("Ranks: %v\n", moves)
fmt.Printf("Computer has simulated %v moves\n", count)
player *= -1 //invert player
}
currentBoard.render(Coordinate{newX, newY})
}
switch currentBoard.victory() {
case 1:
fmt.Println("✗ wins!")
case 2:
fmt.Println("○ wins!")
case 3:
fmt.Println("Draw!")
}
}
// receives a coordinate to make a move and return the values only when they are valid
func receiveInput(b Board) (int, int) {
var boardX, boardY int
for { //loop until valid coordinates
fmt.Print("Type coordinates separated by space: ")
_, err := fmt.Scan(&boardX, &boardY)
if err != nil { //input is not valid
fmt.Println("Error reading coordinates:", err)
continue
}
if (boardX >= 1 && boardX <= 3) && (boardY >= 1 && boardY <= 3) { // check if user input coordinates are on a valid range
if b[boardX-1][boardY-1] == 0 { // check if user input coordinates are on an empy cell
break
} else {
fmt.Println("Coordinate is not empty")
}
} else {
fmt.Println("Coordinates are not on a valid range (1,2 or 3 for each axis)")
}
}
return boardX - 1, boardY - 1
}
//draw a small tictactoe board on terminal
func (b *Board) render(lastMove Coordinate) {
const colorRed = "\033[0;31m"
const colorBlack = "\033[0;30m"
const colorNone = "\033[0m"
fmt.Println()
for i := 0; i < 3; i++ { //iterate rows
fmt.Print(" ")
for j := 0; j < 3; j++ { // iterate columns
switch b[i][j] {
case 0:
fmt.Fprintf(os.Stdout, "%s%s%s", colorBlack, "■ ", colorNone)
case -1:
if lastMove == [2]int{i, j} {
fmt.Fprintf(os.Stdout, "%s%s%s", colorRed, "✗ ", colorNone)
} else {
fmt.Print("✗ ")
}
case 1:
if lastMove == [2]int{i, j} {
fmt.Fprintf(os.Stdout, "%s%s%s", colorRed, "○ ", colorNone)
} else {
fmt.Print("○ ")
}
default:
fmt.Print("? ")
}
}
fmt.Println()
}
fmt.Println()
}
//give a score to the current board configuration. The bigger the score, higher chances to win.
func (b *Board) calculateScore() float32 {
var score float32 = 0
pos2 := 0
neg2 := 0
winCombinations := [8][3]Coordinate{
{{0, 0}, {0, 1}, {0, 2}}, // upper row
{{1, 0}, {1, 1}, {1, 2}}, // middle row
{{2, 0}, {2, 1}, {2, 2}}, // lower row
{{0, 0}, {1, 0}, {2, 0}}, // left column
{{0, 1}, {1, 1}, {2, 1}}, // middle column
{{0, 2}, {1, 2}, {2, 2}}, // right column
{{0, 2}, {1, 1}, {2, 0}}, // ascending diagonal
{{0, 0}, {1, 1}, {2, 2}}, // descending diagonal
}
for _, line := range winCombinations {
sum := 0
for _, coord := range line {
sum += b[coord[0]][coord[1]]
}
switch sum {
case 3:
score = 1000.0
return score
case -3:
score = -1000.0
return score
case 2:
pos2++
score += 20
case -2:
neg2++
score -= 20
}
}
if pos2 >= 2 {
return 300
}
if neg2 >= 2 {
return -300
}
return score
}
//find coordinates with 0 values
func (b Board) availableMoves() []Coordinate {
var result []Coordinate
for i := 0; i < 3; i++ { //iterate rows
for j := 0; j < 3; j++ { //iterate columns
if b[i][j] == 0 {
result = append(result, [2]int{i, j})
}
}
}
return result
}
// check victory status
// 0: game not ended
// 1: ✗ won
// 2: ○ won
// 3: Draw
func (b Board) victory() int {
currentScore := b.calculateScore()
switch {
case currentScore >= 1000:
return 2
case currentScore <= -1000:
return 1
default:
if len(b.availableMoves()) == 0 {
return 3
} else {
return 0
}
}
}
//simulate next moves and create a ranking. r represents how many moves ahead to consider.
func (b Board) rankMoves(r int, counter *int) map[Coordinate]float32 {
available := b.availableMoves()
moveScores := make(map[Coordinate]float32)
for _, move := range available {
var b2 Board = b
var score float32 = 0.0
// simulate player or user moves
if r%2 == 0 {
b2[move[0]][move[1]] = -1
} else {
b2[move[0]][move[1]] = 1
}
score = b2.calculateScore()
if r < maxRecursionLevel && b2.victory() == 0 { //iterate trough next moves
*counter++ //count how many times the function executed
moveScores2 := b2.rankMoves(r+1, counter)
//sum scores to evaluate how balanced is the scenario
var sum float32
for _, s := range moveScores2 {
sum += s
}
averageScores2 := sum
score = (score + averageScores2*0.9) / 1.9
}
moveScores[move] = score
}
return moveScores
}