thomas-mauran / chess-tui

A chess TUI implementation in rust 🦀
https://crates.io/crates/chess-tui
MIT License
351 stars 20 forks source link

3 fold repetition #67

Open nicholasmello opened 4 months ago

nicholasmello commented 4 months ago

Description

3 fold repetition only checks moves in a row, it should check if the position repeats regardless of order

To Reproduce

Play a game and repeat the position, outside of the same order

Expected behavior

Draw in all repetition scenarios

Environment

OS: Linux (NixOS) Terminal Emulator: Alacritty

Additional context

This function just needs to be rewritten

    pub fn draw_by_repetition(&self) -> bool {
        if self.move_history.len() >= 9 {
            let last_ten: Vec<PieceMove> =
                self.move_history.iter().rev().take(9).cloned().collect();

            if (last_ten[0].clone(), last_ten[1].clone())
                == (last_ten[4].clone(), last_ten[5].clone())
                && last_ten[4].clone() == last_ten[8].clone()
                && (last_ten[2].clone(), last_ten[3].clone())
                    == (last_ten[6].clone(), last_ten[7].clone())
            {
                return true;
            }
        }
        false
    }
thomas-mauran commented 4 months ago

Oh yes you are right ! I always thought 3 fold rep needed to be consecutive, do you want to take the pr ? else I might fix it this week, nice catch @nicholasmello

nicholasmello commented 4 months ago

I'll take a look at it if I have time, my focus is on getting #65 flushed out. If someone takes a look at this before me, one thing to remember is "Two positions are by definition "the same" if the same types of pieces occupy the same squares, the same player has the move, the remaining castling rights are the same and the possibility to capture en passant is the same". Castling rights and the possibility of en passant are the obscure rules that often get left out.