Skip to content

Commit ff363c8

Browse files
committed
added benchmark and replaced int by int8
1 parent d4481f5 commit ff363c8

File tree

6 files changed

+63
-16
lines changed

6 files changed

+63
-16
lines changed

cmd/engine/alpha_beta_engine.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ func (e *AlphaBetaEngine) Move(board game.Board, player game.FieldState, ko *gam
2323
e.killerMoves = make(map[int]*game.Point)
2424
}
2525

26-
for i := 0; i < 9; i++ {
27-
for j := 0; j < 9; j++ {
26+
for i := int8(0); i < 9; i++ {
27+
for j := int8(0); j < 9; j++ {
2828
if board[i][j] != game.Empty {
2929
continue
3030
}
@@ -123,7 +123,7 @@ func (e *AlphaBetaEngine) alphaBeta(board game.Board, player, opp game.FieldStat
123123
if board[i][j] != game.Empty {
124124
continue
125125
}
126-
pt := game.Point{Row: i, Col: j}
126+
pt := game.Point{Row: int8(i), Col: int8(j)}
127127
if ko != nil && pt.Row == ko.Row && pt.Col == ko.Col {
128128
continue
129129
}
@@ -179,7 +179,7 @@ func evaluate(board game.Board, player, opp game.FieldState) int {
179179
visited := make(map[game.Point]bool)
180180
for i := 0; i < 9; i++ {
181181
for j := 0; j < 9; j++ {
182-
pt := game.Point{Row: i, Col: j}
182+
pt := game.Point{Row: int8(i), Col: int8(j)}
183183
if visited[pt] || board[i][j] == game.Empty {
184184
continue
185185
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package engine
2+
3+
import (
4+
"testing"
5+
6+
"github.com/RubikNube/GoInGo/cmd/game"
7+
)
8+
9+
func emptyBoard() game.Board {
10+
var b game.Board
11+
for i := range b {
12+
for j := range b[i] {
13+
b[i][j] = game.Empty
14+
}
15+
}
16+
return b
17+
}
18+
19+
func midGameBoard() game.Board {
20+
b := emptyBoard()
21+
// Place some stones for a simple mid-game scenario
22+
b[2][2] = game.Black
23+
b[2][3] = game.White
24+
b[3][2] = game.White
25+
b[3][3] = game.Black
26+
b[4][4] = game.Black
27+
b[4][5] = game.White
28+
b[5][4] = game.White
29+
b[5][5] = game.Black
30+
return b
31+
}
32+
33+
func BenchmarkAlphaBetaEngine_EmptyBoard(b *testing.B) {
34+
engine := NewAlphaBetaEngine()
35+
board := emptyBoard()
36+
for i := 0; i < b.N; i++ {
37+
engine.Move(board, game.Black, nil)
38+
}
39+
}
40+
41+
func BenchmarkAlphaBetaEngine_MidGame(b *testing.B) {
42+
engine := NewAlphaBetaEngine()
43+
board := midGameBoard()
44+
for i := 0; i < b.N; i++ {
45+
engine.Move(board, game.White, nil)
46+
}
47+
}

cmd/engine/random_engine.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func (e *RandomEngine) Move(board game.Board, player game.FieldState, ko *game.P
1515
for i := 0; i < 9; i++ {
1616
for j := 0; j < 9; j++ {
1717
if board[i][j] == game.Empty {
18-
empty = append(empty, game.Point{Row: i, Col: j})
18+
empty = append(empty, game.Point{Row: int8(i), Col: int8(j)})
1919
}
2020
}
2121
}

cmd/game/gui.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
)
88

99
// FieldState A field an either empty or occupied by a black or white stone.
10-
type FieldState int
10+
type FieldState uint8
1111

1212
// Constants for the FieldState type
1313
const (

cmd/game/rules.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ const BoardSize = 9
55

66
// Point represents a coordinate on the board.
77
type Point struct {
8-
Row int
9-
Col int
8+
Row int8
9+
Col int8
1010
}
1111

1212
// Board is a BoardSize x BoardSize Go board.
@@ -15,7 +15,7 @@ type Board [BoardSize][BoardSize]FieldState
1515
// Neighbors returns the adjacent points of a given point.
1616
func Neighbors(p Point) []Point {
1717
var n []Point
18-
dirs := []struct{ dr, dc int }{{-1, 0}, {1, 0}, {0, -1}, {0, 1}}
18+
dirs := []struct{ dr, dc int8 }{{-1, 0}, {1, 0}, {0, -1}, {0, 1}}
1919
for _, d := range dirs {
2020
r, c := p.Row+d.dr, p.Col+d.dc
2121
if r >= 0 && r < BoardSize && c >= 0 && c < BoardSize {
@@ -101,10 +101,10 @@ func IsLegalMove(b Board, p Point, color FieldState, prev Board) bool {
101101
}
102102

103103
// CalculateScore returns the territory score for Black and White.
104-
func CalculateScore(b Board) (black, white int) {
104+
func CalculateScore(b Board) (black, white int8) {
105105
visited := make(map[Point]struct{})
106-
for i := 0; i < BoardSize; i++ {
107-
for j := 0; j < BoardSize; j++ {
106+
for i := int8(0); i < BoardSize; i++ {
107+
for j := int8(0); j < BoardSize; j++ {
108108
pt := Point{i, j}
109109
if b[i][j] == Black {
110110
black++
@@ -125,7 +125,7 @@ func CalculateScore(b Board) (black, white int) {
125125
}
126126

127127
// territoryOwner returns the size and owner (Black/White/Empty) of a territory.
128-
func territoryOwner(b Board, start Point, visited map[Point]struct{}) (size int, owner FieldState) {
128+
func territoryOwner(b Board, start Point, visited map[Point]struct{}) (size int8, owner FieldState) {
129129
queue := []Point{start}
130130
owner = Empty
131131
border := make(map[FieldState]struct{})

main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ func placeStone(g *gocui.Gui, v *gocui.View) error {
118118
return nil
119119
}
120120
// Ko rule: forbid move at koPoint
121-
if koPoint != nil && cursorRow == koPoint.Row && cursorCol == koPoint.Col {
121+
if koPoint != nil && int8(cursorRow) == koPoint.Row && int8(cursorCol) == koPoint.Col {
122122
if v, err := g.View("prompt"); err == nil && v != nil {
123123
v.Clear()
124124
fmt.Fprint(v, "Illegal move! Ko rule.")
@@ -156,7 +156,7 @@ func placeStone(g *gocui.Gui, v *gocui.View) error {
156156
opp = game.White
157157
}
158158
captured := []game.Point{}
159-
for _, n := range game.Neighbors(game.Point{Row: cursorRow, Col: cursorCol}) {
159+
for _, n := range game.Neighbors(game.Point{Row: int8(cursorRow), Col: int8(cursorCol)}) {
160160
if nextBoard[n.Row][n.Col] == opp {
161161
group, libs := game.Group(nextBoard, n)
162162
if len(libs) == 0 {
@@ -169,7 +169,7 @@ func placeStone(g *gocui.Gui, v *gocui.View) error {
169169
}
170170

171171
// Check for liberties of the placed stone's group (suicide rule)
172-
_, libs := game.Group(nextBoard, game.Point{Row: cursorRow, Col: cursorCol})
172+
_, libs := game.Group(nextBoard, game.Point{Row: int8(cursorRow), Col: int8(cursorCol)})
173173
if len(libs) == 0 {
174174
if v, err := g.View("prompt"); err == nil && v != nil {
175175
v.Clear()

0 commit comments

Comments
 (0)