Skip to content

Commit 8ce5e4a

Browse files
committed
feat: switch to corentings chess library
1 parent d2ac450 commit 8ce5e4a

File tree

12 files changed

+41
-18
lines changed

12 files changed

+41
-18
lines changed

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ module tinychess
33
go 1.22.2
44

55
require (
6+
github.com/corentings/chess/v2 v2.2.0
67
github.com/google/uuid v1.5.0
7-
github.com/notnil/chess v1.10.0
88
gorm.io/driver/postgres v1.5.4
99
gorm.io/gorm v1.25.5
1010
)
@@ -16,5 +16,6 @@ require (
1616
github.com/jinzhu/inflection v1.0.0 // indirect
1717
github.com/jinzhu/now v1.1.5 // indirect
1818
golang.org/x/crypto v0.14.0 // indirect
19+
golang.org/x/exp v0.0.0-20250128182459-e0ece0dbea4c // indirect
1920
golang.org/x/text v0.13.0 // indirect
2021
)

go.sum

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
github.com/ajstarks/svgo v0.0.0-20200320125537-f189e35d30ca/go.mod h1:K08gAheRH3/J6wwsYMMT4xOr94bZjxIelGM0+d/wbFw=
1+
github.com/corentings/chess/v2 v2.2.0 h1:cvponglvX2gw73hGCgeP2B/RJOWqS1NElwBKNc2eue8=
2+
github.com/corentings/chess/v2 v2.2.0/go.mod h1:JhWYDbjY81/7NECXrLzz4g2r9taaMEXvyqS4gYZciVE=
23
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
5+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
36
github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU=
47
github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
58
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
@@ -12,18 +15,23 @@ github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD
1215
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
1316
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
1417
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
15-
github.com/notnil/chess v1.10.0 h1:RR3MgS9G6zZmJ+VPTJolyxdaIgxoUPyUUY+2iaw35G0=
16-
github.com/notnil/chess v1.10.0/go.mod h1:cRuJUIBFq9Xki05TWHJxHYkC+fFpq45IWwk94DdlCrA=
18+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
1719
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
1820
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
1921
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
2022
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
23+
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
24+
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
2125
golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc=
2226
golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4=
27+
golang.org/x/exp v0.0.0-20250128182459-e0ece0dbea4c h1:KL/ZBHXgKGVmuZBZ01Lt57yE5ws8ZPSkkihmEyq7FXc=
28+
golang.org/x/exp v0.0.0-20250128182459-e0ece0dbea4c/go.mod h1:tujkw807nyEEAamNbDrEGzRav+ilXA7PCRAd6xsmwiU=
2329
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
2430
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
2531
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
2632
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
33+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
34+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
2735
gorm.io/driver/postgres v1.5.4 h1:Iyrp9Meh3GmbSuyIAGyjkN+n9K+GHX9b9MqsTL4EJCo=
2836
gorm.io/driver/postgres v1.5.4/go.mod h1:Bgo89+h0CRcdA33Y6frlaHHVuTdOf87pmyzwW9C/BH0=
2937
gorm.io/gorm v1.25.5 h1:zR9lOiiYf09VNh5Q1gphfyia1JpiClIWG9hQaxB/mls=

internal/game/game.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"fmt"
66
"time"
77

8-
"github.com/notnil/chess"
8+
"github.com/corentings/chess/v2"
99
)
1010

1111
// Touch updates the last seen timestamp for a game
@@ -25,7 +25,7 @@ func (g *Game) MovesUCI() []string {
2525
s := uci.Encode(tmp.Position(), m)
2626
out = append(out, s)
2727
if mv2, err := uci.Decode(tmp.Position(), s); err == nil {
28-
_ = tmp.Move(mv2)
28+
_ = tmp.Move(mv2, nil)
2929
}
3030
}
3131
return out
@@ -72,7 +72,21 @@ func (g *Game) MakeMove(uci string) error {
7272
g.Mu.Lock()
7373
defer g.Mu.Unlock()
7474

75-
return g.g.MoveStr(uci)
75+
mv, err := chess.UCINotation{}.Decode(g.g.Position(), uci)
76+
if err != nil {
77+
return err
78+
}
79+
valid := false
80+
for _, m := range g.g.ValidMoves() {
81+
if m.S1() == mv.S1() && m.S2() == mv.S2() && m.Promo() == mv.Promo() {
82+
valid = true
83+
break
84+
}
85+
}
86+
if !valid {
87+
return fmt.Errorf("illegal move")
88+
}
89+
return g.g.Move(mv, nil)
7690
}
7791

7892
// AddWatcher adds a new watcher channel

internal/game/game_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ import (
55
"testing"
66
"time"
77

8-
"github.com/notnil/chess"
8+
"github.com/corentings/chess/v2"
99
)
1010

1111
// helper to create a new Game with necessary fields
1212
func newTestGame() *Game {
1313
return &Game{
14-
g: chess.NewGame(chess.UseNotation(chess.UCINotation{})),
14+
g: chess.NewGame(),
1515
Watchers: make(map[chan []byte]struct{}),
1616
LastReact: make(map[string]time.Time),
1717
}

internal/game/hub.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"math/rand"
55
"time"
66

7-
"github.com/notnil/chess"
7+
"github.com/corentings/chess/v2"
88
)
99

1010
// NewHub creates a new game hub with cleanup goroutine
@@ -44,7 +44,7 @@ func (h *Hub) Get(id, clientId string) (*Game, *chess.Color) {
4444
color = chess.Black
4545
}
4646
g = &Game{
47-
g: chess.NewGame(chess.UseNotation(chess.UCINotation{})),
47+
g: chess.NewGame(),
4848
Watchers: make(map[chan []byte]struct{}),
4949
LastReact: make(map[string]time.Time),
5050
Clients: make(map[string]chess.Color),

internal/game/hub_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"testing"
55
"time"
66

7-
"github.com/notnil/chess"
7+
"github.com/corentings/chess/v2"
88
)
99

1010
// runCleanup mimics the hub's cleanup routine for testing purposes.

internal/game/remove_client_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package game
33
import (
44
"testing"
55

6-
"github.com/notnil/chess"
6+
"github.com/corentings/chess/v2"
77
)
88

99
func TestRemoveClient(t *testing.T) {

internal/game/types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"sync"
55
"time"
66

7-
"github.com/notnil/chess"
7+
"github.com/corentings/chess/v2"
88
)
99

1010
// Hub manages all active chess games

internal/handlers/handle_move_test.go

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

99
"tinychess/internal/game"
1010

11-
"github.com/notnil/chess"
11+
"github.com/corentings/chess/v2"
1212
)
1313

1414
// Test that a move is rejected when the piece is not of the player's color.

internal/handlers/handle_release_test.go

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

99
"tinychess/internal/game"
1010

11-
"github.com/notnil/chess"
11+
"github.com/corentings/chess/v2"
1212
)
1313

1414
func TestHandleRelease(t *testing.T) {

0 commit comments

Comments
 (0)