summaryrefslogtreecommitdiff
path: root/src/board.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/board.rs')
-rw-r--r--src/board.rs56
1 files changed, 40 insertions, 16 deletions
diff --git a/src/board.rs b/src/board.rs
index 45c0d49..626dcb0 100644
--- a/src/board.rs
+++ b/src/board.rs
@@ -56,6 +56,7 @@ pub enum Direction {
Left,
Right,
}
+
impl fmt::Display for Direction {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
@@ -71,16 +72,16 @@ impl fmt::Display for Direction {
}
}
-pub enum Move {
+pub enum Action {
Place(Player, Position, Stone),
- Slide(Player, Position, Direction, Vec<u8>),
+ Move(Player, Position, Direction, Vec<u8>),
}
-impl fmt::Display for Move {
+impl fmt::Display for Action {
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!(
+ Action::Place(player, pos, stone) => write!(f, "{}{}{}", pos, player, stone),
+ Action::Move(_, pos, direction, drops) => write!(
f,
"{}{}{}",
pos,
@@ -91,7 +92,7 @@ impl fmt::Display for Move {
}
}
-type Stack = Vec<(Stone, Player)>;
+type Stack = Vec<(Player, Stone)>;
pub struct Board {
size: u16,
@@ -100,16 +101,14 @@ pub struct Board {
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)
+ let y: u16 = pos.y as u16;
+ let x: u16 = pos.x as u16;
+ self.board.get((x + y * self.size) as usize)
}
- fn is_legal_move(&self, mov: &Move) -> bool {
+ fn is_legal_move(&self, mov: &Action) -> bool {
match mov {
- Move::Place(_, pos, _) => match self.lookup(pos) {
+ Action::Place(_, pos, _) => match self.lookup(pos) {
Some(stack) => {
if stack.len() == 0 {
true
@@ -119,11 +118,36 @@ impl Board {
}
None => false,
},
- Move::Slide(player, pos, direction, drops) => match self.lookup(pos) {
+ Action::Move(player, pos, direction, drops) => match self.lookup(pos) {
Some(stack) => {
- if stack.len() == 0 {
- true
+ /* Rules for moving a stack:
+ 0. There are stones
+ 1. Top stone belongs to player
+ 2. Zero or One stones dropped on starting square
+ 3. Total number of stones moved does not exceed the carry capacity
+ 4. Direction does not contain a capstone
+ 5. Wall may only appear on last spot if it's capstone alone that covers
+ 6. All stones are used up before then end of the board is met
+
+ We check 0,1,2, and 3 first
+ */
+ if (stack.len() == 0)
+ || (drops.len() == 0)
+ || (stack[0].0 != *player)
+ || (drops[0] > 1)
+ || (drops.iter().sum::<u8>() as u16 > self.size)
+ {
+ false
} else {
+ let steps: usize = drops.len() - 1;
+ let cap: bool = stack[0].1 == Stone::Capstone;
+ let (dy, dx): (i8, i8) = match direction {
+ Direction::Up => (1, 0),
+ Direction::Down => (-1, 0),
+ Direction::Left => (0, -1),
+ Direction::Right => (0, 1),
+ };
+
false
}
}