aboutsummaryrefslogtreecommitdiff
path: root/src/moves.rs
blob: 5dc769c0c790a77b9823fa0f9082c5fb52442d75 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
use std::{fmt::Display, str::Chars};

use crate::{bitboard::BitboardFns, board::piece::Piece, square::Square};

#[derive(Debug, Clone, PartialEq, Eq, Copy)]
pub enum MoveKind {
    Quiet,
    Capture,
    Castle,
    EnPassant,
    DoublePush,
    Promotion(Piece),
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Move {
    pub source: Square,
    pub target: Square,
    pub kind: MoveKind,
}

impl Move {
    pub fn print(&self) {
        let bb = self.source.to_bitboard() | self.target.to_bitboard();
        bb.print(format!("{:?}", self).as_str());
    }

    /// Tactical move is a move that changes material score
    pub fn is_tactical(&self) -> bool {
        matches!(
            self.kind,
            MoveKind::Capture | MoveKind::EnPassant | MoveKind::Promotion(_)
        )
    }

    pub fn from_notation(mut notation: Chars) -> Self {
        let source = Square::from_notation(&mut notation).unwrap();
        let target = Square::from_notation(&mut notation).unwrap();

        if let Some(promotion) = notation.next() {
            let piece = match promotion {
                'r' => Piece::Rook,
                'q' => Piece::Queen,
                'b' => Piece::Bishop,
                'n' => Piece::Knight,
                _ => panic!("Illegal promotion piece: {:?}", promotion),
            };
            return Move {
                source,
                target,
                kind: MoveKind::Promotion(piece),
            };
        }
        Move {
            source,
            target,
            kind: MoveKind::Quiet,
        }
    }
}

impl Display for Move {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let promotion_postfix = match self.kind {
            MoveKind::Promotion(piece) => match piece.without_color() {
                Piece::Rook => "r",
                Piece::Queen => "q",
                Piece::Bishop => "b",
                Piece::Knight => "n",
                _ => panic!("Illegal promotion piece: {:?}", piece),
            },
            _ => "",
        };
        write!(f, "{}{}{}", self.source, self.target, promotion_postfix)
    }
}