Skip to content

Commit

Permalink
wip: ICCF hack
Browse files Browse the repository at this point in the history
  • Loading branch information
marianogappa committed Jun 4, 2023
1 parent 098c7f6 commit 620c8cf
Show file tree
Hide file tree
Showing 13 changed files with 5,775 additions and 4,141 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
cheesse
poc

dist/
45 changes: 45 additions & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# This is an example .goreleaser.yml file with some sensible defaults.
# Make sure to check the documentation at https://goreleaser.com
before:
hooks:
# You may remove this if you don't use go modules.
- go mod tidy
# you may remove this if you don't need go generate
- go generate ./...
builds:
- env:
- CGO_ENABLED=0
goos:
- linux
- windows
- darwin

archives:
- format: tar.gz
# this name template makes the OS and Arch compatible with the results of uname.
name_template: >-
{{ .ProjectName }}_
{{- title .Os }}_
{{- if eq .Arch "amd64" }}x86_64
{{- else if eq .Arch "386" }}i386
{{- else }}{{ .Arch }}{{ end }}
{{- if .Arm }}v{{ .Arm }}{{ end }}
# use zip for windows archives
format_overrides:
- goos: windows
format: zip
checksum:
name_template: 'checksums.txt'
snapshot:
name_template: "{{ incpatch .Version }}-next"
changelog:
sort: asc
filters:
exclude:
- '^docs:'
- '^test:'

# The lines beneath this are called `modelines`. See `:help modeline`
# Feel free to remove those if you don't want/use them.
# yaml-language-server: $schema=https://goreleaser.com/static/schema.json
# vim: set ts=2 sw=2 tw=0 fo=cnqoj
14 changes: 14 additions & 0 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,17 @@ func (a API) ParseNotation(game InputGame, notationString string) (OutputGame, [
gameSteps, err := newNotationParserAlgebraic(characteristics{}).parse(parsedGame, notationString)
return mapGameToOutputGame(parsedGame), mapGameStepsToOutputGameSteps(gameSteps), err
}

// N.B. I'm making this quick hack for a request I got from someone
func (a API) HackParseNotationToICCF(game InputGame, notationString string) (OutputGame, []OutputGameStep, error) {
parsedGame, err := a.parseGame(game)
if err != nil {
return OutputGame{}, []OutputGameStep{}, err
}

// TODO at the moment there only exists an algebraic notation parser
gameSteps, err := newNotationParserAlgebraic(characteristics{}).parse(parsedGame, notationString)
gameSteps = gamestepsToICCF(gameSteps)

return mapGameToOutputGame(parsedGame), mapGameStepsToOutputGameSteps(gameSteps), err
}
28 changes: 16 additions & 12 deletions api/entities.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import (
)

type game struct {
canWhiteCastle bool
canWhiteKingsideCastle bool
canWhiteQueensideCastle bool
canBlackCastle bool
canBlackKingsideCastle bool
canBlackQueensideCastle bool
canBlackKingsideCastle bool
canWhiteCastle bool
canWhiteQueensideCastle bool
canWhiteKingsideCastle bool
halfMoveClock int
fullMoveNumber int
isLastMoveEnPassant bool
Expand Down Expand Up @@ -58,12 +58,12 @@ func (g game) clone() game {
clonedInCheckBy[i] = piece
}
return game{
canWhiteCastle: g.canWhiteCastle,
canWhiteKingsideCastle: g.canWhiteKingsideCastle,
canWhiteQueensideCastle: g.canWhiteQueensideCastle,
canBlackCastle: g.canBlackCastle,
canBlackKingsideCastle: g.canBlackKingsideCastle,
canBlackQueensideCastle: g.canBlackQueensideCastle,
canBlackKingsideCastle: g.canBlackKingsideCastle,
canWhiteCastle: g.canWhiteCastle,
canWhiteQueensideCastle: g.canWhiteQueensideCastle,
canWhiteKingsideCastle: g.canWhiteKingsideCastle,
halfMoveClock: g.halfMoveClock,
fullMoveNumber: g.fullMoveNumber,
enPassantTargetSquare: g.enPassantTargetSquare,
Expand Down Expand Up @@ -96,8 +96,8 @@ type action struct {
isEnPassant bool
isEnPassantCapture bool
isCastle bool
isKingsideCastle bool
isQueensideCastle bool
isKingsideCastle bool
promotionPieceType pieceType
capturedPiece piece
}
Expand All @@ -116,10 +116,10 @@ func (a action) String() string {
return fmt.Sprintf("%s's Pawn at %v promotes to %v", a.fromPiece.owner, a.fromPiece.xy.toAlgebraic(), a.promotionPieceType)
case a.isEnPassant:
return fmt.Sprintf("%s's Pawn at %v does en passant", a.fromPiece.owner, a.fromPiece.xy.toAlgebraic())
case a.isKingsideCastle:
return fmt.Sprintf("%s kingside castles", a.fromPiece.owner)
case a.isQueensideCastle:
return fmt.Sprintf("%s queenside castles", a.fromPiece.owner)
case a.isKingsideCastle:
return fmt.Sprintf("%s kingside castles", a.fromPiece.owner)
}
return fmt.Sprintf("%s's %s at %v moves to %v", a.fromPiece.owner, a.fromPiece.pieceType, a.fromPiece.xy.toAlgebraic(), a.toXY.toAlgebraic())
}
Expand All @@ -137,8 +137,8 @@ func (a action) DebugString() string {
a.isEnPassant,
a.isEnPassantCapture,
a.isCastle,
a.isKingsideCastle,
a.isQueensideCastle,
a.isKingsideCastle,
a.promotionPieceType,
a.capturedPiece.pieceType,
a.capturedPiece.xy.x,
Expand Down Expand Up @@ -209,6 +209,10 @@ func (c xy) toAlgebraic() string {
return fmt.Sprintf("%v%v", string("abcdefgh"[c.x]), 8-c.y)
}

func (c xy) toICCF() string {
return fmt.Sprintf("%v%v", c.x+1, 8-c.y)
}

func (c xy) deltaTowards(c2 xy) xy {
var delta xy
switch {
Expand Down
34 changes: 34 additions & 0 deletions api/gamesteps_to_iccf.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package api

import "fmt"

func gamestepsToICCF(gamesteps []gameStep) []gameStep {
iccfGameSteps := []gameStep{}
for _, gamestep := range gamesteps {
gamestep.s = gamestepToICCF(gamestep)
iccfGameSteps = append(iccfGameSteps, gamestep)
}
return iccfGameSteps
}

func gamestepToICCF(gamestep gameStep) string {
iccfMove := fmt.Sprintf(
"%v%v",
gamestep.a.fromPiece.xy.toICCF(),
gamestep.a.toXY.toICCF(),
)
// For promotion, a fifth digit is added to the move's notation: 1 for queen, 2 for rook, 3 for bishop, and 4 for knight.
if gamestep.a.isPromotion {
switch gamestep.a.promotionPieceType {
case pieceQueen:
iccfMove += "1"
case pieceRook:
iccfMove += "2"
case pieceBishop:
iccfMove += "3"
case pieceKnight:
iccfMove += "4"
}
}
return iccfMove
}
Loading

0 comments on commit 620c8cf

Please sign in to comment.