FEN strings are compact representations of chess board positions which allows you to derive the necessary information to start playing a chess game from that position. This includes things like the pieces on the board, castle statuses, etc. More information here: https://en.wikipedia.org/wiki/Forsyth%E2%80%93Edwards_Notation
I have written a simple function in go to decode a FEN string into separate variables with suitable types for each of the desired pieces of information that FEN strings represent, and I would like to see if I am parsing the FEN string in a reasonable manner.
import (
"strings"
)
type pair struct {
x, y int8
}
func decodeFen(fen string) ([8][8]int8, int8, [4]bool, pair, int8, int) {
split := strings.Split(fen, " ")
var board [8][8]int8
board_split := strings.Split(split[0], "/")
for r, row := range board_split {
var col_ind int8
for _, char := range row {
if char >= 49 && char <= 57 {
for i := int8(0); i < int8(char) - 48; i++ {
board[r][col_ind] = '-'
col_ind++
}
} else {
board[r][col_ind] = int8(char)
col_ind++
}
}
}
var color int8
if split[1] == "w" {
color = 1
} else {color = 2}
var castle_status [4]bool
var status_map = map[rune]int8 {
'K' : 0, 'Q' : 1, 'k' : 2, 'q' : 3}
if split[2] != "-" {
for _, char := range split[2] {
castle_status[status_map[char]] = true
}
}
var ep_pos pair = pair{-1, -1}
if split[3] != "-" {
x_label := split[3][0]
y_label := split[3][1]
y_label -= 48
ep_pos = pair{int8(x_label - 97), int8(y_label - ((y_label - 1) - (8 - y_label)) - 1)}
}
var halfmove_clock int8 = int8(split[4][0]) - 48
var fullmove_number int = int(split[5][0]) - 48
return board, color, castle_status, ep_pos, halfmove_clock, fullmove_number
}
This is really just a parsing problem, and to split the strings I decided to use the Split() function from the strings
go library, rather than writing my own. Any improvements and feedback for this code would be great, and I apologize if I am not correctly asking this question as this is my first post in this community.