Include algebraic notation

This commit is contained in:
ekzyis 2024-09-23 06:07:34 +02:00
parent 677c50d007
commit 72da5d3afd
2 changed files with 21 additions and 4 deletions

View File

@ -13,6 +13,7 @@ import (
type Board struct { type Board struct {
tiles [8][8]*Piece tiles [8][8]*Piece
turn Color turn Color
moves []string
} }
func NewBoard() *Board { func NewBoard() *Board {
@ -131,6 +132,18 @@ func (b *Board) SetPiece(name PieceName, color Color, position string) error {
return nil return nil
} }
func (b *Board) AlgebraicNotation() string {
var text string
for i, m := range b.moves {
if i%2 == 0 {
text += fmt.Sprintf("%d. %s", i/2+1, m)
} else {
text += fmt.Sprintf(" %s ", m)
}
}
return text
}
func (b *Board) Parse(pgn string) error { func (b *Board) Parse(pgn string) error {
var ( var (
moves = strings.Split(pgn, " ") moves = strings.Split(pgn, " ")
@ -205,7 +218,7 @@ func (b *Board) Move(position string) error {
err = fmt.Errorf("invalid move: %s", position) err = fmt.Errorf("invalid move: %s", position)
} }
} else { } else {
err = fmt.Errorf("invalid move: %s", position) return fmt.Errorf("invalid move: %s", position)
} }
if err != nil { if err != nil {
@ -218,6 +231,8 @@ func (b *Board) Move(position string) error {
b.turn = Light b.turn = Light
} }
b.moves = append(b.moves, position)
return nil return nil
} }

View File

@ -1,6 +1,7 @@
package main package main
import ( import (
"fmt"
"log" "log"
"strings" "strings"
"time" "time"
@ -70,9 +71,9 @@ func tickGameStart(c *sn.Client) {
continue continue
} }
text := fmt.Sprintf("`%s`\n\n%s", b.AlgebraicNotation(), imgUrl)
var cId int var cId int
parentId := n.Item.Id if cId, err = c.CreateComment(n.Item.Id, text); err != nil {
if cId, err = c.CreateComment(parentId, imgUrl); err != nil {
log.Printf("error creating reply: %v\n", err) log.Printf("error creating reply: %v\n", err)
continue continue
} }
@ -158,8 +159,9 @@ func tickGameProgress(c *sn.Client) {
continue continue
} }
text := fmt.Sprintf("`%s`\n\n%s", b.AlgebraicNotation(), imgUrl)
var cId int var cId int
if cId, err = c.CreateComment(n.Item.Id, imgUrl); err != nil { if cId, err = c.CreateComment(n.Item.Id, text); err != nil {
log.Printf("error creating reply: %v\n", err) log.Printf("error creating reply: %v\n", err)
continue continue
} }