1
0
mirror of https://github.com/maaslalani/gambit.git synced 2024-12-26 20:54:07 +02:00

fix: validate fen

This commit is contained in:
Maas Lalani 2022-02-06 23:02:42 -05:00
parent 3bc7d04a80
commit f476062edb
No known key found for this signature in database
GPG Key ID: 5A6ED5CBF1A0A000
2 changed files with 11 additions and 0 deletions

View File

@ -2,9 +2,16 @@ package fen
import (
"fmt"
"regexp"
"strings"
)
var fenRegex = regexp.MustCompile(`\s*([rnbqkpRNBQKP1-8]+\/){7}([rnbqkpRNBQKP1-8]+)\s[bw-]\s(([a-hkqA-HKQ]{1,4})|(-))\s(([a-h][36])|(-))\s\d+\s\d+\s*`)
func IsValid(fen string) bool {
return fenRegex.MatchString(fen)
}
// Tokens returns the (6) tokens of a FEN string
//
// [Pieces, Turn, Castling, En passant, Halfmove Clock, Fullmove number]

View File

@ -31,7 +31,11 @@ type model struct {
// InitialModel returns an initial model of the game board.
func InitialModel(position string) tea.Model {
if !fen.IsValid(position) {
position = dt.Startpos
}
board := dt.ParseFen(position)
return model{
board: &board,
moves: board.GenerateLegalMoves(),