summaryrefslogtreecommitdiffstats
path: root/src/board.rs
blob: 1396ffc68a98b51213375e4e60508bdab69d9f84 (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// -*- coding: utf-8 -*-
//
// Copyright 2021 Michael Buesch <m@bues.ch>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//

use crate::coord::{Coord, CoordAxis};
use crate::coord;

pub const BOARD_WIDTH: CoordAxis    = 5;
pub const BOARD_HEIGHT: CoordAxis   = 7;

pub const BOARD_LINES: [(Coord, Coord); 15] = [
    // Vertical lines
    (coord!(0, 2), coord!(0, 6)),
    (coord!(1, 1), coord!(1, 6)),
    (coord!(2, 0), coord!(2, 6)),
    (coord!(3, 1), coord!(3, 6)),
    (coord!(4, 2), coord!(4, 6)),
    // Horizontal lines
    (coord!(1, 1), coord!(3, 1)),
    (coord!(0, 2), coord!(4, 2)),
    (coord!(0, 3), coord!(4, 3)),
    (coord!(0, 4), coord!(4, 4)),
    (coord!(0, 5), coord!(4, 5)),
    (coord!(0, 6), coord!(4, 6)),
    // Diagonal lines (center)
    (coord!(0, 2), coord!(4, 6)),
    (coord!(0, 6), coord!(4, 2)),
    // Diagonal lines (top)
    (coord!(1, 1), coord!(2, 0)),
    (coord!(2, 0), coord!(3, 1)),
];

#[derive(Copy, Clone, PartialEq, Debug)]
pub enum PosType {
    Invalid,
    Barn,
    Field,
}

macro_rules! invalid { () => { PosType::Invalid } }
macro_rules! barn { () => { PosType::Barn } }
macro_rules! field { () => { PosType::Field } }

pub const BOARD_POSITIONS: [[PosType; BOARD_WIDTH as usize]; BOARD_HEIGHT as usize] = [
    [ invalid!(), invalid!(), barn!(),  invalid!(), invalid!(), ],
    [ invalid!(), barn!(),    barn!(),  barn!(),    invalid!(), ],
    [ field!(),   barn!(),    barn!(),  barn!(),    field!(),   ],
    [ field!(),   field!(),   field!(), field!(),   field!(),   ],
    [ field!(),   field!(),   field!(), field!(),   field!(),   ],
    [ field!(),   field!(),   field!(), field!(),   field!(),   ],
    [ field!(),   field!(),   field!(), field!(),   field!(),   ],
];

/// Check if a position is on the board.
pub fn coord_is_on_board(pos: Coord) -> bool {
    pos.x >= 0 && pos.x < BOARD_WIDTH &&
    pos.y >= 0 && pos.y < BOARD_HEIGHT &&
    BOARD_POSITIONS[pos.y as usize][pos.x as usize] != PosType::Invalid
}

/// Check if a position is on the main diagonal lines.
pub fn is_on_main_diag(pos: Coord) -> bool {
    let mut x = 0;
    let mut y = 2;
    for _ in 0..5 {
        if pos == coord!(x, y) {
            return true;
        }
        x += 1;
        y += 1;
    }
    let mut x = 0;
    let mut y = 6;
    for _ in 0..5 {
        if pos == coord!(x, y) {
            return true;
        }
        x += 1;
        y -= 1;
    }
    false
}

pub struct BoardIterator {
    x: CoordAxis,
    y: CoordAxis,
}

impl BoardIterator {
    pub fn new() -> BoardIterator {
        BoardIterator {
            x: 0,
            y: 0,
        }
    }
}

impl Iterator for BoardIterator {
    type Item = Coord;

    fn next(&mut self) -> Option<Self::Item> {
        loop {
            let mut coord = Some(coord!(self.x, self.y));
            if self.x >= BOARD_WIDTH - 1 {
                self.x = 0;
                if self.y >= BOARD_HEIGHT {
                    self.y = 0;
                    coord = None;
                } else {
                    self.y += 1;
                }
            } else {
                self.x += 1;
            }

            match coord {
                None => break None,
                Some(coord) => {
                    if coord_is_on_board(coord) {
                        break Some(coord);
                    }
                },
            }
        }
    }
}

pub struct BoardPosIterator {
    iter: BoardIterator,
}

impl BoardPosIterator {
    pub fn new() -> BoardPosIterator {
        BoardPosIterator {
            iter: BoardIterator::new(),
        }
    }
}

impl Iterator for BoardPosIterator {
    type Item = (Coord, PosType);

    fn next(&mut self) -> Option<Self::Item> {
        match self.iter.next() {
            Some(coord) =>
                Some((coord, BOARD_POSITIONS[coord.y as usize][coord.x as usize])),
            None => None,
        }
    }
}

// vim: ts=4 sw=4 expandtab
bues.ch cgit interface