-
Notifications
You must be signed in to change notification settings - Fork 1
/
player.go
72 lines (65 loc) · 1.22 KB
/
player.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
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
)
func play(one Player, two Player) {
game := NewGame()
winner, over := game.Winner()
for !over {
switch game.LastPlayer() {
case ONE:
game.Nextp(two.TakeMove(game))
case TWO:
game.Nextp(one.TakeMove(game))
}
fmt.Println(game)
winner, over = game.Winner()
}
if winner == ZERO {
fmt.Printf("even!\n")
} else {
fmt.Printf("%s win!\n", winner)
}
}
type Player interface {
TakeMove(state *State) *Pos
}
type Human struct{}
func (h *Human) TakeMove(state *State) *Pos {
var move Pos
reader := bufio.NewReader(os.Stdin)
for {
fmt.Printf("%s(row, col)> ", state.LastPlayer().Flip())
line, _, _ := reader.ReadLine()
nums := strings.Split(string(line), ",")
if len(nums) != 2 {
if len(nums) == 1 && nums[0] == "q" {
fmt.Println("quit!")
os.Exit(0)
}
continue
}
r, err1 := strconv.Atoi(nums[0])
c, err2 := strconv.Atoi(nums[1])
if err1 != nil || err2 != nil {
continue
}
move.Row = r - 1
move.Col = c - 1
break
}
if state.IsValidMove(&move) {
return &move
}
return h.TakeMove(state)
}
type Robot struct {
MCTSIterMax int
}
func (r *Robot) TakeMove(state *State) *Pos {
return UCT(state, r.MCTSIterMax)
}