-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
64 lines (57 loc) · 1.25 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
package main
import (
"fmt"
"strconv"
"time"
"github.com/jsadowyj0/CIS167_FinalProject/ansi"
)
func clear() {
// clears screen with ANSI escapes
fmt.Print("\033[H\033[2J")
}
func isValidInput(input string) bool {
if _, err := strconv.Atoi(input); err == nil {
parsedInput, _ := strconv.Atoi(input)
return parsedInput >= 0 && parsedInput <= 8
}
return false
}
func isValidBoolean(input string) bool {
_, err := strconv.ParseBool(input)
return err == nil
}
func main() {
var input string
gameOver := false
board := Board{}
board.New()
board.Print()
fmt.Print(ansi.Bold("Press enter to start..."))
fmt.Scanln(&input)
for !gameOver {
var msg string
// X's turn
board.PlayerTurn()
gameOver, msg = board.CheckGameOver()
if !gameOver {
// O's turn
board.Print()
fmt.Print(ansi.Bold(ansi.Green("Thinking...")))
time.Sleep(time.Second / 3)
board.PlaceO(generateMaximizerMove(board))
gameOver, msg = board.CheckGameOver()
}
if gameOver {
input = ""
for !isValidBoolean(input) {
board.Print()
fmt.Println(board.GenerateMessage(msg))
fmt.Printf("Play again? [%s/%s]: ", ansi.Red("0"), ansi.Green("1"))
fmt.Scanln(&input)
}
choice, _ := strconv.ParseBool(input)
gameOver = !choice
board.New()
}
}
}