aboutsummaryrefslogtreecommitdiff
path: root/src/board.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/board.rs')
-rw-r--r--src/board.rs50
1 files changed, 30 insertions, 20 deletions
diff --git a/src/board.rs b/src/board.rs
index eb8a638..578842c 100644
--- a/src/board.rs
+++ b/src/board.rs
@@ -94,7 +94,7 @@ impl fmt::Display for Action {
}
}
-type Stack = Vec<(Player, Stone)>;
+pub type Stack = Vec<(Player, Stone)>;
pub struct Board<L>
where
@@ -115,18 +115,22 @@ where
// TODO: Generate all legal actions for a given player
impl<L: Fn(String)> Board<L> {
- pub fn new(size: u8, log: L) -> Option<Board<L>> {
- if size >= 3 && size <= 8 {
- let (flats, caps) = match size {
- 3 => (10, 0),
- 4 => (15, 0),
- 5 => (21, 1),
- 6 => (30, 1),
- 7 => (40, 2),
- 8 => (50, 2),
- _ => return None,
- };
- Some(Board {
+ pub fn get_size(&self) -> u8 {
+ self.size
+ }
+
+ // Defaults to 5x5 if requested things are out of range
+ pub fn new(size: u8, log: L) -> (Board<L>, bool) {
+ let (flats, caps, correct) = match size {
+ 3 => (10, 0, true),
+ 4 => (15, 0, true),
+ 6 => (30, 1, true),
+ 7 => (40, 2, true),
+ 8 => (50, 2, true),
+ _ => (21, 1, false),
+ };
+ (
+ Board {
size: size,
black_flats: flats,
white_flats: flats,
@@ -135,19 +139,17 @@ impl<L: Fn(String)> Board<L> {
board: {
let mut v: Vec<Stack> = Vec::new();
for _i in 0..size * size {
- let t: Stack = Vec::new();
- v.push(t);
+ v.push(Vec::new());
}
v
},
log: log,
- })
- } else {
- None
- }
+ },
+ correct,
+ )
}
- fn remaining_pieces(&self, player: &Player, stone: &Stone) -> u8 {
+ pub fn remaining_pieces(&self, player: &Player, stone: &Stone) -> u8 {
match player {
Player::Black => {
if *stone == Stone::Capstone {
@@ -174,6 +176,14 @@ impl<L: Fn(String)> Board<L> {
}
}
+ pub fn query_square(&self, pos: &Position) -> Option<&Stack> {
+ if self.within_bounds(pos) {
+ self.board.get(self.pos_to_idx(pos))
+ } else {
+ None
+ }
+ }
+
fn pos_to_idx(&self, pos: &Position) -> usize {
let y: usize = pos.y as usize;
let x: usize = pos.x as usize;