mirror of
https://github.com/maaslalani/gambit.git
synced 2024-12-28 21:08:58 +02:00
passing
This commit is contained in:
parent
8004a4bf4f
commit
156c4a7eea
36
board/fen.go
36
board/fen.go
@ -1,12 +1,15 @@
|
||||
package board
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/maaslalani/gambit/piece"
|
||||
)
|
||||
|
||||
var ErrInvalidFEN = errors.New("Invalid FEN")
|
||||
|
||||
func (b Board) ToFen() string {
|
||||
var sb strings.Builder
|
||||
// Loop through the entire board and build the FEN string for each rank
|
||||
@ -30,7 +33,7 @@ func (b Board) ToFen() string {
|
||||
}
|
||||
|
||||
// Display the piece's Fen representation
|
||||
sb.WriteString(p.Fen())
|
||||
sb.WriteString(p.ToFen())
|
||||
}
|
||||
|
||||
// If we have reached the end of the rank and we have encountered
|
||||
@ -44,7 +47,7 @@ func (b Board) ToFen() string {
|
||||
}
|
||||
}
|
||||
|
||||
sb.WriteString(" w ")
|
||||
sb.WriteString(" " + string(b.Turn) + " ")
|
||||
// TODO: Add castling
|
||||
sb.WriteString("KQkq")
|
||||
sb.WriteString(" - ")
|
||||
@ -53,5 +56,32 @@ func (b Board) ToFen() string {
|
||||
}
|
||||
|
||||
func FromFen(fen string) (Board, error) {
|
||||
return Board{}, nil
|
||||
var b Board
|
||||
|
||||
parts := strings.Split(fen, " ")
|
||||
if len(parts) != 6 {
|
||||
return b, ErrInvalidFEN
|
||||
}
|
||||
|
||||
b.Turn = piece.Color(parts[1])
|
||||
|
||||
ranks := strings.Split(parts[0], "/")
|
||||
|
||||
for r, rank := range ranks {
|
||||
for c, char := range rank {
|
||||
// Empty squares, append n empty pieces into the board
|
||||
if char >= '1' && char <= '8' {
|
||||
n := int(char - '0')
|
||||
for i := 0; i < n; i++ {
|
||||
b.Grid[7-r][c+i] = piece.Empty()
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
p := piece.FromFen(string(char))
|
||||
b.Grid[7-r][c] = p
|
||||
}
|
||||
}
|
||||
|
||||
return b, nil
|
||||
}
|
||||
|
26
piece/fen.go
Normal file
26
piece/fen.go
Normal file
@ -0,0 +1,26 @@
|
||||
package piece
|
||||
|
||||
import "strings"
|
||||
|
||||
func (p Piece) ToFen() string {
|
||||
if p.Color == NoColor {
|
||||
return ""
|
||||
}
|
||||
|
||||
if p.Color == Black {
|
||||
return strings.ToLower(string(p.Type))
|
||||
}
|
||||
|
||||
return string(p.Type)
|
||||
}
|
||||
|
||||
func FromFen(fen string) Piece {
|
||||
t := Type(strings.ToUpper(fen))
|
||||
var c Color
|
||||
if strings.ToUpper(fen) == fen {
|
||||
c = White
|
||||
} else {
|
||||
c = Black
|
||||
}
|
||||
return Piece{Type: t, Color: c}
|
||||
}
|
@ -1,7 +1,5 @@
|
||||
package piece
|
||||
|
||||
import "strings"
|
||||
|
||||
type Piece struct {
|
||||
Type Type
|
||||
Color Color
|
||||
@ -15,18 +13,6 @@ func (p Piece) String() string {
|
||||
return Display[p.Type]
|
||||
}
|
||||
|
||||
func (p Piece) Fen() string {
|
||||
if p.Color == NoColor {
|
||||
return ""
|
||||
}
|
||||
|
||||
if p.Color == Black {
|
||||
return strings.ToLower(string(p.Type))
|
||||
}
|
||||
|
||||
return string(p.Type)
|
||||
}
|
||||
|
||||
var (
|
||||
BW = Piece{Bishop, White}
|
||||
KW = Piece{King, White}
|
||||
|
Loading…
Reference in New Issue
Block a user