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
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
use crate::error::ShogiUtilError::CsaParseError;
use crate::piece::Piece;
use crate::{Result, ShogiUtilError};
use std::str::FromStr;

#[derive(Debug, Eq, PartialEq, Copy, Clone)]
pub enum Color {
    Black,
    White,
}

impl Color {
    pub fn to_byte(&self) -> u8 {
        match self {
            Color::Black => 0,
            Color::White => 1,
        }
    }
    pub fn to_usize(&self) -> usize {
        self.to_byte() as usize
    }

    pub fn opponent(&self) -> Color {
        match self {
            Color::White => Color::Black,
            Color::Black => Color::White,
        }
    }
}

impl FromStr for Color {
    type Err = ShogiUtilError;

    fn from_str(s: &str) -> Result<Self> {
        match s {
            "+" => Ok(Color::Black),
            "-" => Ok(Color::White),
            _ => Err(CsaParseError(format!("Invalid color symbol: {}", s))),
        }
    }
}

#[derive(Debug, Eq, PartialEq, Clone)]
pub struct Square {
    pub file: u8,
    pub rank: u8,
}

impl Square {
    pub fn from_pos(i: usize, j: usize) -> Square {
        assert!(i < 9 && j < 9);
        Square {
            rank: (i + 1) as u8,
            file: (9 - j) as u8,
        }
    }
    pub fn to_pos(&self) -> (usize, usize) {
        assert!(self.is_valid());
        let i = self.rank as usize - 1;
        let j = 9 - self.file as usize;
        (i, j)
    }

    pub fn is_valid(&self) -> bool {
        1 <= self.rank && self.rank <= 9 && 1 <= self.file && self.file <= 9
    }
    pub fn rotate(&self) -> Self {
        Self {
            rank: 9 - self.rank + 1,
            file: 9 - self.file + 1,
        }
    }
}

impl FromStr for Square {
    type Err = ShogiUtilError;

    fn from_str(s: &str) -> Result<Self> {
        if s.len() != 2 {
            Err(CsaParseError(format!("Invalid square: {}", s)))
        } else {
            let file = s[0..1].parse::<u8>();
            let rank = s[1..2].parse::<u8>();
            match (file, rank) {
                (Ok(file), Ok(rank)) if file > 0 && rank > 0 => Ok(Square { file, rank }),
                _ => Err(CsaParseError(format!("Invalid square {}", s))),
            }
        }
    }
}

#[derive(Debug, Eq, PartialEq, Clone)]
pub struct Move {
    pub color: Color,
    pub from: Option<Square>,
    pub to: Square,
    pub piece: Piece,
}

pub struct LegalMove {
    pub mv: Move,
    pub promoted: bool,
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn test_square() {
        let sq = Square { file: 2, rank: 3 };
        assert_eq!(sq.to_pos(), (2, 7));

        let sq = Square::from_pos(2, 7);
        assert_eq!(Square { file: 2, rank: 3 }, sq);
    }

    #[test]
    fn test_rotate_square() {
        let sq = Square { file: 2, rank: 7 };
        let rotated = sq.rotate();
        assert_eq!(rotated.file, 8);
        assert_eq!(rotated.rank, 3);
    }
}