niklasf / shakmaty

A Rust library for chess and chess variant rules and operations
https://docs.rs/shakmaty
GNU General Public License v3.0
210 stars 41 forks source link

Mirror boards #74

Closed mlomb closed 10 months ago

mlomb commented 10 months ago

Hi, I'm looking for a way to mirror boards. For example, with python-chess I can do .mirror():

import chess
board = chess.Board(fen="6k1/pp3pp1/4b2p/4N3/2p5/2q3P1/P4PKP/3r4 w - - 0 1")
print(board.mirror().fen()) # 3R4/p4pkp/2Q3p1/2P5/4n3/4B2P/PP3PP1/6K1 b - - 0 1

fens

Which flips vertically and swaps colors.

I tried the same thing with shakmaty but only managed to flip the board, and can't find a way to swap colors:

let fen: Fen = "6k1/pp3pp1/4b2p/4N3/2p5/2q3P1/P4PKP/3r4 w - - 0 1"
    .parse()
    .unwrap();
let pos: Chess = fen.into_position(CastlingMode::Standard).unwrap();
let mut board = pos.board().clone();
board.flip_vertical();
println!("{}", board); // 3r4/P4PKP/2q3P1/2p5/4N3/4b2p/pp3pp1/6k1

image

How could I do it? Can it be added to shakmaty?


I'm looking for this because I'm working with ML and need the board to always be from the POV of white

Thanks!

niklasf commented 10 months ago

Hi. Sure, added Board::swap_colors().