Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

go.mod and castling support #8

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
33 changes: 0 additions & 33 deletions Gopkg.lock

This file was deleted.

26 changes: 0 additions & 26 deletions Gopkg.toml

This file was deleted.

29 changes: 25 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,21 @@ Initialize the renderer with a FEN notation.
board, _ := chessimage.NewRendererFromFEN("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1")
```

Render the chess board to a png `image.Image` interface.
Render the chess board to a png `image.Image` interface. This will use the internal (embedded) images, that are licensed under the CC license (see ./assets/license.md)

```go
f, _ := os.Create("board.png")
defer f.Close()
image, _ := board.Render(chessimage.Options{AssetPath: "./assets/")})
image, _ := board.Render(chessimage.Options{})
png.Encode(f, image)
```

If you want to use your own chess piece images, provide a path to an asset folder:

```go
f, _ := os.Create("board.png")
defer f.Close()
image, _ := board.Render(chessimage.Options{AssetPath: "./assets/"})
png.Encode(f, image)
```

Expand All @@ -46,6 +55,18 @@ board.SetLastMove(chessimage.LastMove{

![](./docs/board_with_moves.png)

To highlight a castling move, use the MoveType field of the LastMove struct. Note that the From and To fields are not being used when highlighting castling moves.

```go
board.SetLastMove(chessimage.LastMove{
MoveType: chessimage.MoveTypeCastlingWK,
})
```

[Example](./blob/master/examples/castling.go)

![](./docs/castling.png)

## Mark Checked

You can highlight a tile as "checked".
Expand All @@ -69,9 +90,9 @@ options := chessimage.Options{
renderer.Render(options)
```

#### AssetPath (**Required**)
#### AssetPath

Specify the path of the image assets for the individual pieces. Feel free to use the assets packaged in this repo, but be aware they are under CC license.
Specify the path of the image assets for the individual pieces. Feel free to use the assets embedded in this repo, but be aware they are under CC license.

#### Inverted (`false`)

Expand Down
35 changes: 19 additions & 16 deletions board.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,12 @@ import "fmt"
// Tile represents a specific position of a tile on a chess board
type Tile int8

func (s Tile) rank() int {
return int(int(s) / 8)
func (t Tile) rankFile() (int, int) {
return int(t) / 8, int(t) % 8
}

func (s Tile) rankInverted() int {
return 7 - s.rank()
}

func (s Tile) file() int {
return int(s) % 8
}

func (s Tile) fileInverted() int {
return 7 - s.file()
func (t Tile) rankFileInv() (int, int) {
return 7 - int(t)/8, 7 - int(t)%8
}

func tileFromRankFile(rank int, file int) Tile {
Expand All @@ -32,13 +24,24 @@ type position struct {

type board []position

//LastMove represents two tiles that indicate a piece was moved
type MoveType int

const (
MoveTypeStandard MoveType = iota
MoveTypeCastlingWK
MoveTypeCastlingWQ
MoveTypeCastlingBK
MoveTypeCastlingBQ
)

// LastMove represents two tiles that indicate a piece was moved
type LastMove struct {
From Tile
To Tile
From Tile
To Tile
MoveType MoveType
}

//TileFromAN will attempt to get a tile by its algebraic notation (ie: "e5")
// TileFromAN will attempt to get a tile by its algebraic notation (ie: "e5")
func TileFromAN(an string) (Tile, error) {
tile, ok := tileMap[an]
if !ok {
Expand Down
Binary file added docs/castling.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/king_checked_inverted.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
41 changes: 41 additions & 0 deletions embed.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package chessimage

import (
_ "embed"
)

//go:embed "assets/pl.png"
var whitePawn []byte

//go:embed "assets/pd.png"
var blackPawn []byte

//go:embed "assets/bl.png"
var whiteBishop []byte

//go:embed "assets/bd.png"
var blackBishop []byte

//go:embed "assets/nl.png"
var whiteKnight []byte

//go:embed "assets/nd.png"
var blackKnight []byte

//go:embed "assets/rl.png"
var whiteRook []byte

//go:embed "assets/rd.png"
var blackRook []byte

//go:embed "assets/ql.png"
var whiteQueen []byte

//go:embed "assets/qd.png"
var blackQueen []byte

//go:embed "assets/kl.png"
var whiteKing []byte

//go:embed "assets/kd.png"
var blackKing []byte
15 changes: 8 additions & 7 deletions examples/board_with_moves.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build ignore
// +build ignore

package main
Expand All @@ -15,13 +16,13 @@ func main() {
if err != nil {
log.Fatalln(err)
}
board.SetLastMove(chessimage.LastMove{
From: chessimage.E2,
To: chessimage.E4,
})
image, err := board.Render(chessimage.Options{
AssetPath: "./assets/",
})
board.SetLastMove(
chessimage.LastMove{
From: chessimage.E2,
To: chessimage.E4,
},
)
image, err := board.Render(chessimage.Options{})
if err != nil {
log.Fatalln(err)
}
Expand Down
22 changes: 22 additions & 0 deletions examples/castling.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package main

import (
"image/png"
"log"
"os"

"github.com/cjsaylor/chessimage"
)

func main() {
board, err := chessimage.NewRendererFromFEN("r1bqkbnr/ppp2ppp/2np4/4p3/4P3/3B1N2/PPPP1PPP/RNBQ1RK1 b kq - 3 4")
if err != nil {
log.Fatalln(err)
}
board.SetLastMove(chessimage.LastMove{MoveType: chessimage.MoveTypeCastlingWK})
image, err := board.Render(chessimage.Options{})
if err != nil {
log.Fatalln(err)
}
png.Encode(os.Stdout, image)
}
12 changes: 7 additions & 5 deletions examples/king_checked.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ func main() {
if err != nil {
log.Fatalln(err)
}
board.SetLastMove(chessimage.LastMove{
From: chessimage.D7,
To: chessimage.G4,
})
board.SetLastMove(
chessimage.LastMove{
From: chessimage.D7,
To: chessimage.G4,
},
)
board.SetCheckTile(chessimage.G1)
image, err := board.Render(chessimage.Options{AssetPath: "./assets/"})
image, err := board.Render(chessimage.Options{})
if err != nil {
log.Fatalln(err)
}
Expand Down
12 changes: 7 additions & 5 deletions examples/king_checked_inverted.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ func main() {
if err != nil {
log.Fatalln(err)
}
board.SetLastMove(chessimage.LastMove{
From: chessimage.D7,
To: chessimage.G4,
})
board.SetLastMove(
chessimage.LastMove{
From: chessimage.D7,
To: chessimage.G4,
},
)
board.SetCheckTile(chessimage.G1)
image, err := board.Render(chessimage.Options{AssetPath: "./assets/", Inverted: true})
image, err := board.Render(chessimage.Options{Inverted: true})
if err != nil {
log.Fatalln(err)
}
Expand Down
5 changes: 2 additions & 3 deletions examples/starting_board.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build ignore
// +build ignore

package main
Expand All @@ -15,9 +16,7 @@ func main() {
if err != nil {
log.Fatalln(err)
}
image, err := board.Render(chessimage.Options{
AssetPath: "./assets/",
})
image, err := board.Render(chessimage.Options{})
if err != nil {
log.Fatalln(err)
}
Expand Down
11 changes: 11 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module github.com/hultan/chessimage

go 1.19

require (
github.com/flopp/go-findfont v0.0.0-20180308170802-e788239e52bc
github.com/fogleman/gg v1.1.0
golang.org/x/image v0.0.0-20180708004352-c73c2afc3b81
)

require github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
github.com/flopp/go-findfont v0.0.0-20180308170802-e788239e52bc h1:cqzZoaYMsDUGa4J2OP6UiJqRxXHarhT8tKkO/WpFL5Y=
github.com/flopp/go-findfont v0.0.0-20180308170802-e788239e52bc/go.mod h1:IOE5a/919uJLUrsF48h4y96LcOEjPWbZkMEAMPQDEnQ=
github.com/fogleman/gg v1.1.0 h1:wVTfU9tB/LDr2eI5HILatkzBQLD3yl0KtPFt8KlcSIY=
github.com/fogleman/gg v1.1.0/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k=
golang.org/x/image v0.0.0-20180708004352-c73c2afc3b81 h1:00VmoueYNlNz/aHIilyyQz/MHSqGoWJzpFv/HW8xpzI=
golang.org/x/image v0.0.0-20180708004352-c73c2afc3b81/go.mod h1:ux5Hcp/YLpHSI86hEcLt0YII63i6oz57MZXIpbrjZUs=
Loading