Skip to content

Commit ca236dd

Browse files
committed
introduce Match class
1 parent 4b8e130 commit ca236dd

File tree

8 files changed

+142
-42
lines changed

8 files changed

+142
-42
lines changed

Diff for: .gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
go-go
1+
go
22
*.swp
33
*.swo
44
*.6
5+
todo.txt

Diff for: Makefile

+7-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ include $(GOROOT)/src/Make.inc
22

33
TARG=go
44
GOFILES=src/go/go.go
5-
O_FILES=point.$O cell.$O history.$O board.$O player.$O console_player.$O auto_player.$O server.$O go.$O
5+
#O_FILES=point.$O cell.$O history.$O board.$O player.$O console_player.$O auto_player.$O match.$0 server.$O go.$O
6+
O_FILES=point.$O cell.$O history.$O board.$O player.$O console_player.$O match.$O server.$O go.$O
67

78
all: $(O_FILES)
89
install: $(O_FILES)
@@ -27,8 +28,11 @@ player.$O:
2728
console_player.$O:
2829
$(QUOTED_GOBIN)/$(GC) -o console_player.$O src/go/console_player.go
2930

30-
auto_player.$O:
31-
$(QUOTED_GOBIN)/$(GC) -o auto_player.$O src/go/auto_player.go
31+
#auto_player.$O:
32+
# $(QUOTED_GOBIN)/$(GC) -o auto_player.$O src/go/auto_player.go
33+
34+
match.$O:
35+
$(QUOTED_GOBIN)/$(GC) -o match.$O src/go/match.go
3236

3337
server.$O:
3438
$(QUOTED_GOBIN)/$(GC) -o server.$O src/go/server.go

Diff for: src/go/auto_player.go

+8
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ func (cp *AutoPlayer)Teban() player.Teban {
2121
return cp.teban
2222
}
2323

24+
func (cp *ConsolePlayer)SetTeban(t player.Teban) {
25+
cp.teban = t
26+
}
27+
2428
func (cp *AutoPlayer)Next(b *board.Board) player.Status {
2529
return player.GIVEUP
2630
}
31+
32+
func (cp *AutoPlayer)Next2(b *board.Board, h *history.History, agehama [2]int) Status {
33+
return cp.Next(b)
34+
}

Diff for: src/go/console_player.go

+22-14
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"./cell"
1010
"./board"
1111
"./player"
12+
"./history"
1213
)
1314

1415
type ConsolePlayer struct {
@@ -28,7 +29,9 @@ func (cp *ConsolePlayer)Teban() player.Teban {
2829
return cp.teban
2930
}
3031

31-
func (cp *ConsolePlayer)Next(b *board.Board) player.Status {
32+
func (cp *ConsolePlayer)Next(b *board.Board, h *history.History, agehama [2]int) *player.Response {
33+
res := new(player.Response)
34+
3235
r := bufio.NewReader(os.Stdin)
3336
re := regexp.MustCompile("[0-9]+")
3437
var color cell.Cell
@@ -45,19 +48,24 @@ func (cp *ConsolePlayer)Next(b *board.Board) player.Status {
4548
if len(xy) < 2 {
4649
b.Pass(color)
4750
fmt.Printf("[%s] Pass!\n", cp.name)
48-
return player.PASS
49-
}
50-
x, _ := strconv.Atoi(xy[0])
51-
y, _ := strconv.Atoi(xy[1])
52-
takenOffs, ok := b.PutAt(color, x, y)
53-
if ok == board.OK {
54-
fmt.Printf("[%s] Put (%d, %d) and Take %d\n", cp.name, x, y, len(takenOffs))
55-
break
56-
} else if ok == board.KO {
57-
fmt.Printf("[%s] Kou\n", cp.name)
58-
} else if ok == board.FORBIDDEN {
59-
fmt.Printf("[%s] Forbidden\n", cp.name)
51+
res.Status = player.PASS
52+
} else {
53+
x, _ := strconv.Atoi(xy[0])
54+
y, _ := strconv.Atoi(xy[1])
55+
takenOffs, ok := b.PutAt(color, x, y)
56+
if ok == board.OK {
57+
fmt.Printf("[%s] Put (%d, %d) and Take %d\n", cp.name, x, y, len(takenOffs))
58+
res.Status = player.PUT
59+
res.Data = map[string]interface{}{"x":x, "y":y, "taken":takenOffs}
60+
break
61+
} else if ok == board.KO {
62+
fmt.Printf("[%s] Ko\n", cp.name)
63+
res.Status = player.KO
64+
} else if ok == board.FORBIDDEN {
65+
fmt.Printf("[%s] Forbidden\n", cp.name)
66+
res.Status = player.FORBIDDEN
67+
}
6068
}
6169
}
62-
return player.PUT
70+
return res
6371
}

Diff for: src/go/go.go

+24-23
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,49 @@ import (
44
"os"
55
"fmt"
66
"flag"
7+
"./match"
78
"./board"
89
"./player"
910
"./server"
1011
"./console_player"
1112
//"./auto_player"
1213
)
1314

14-
func startConsole(b *board.Board, ps [2]player.Player) {
15+
func startConsole(b *board.Board) {
16+
players := [2]player.Player{
17+
console_player.New("ando", player.SENTE),
18+
console_player.New("yasushi", player.GOTE)}
19+
m := match.New(b, players)
20+
1521
fmt.Printf("%v", b)
16-
turn := 0
17-
pass := false
1822
for {
19-
switch ps[turn].Next(b) {
23+
matchStatus, playerStatus := m.Next()
24+
switch playerStatus {
2025
case player.PUT:
2126
fmt.Printf("%v", b)
22-
pass = false
27+
case player.KO:
28+
fmt.Printf("Ko", b)
29+
case player.FORBIDDEN:
30+
fmt.Printf("Forbidden", b)
2331
case player.PASS:
24-
if pass {
32+
if matchStatus == match.FINISH {
2533
fmt.Println("Finish!")
2634
os.Exit(0)
27-
} else {
28-
pass = true
2935
}
3036
case player.GIVEUP:
31-
fmt.Printf("%s Win!\n", ps[turn].Name())
37+
fmt.Printf("%s Win!\n", (*m.Winner).Name())
3238
os.Exit(0)
3339
}
34-
turn++
35-
turn %= 2
3640
}
3741
}
3842

39-
func startServer(port int, b *board.Board, ps [2]player.Player) {
40-
s := server.New(b, ps)
43+
func startServer(port int, b *board.Board) {
44+
players := [2]player.Player{
45+
//http_player.New("ando", player.SENTE),
46+
//http_player.New("yasushi", player.GOTE)}
47+
console_player.New("ando", player.SENTE),
48+
console_player.New("yasushi", player.GOTE)}
49+
s := server.New(b, players)
4150
s.Start(port)
4251
}
4352

@@ -55,16 +64,8 @@ func main() {
5564
}
5665

5766
if *server {
58-
players := [2]player.Player{
59-
//http_player.New("ando", player.SENTE),
60-
//http_player.New("yasushi", player.GOTE)}
61-
console_player.New("ando", player.SENTE),
62-
console_player.New("yasushi", player.GOTE)}
63-
startServer(*port, b, players)
67+
startServer(*port, b)
6468
} else {
65-
players := [2]player.Player{
66-
console_player.New("ando", player.SENTE),
67-
console_player.New("yasushi", player.GOTE)}
68-
startConsole(b, players)
69+
startConsole(b)
6970
}
7071
}

Diff for: src/go/history.go

+4
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,7 @@ func (h *History)Pass(color cell.Cell) {
4545
func (h *History)Last() Record {
4646
return h.history.Last().(Record)
4747
}
48+
49+
func (h *History)IsLastPass() bool {
50+
return h.history.Last().(Record).x == -1
51+
}

Diff for: src/go/match.go

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package match
2+
3+
import (
4+
"fmt"
5+
"container/vector"
6+
"./cell"
7+
"./board"
8+
"./player"
9+
"./history"
10+
)
11+
12+
type Status int
13+
const (
14+
PLAYING = 0
15+
FINISH = 1
16+
)
17+
18+
type Match struct {
19+
turn int
20+
board *board.Board
21+
history *history.History
22+
players [2]player.Player
23+
agehama [2]int
24+
Winner *player.Player
25+
}
26+
27+
func New(b *board.Board, players [2]player.Player) *Match {
28+
match := &Match{0, b, history.New(), players, [2]int{0, 0}, nil}
29+
return match
30+
}
31+
32+
func (m *Match)NextTurn() {
33+
m.turn++
34+
m.turn %= 2
35+
}
36+
37+
func (m *Match)Next() (Status, player.Status) {
38+
p := m.players[m.turn]
39+
var color cell.Cell
40+
if p.Teban() == player.SENTE { color = cell.BLACK } else { color = cell.WHITE }
41+
resp := p.Next(m.board, m.history, m.agehama)
42+
var status Status
43+
switch resp.Status {
44+
case player.PUT:
45+
fmt.Printf("%v\n", resp.Data)
46+
taken := resp.Data["taken"].(vector.Vector)
47+
m.history.Add(color, resp.Data["x"].(int), resp.Data["y"].(int), taken)
48+
m.agehama[m.turn] += len(taken)
49+
status = PLAYING
50+
m.NextTurn()
51+
case player.PASS:
52+
if m.history.IsLastPass() {
53+
m.history.Pass(color)
54+
// m.Winner = ?
55+
status = FINISH
56+
} else {
57+
m.history.Pass(color)
58+
status = PLAYING
59+
m.NextTurn()
60+
}
61+
case player.GIVEUP:
62+
m.Winner = &m.players[(m.turn+1)%2]
63+
status = FINISH
64+
}
65+
return status, resp.Status
66+
}

Diff for: src/go/player.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,23 @@ package player
22

33
import (
44
"./board"
5+
"./history"
56
)
67

78
type Status int
89
const (
910
PUT Status = 0
1011
PASS Status = 1
1112
GIVEUP Status = 2
13+
KO Status = 3
14+
FORBIDDEN Status = 4
1215
)
1316

17+
type Response struct {
18+
Status Status
19+
Data map[string] interface{}
20+
}
21+
1422
type Teban int
1523
const (
1624
SENTE Teban = 0
@@ -21,5 +29,5 @@ const (
2129
type Player interface {
2230
Name() string
2331
Teban() Teban
24-
Next(b *board.Board) Status
32+
Next(b *board.Board, h *history.History, agehama [2]int) *Response
2533
}

0 commit comments

Comments
 (0)