summaryrefslogtreecommitdiff
path: root/src/board.rs
blob: 45c0d4975f67485193e41d906bbb4a3c1eb63444 (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
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
125
126
127
128
129
130
131
132
133
134
use std::fmt;

#[derive(PartialEq)]
pub enum Player {
    Black,
    White,
}

impl fmt::Display for Player {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "{}",
            match self {
                Player::Black => "B",
                Player::White => "W",
            }
        )
    }
}

#[derive(PartialEq)]
pub enum Stone {
    Flat,
    Standing,
    Capstone,
}

impl fmt::Display for Stone {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "{}",
            match self {
                Stone::Flat => "",
                Stone::Standing => "S",
                Stone::Capstone => "C",
            }
        )
    }
}

pub struct Position {
    pub x: u8,
    pub y: u8,
}
impl fmt::Display for Position {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:x}{}", 10 + self.x, self.y)
    }
}

pub enum Direction {
    Up,
    Down,
    Left,
    Right,
}
impl fmt::Display for Direction {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "{}",
            match self {
                Direction::Up => "+",
                Direction::Down => "-",
                Direction::Left => "<",
                Direction::Right => ">",
            }
        )
    }
}

pub enum Move {
    Place(Player, Position, Stone),
    Slide(Player, Position, Direction, Vec<u8>),
}

impl fmt::Display for Move {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Move::Place(player, pos, stone) => write!(f, "{}{}{}", pos, player, stone),
            Move::Slide(_, pos, direction, drops) => write!(
                f,
                "{}{}{}",
                pos,
                direction,
                drops.into_iter().map(|q| q.to_string()).collect::<String>()
            ),
        }
    }
}

type Stack = Vec<(Stone, Player)>;

pub struct Board {
    size: u16,
    board: Vec<Stack>,
}

impl Board {
    fn lookup(&self, pos: &Position) -> Option<&Stack> {
        let y: u16 = pos.y.into();
        let x: u16 = pos.x.into();
        // Really?
        let idx: usize = (x + y * self.size).into();
        self.board.get(idx)
    }

    fn is_legal_move(&self, mov: &Move) -> bool {
        match mov {
            Move::Place(_, pos, _) => match self.lookup(pos) {
                Some(stack) => {
                    if stack.len() == 0 {
                        true
                    } else {
                        false
                    }
                }
                None => false,
            },
            Move::Slide(player, pos, direction, drops) => match self.lookup(pos) {
                Some(stack) => {
                    if stack.len() == 0 {
                        true
                    } else {
                        false
                    }
                }
                None => false,
            },
        }
    }
}