-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
58 lines (49 loc) · 1.16 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
package main
import (
"flag"
"fmt"
"math/rand"
"time"
)
var nplayers int
var depthAI int
var turnAI bool
var sleepAI time.Duration
func init() {
rand.Seed(time.Now().Unix())
flag.IntVar(&nplayers, "n", 1, "number of players")
flag.IntVar(&depthAI, "d", 5, "AI depth (-1 for best play)")
flag.BoolVar(&turnAI, "c", true, "computer starts")
flag.BoolVar(&ShowAnalysis, "a", true, "show computer analysis")
flag.BoolVar(&ClearScreen, "l", true, "show one board at a time (clear screen)")
flag.DurationVar(&sleepAI, "s", 5e8, `simulate thinking by "sleeping"`)
}
func scanMove(p Player) (in int) {
fmt.Printf("%v> ", p)
fmt.Scanf("%d", &in)
return in
}
func main() {
flag.Parse()
game := NewGame()
fmt.Println(game)
for !game.Over() {
if nplayers >= 2 || nplayers == 1 && !turnAI { // human turn
m := scanMove(game.Turn())
if ok := game.Move(m); !ok {
continue
}
} else {
time.Sleep(sleepAI)
game.MoveAI(depthAI)
}
fmt.Println(game)
turnAI = !turnAI
}
if w := game.Winner(); w != None {
fmt.Printf("Player %s won\n", w)
} else {
fmt.Printf("Draw\n")
}
time.Sleep(sleepAI) // useful if program run in a loop, WarGames style
}