1
0
mirror of https://github.com/maaslalani/gambit.git synced 2024-11-24 08:22:12 +02:00
This commit is contained in:
Maas Lalani 2021-12-24 16:38:25 -05:00
parent 51632cd1e8
commit 20709692d9
No known key found for this signature in database
GPG Key ID: 5A6ED5CBF1A0A000
2 changed files with 48 additions and 1 deletions

46
fen/fen.go Normal file
View File

@ -0,0 +1,46 @@
package fen
import (
"fmt"
"strings"
)
// Tokens returns the tokens of a FEN string
func Tokens(fen string) []string {
return strings.Split(fen, " ")
}
// Ranks returns a slice of ranks from the FEN string
func Ranks(fen string) []string {
return strings.Split(Tokens(fen)[0], "/")
}
// Grid returns a 8x8 grid of the board represented by the FEN string
func Grid(fen string) [8][8]string {
var grid [8][8]string
for r, rank := range Ranks(fen) {
var row [8]string
c := 0
for _, col := range rank {
skip := 1
if isNumeric(col) {
skip = runeToInt(col)
} else {
row[c] = fmt.Sprintf("%c", col)
}
c += skip
}
grid[r] = row
}
return grid
}
// isNumeric returns true if the current rune is a number
func isNumeric(r rune) bool {
return r >= '0' && r <= '9'
}
// runeToInt converts a rune to an int
func runeToInt(r rune) int {
return int(r - '0')
}

View File

@ -6,6 +6,7 @@ import (
tea "github.com/charmbracelet/bubbletea"
dt "github.com/dylhunn/dragontoothmg"
"github.com/maaslalani/gambit/board"
"github.com/maaslalani/gambit/border"
"github.com/maaslalani/gambit/fen"
@ -65,8 +66,8 @@ func (m model) View() string {
}
s.WriteString(display + border.Vertical)
}
s.WriteString("\n")
if r != board.LastRow {
s.WriteString(border.Middle())
} else {