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
use bevy::prelude::Component;

use crate::chess_board::BoardPosition;

use super::{Piece, PieceColor, PieceType};

#[derive(Component, Clone, Debug)]
pub(super) struct Rook {
    color: PieceColor,
    starting_position: BoardPosition,
    position: BoardPosition,
}

impl Rook {
    pub(super) fn new(position: BoardPosition, color: PieceColor) -> Box<Self> {
        Box::new(Rook {
            color,
            starting_position: position,
            position,
        })
    }
}

impl Piece for Rook {
    fn get_type(&self) -> &PieceType {
        &PieceType::Rook
    }

    fn get_color(&self) -> &PieceColor {
        &self.color
    }

    fn get_position(&self) -> &BoardPosition {
        &self.position
    }

    fn set_position(&mut self, new_position: &BoardPosition) {
        self.position = *new_position;
    }

    fn get_moves(&self, _include_captures: &bool) -> Vec<BoardPosition> {
        let mut moves = Vec::new();
        for rank in 0..8 {
            for file in 0..8 {
                if (rank == self.position.rank || file == self.position.file)
                    && (rank != self.position.rank || file != self.position.file)
                {
                    moves.push(BoardPosition::new(rank, file));
                }
            }
        }
        moves
    }

    fn is_sliding(&self) -> bool {
        true
    }

    fn get_starting_position(&self) -> &BoardPosition {
        &self.starting_position
    }

    fn valid_move(&self, end_position: &BoardPosition) -> bool {
        let valid_moves = self.get_moves(&false);
        valid_moves.contains(end_position)
    }

    fn valid_capture(&self, end_position: &BoardPosition) -> bool {
        self.valid_move(end_position)
    }
}