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
use piece::Piece;
use square::Square;
use std::fmt;
use std::cmp::Ordering;

/// Represent a ChessMove in memory
#[derive(Clone, Copy, Eq, PartialOrd, PartialEq, Default)]
pub struct ChessMove {
    source: Square,
    dest: Square,
    promotion: Option<Piece>
}

impl ChessMove {
    /// Create a new chess move, given a source `Square`, a destination `Square`, and an optional
    /// promotion `Piece`
    pub fn new(source: Square, dest: Square, promotion: Option<Piece>) -> ChessMove {
        ChessMove { source: source, dest: dest, promotion: promotion }
    }

    /// Get the source square (square the piece is currently on).
    pub fn get_source(&self) -> Square {
        self.source
    }

    /// Get the destination square (square the piece is going to).
    pub fn get_dest(&self) -> Square {
        self.dest
    }

    /// Get the promotion piece (maybe).
    pub fn get_promotion(&self) -> Option<Piece> {
        self.promotion
    }
}

impl fmt::Display for ChessMove {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self.promotion {
            None => write!(f, "{}-{}", self.source, self.dest),
            Some(x) => write!(f, "{}-{}={}", self.source, self.dest, x)
        }
    }
}

impl Ord for ChessMove {
    fn cmp(&self, other: &ChessMove) -> Ordering {
        if self.source != other.source {
            self.source.cmp(&other.source)
        } else if self.dest != other.dest {
            self.dest.cmp(&other.dest)
        } else if self.promotion != other.promotion {
            match self.promotion {
                None => Ordering::Less,
                Some(x) => {
                    match other.promotion {
                        None => Ordering::Greater,
                        Some(y) => x.cmp(&y)
                    }
                }
            }
        } else {
            Ordering::Equal
        }
    }
}