diff options
| author | tslil clingman <> | 2020-01-11 23:07:13 -0500 |
|---|---|---|
| committer | tslil clingman <> | 2020-01-11 23:07:13 -0500 |
| commit | 258e083264971acd3ef7ace721083972954f45e1 (patch) | |
| tree | 7fcba449dc83137d4768e1df8631e2969bdc5601 | |
| parent | 46e1af1e694f066fd446f7d48b876f6217463818 (diff) | |
Working on checking validity of stack moves
| -rw-r--r-- | src/board.rs | 56 | ||||
| -rw-r--r-- | src/main.rs | 112 |
2 files changed, 102 insertions, 66 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 } } diff --git a/src/main.rs b/src/main.rs index 82b38af..039ea9f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,66 +1,71 @@ extern crate pancurses; -use pancurses::*; use pancurses::Input::*; +use pancurses::*; mod board; use crate::board::*; struct BoardGui { - win : Window, - size : i32, - cell_height : i32, - cell_width : i32, - hoff : i32, - cur_cell : Position + win: Window, + size: i32, + cell_height: i32, + cell_width: i32, + hoff: i32, + cur_cell: Position, } #[derive(PartialEq)] -enum BGAction { Quit , None } - -use std::convert::*; +enum BGAction { + Quit, + None, +} impl BoardGui { - fn mv_to_cell(&self, pos : &Position) { - let y:i32 = pos.y.into(); - let x:i32 = pos.x.into(); + fn mv_to_cell(&self, pos: &Position) { + let y: i32 = pos.y as i32; + let x: i32 = pos.x as i32; if (y < self.size) && (x < self.size) { - let y = y*self.cell_height+1; - let x = self.hoff + x*self.cell_width+1; - self.win.mv(y,x); + let y = y * self.cell_height + 1; + let x = self.hoff + x * self.cell_width + 1; + self.win.mv(y, x); self.win.refresh(); } } fn draw_grid(&self) { - for y in 0..self.cell_height*self.size+1 { - for x in 0..self.cell_width*self.size+1 { + for y in 0..self.cell_height * self.size + 1 { + for x in 0..self.cell_width * self.size + 1 { if y % self.cell_height == 0 { if x % self.cell_width == 0 { - self.win.mvaddch(y,x+self.hoff,'+'); + self.win.mvaddch(y, x + self.hoff, '+'); } else { - self.win.mvaddch(y,x+self.hoff,'-'); + self.win.mvaddch(y, x + self.hoff, '-'); } } else if x % self.cell_width == 0 { - self.win.mvaddch(y,x+self.hoff,'|'); + self.win.mvaddch(y, x + self.hoff, '|'); } } } for y in 0..self.size { - self.win.mvaddstr(y*self.cell_height+self.cell_height/2, - 0, - format!("{}.",self.size-y)); + self.win.mvaddstr( + y * self.cell_height + self.cell_height / 2, + 0, + format!("{}.", self.size - y), + ); } for x in 0..self.size { - self.win.mvaddstr(self.size*self.cell_height+1, - self.cell_width*x+self.cell_width/2+self.hoff, - format!("{:x}.",x+10)); + self.win.mvaddstr( + self.size * self.cell_height + 1, + self.cell_width * x + self.cell_width / 2 + self.hoff, + format!("{:x}.", x + 10), + ); } self.win.refresh(); } - fn handle_key(&mut self, key : Input) -> BGAction { + fn handle_key(&mut self, key: Input) -> BGAction { match key { KeyUp => { if 0 < self.cur_cell.y { @@ -68,33 +73,33 @@ impl BoardGui { self.mv_to_cell(&self.cur_cell); } BGAction::None - }, + } KeyDown => { - let y:i32 = self.cur_cell.y.into(); + let y: i32 = self.cur_cell.y as i32; if y + 1 < self.size { self.cur_cell.y += 1; self.mv_to_cell(&self.cur_cell); } BGAction::None - }, + } KeyLeft => { if 0 < self.cur_cell.x { self.cur_cell.x -= 1; self.mv_to_cell(&self.cur_cell); - } - BGAction::None - }, + } + BGAction::None + } KeyRight => { - let x:i32 = self.cur_cell.x.into(); - if x + 1< self.size { + let x: i32 = self.cur_cell.x as i32; + if x + 1 < self.size { self.cur_cell.x += 1; self.mv_to_cell(&self.cur_cell); } BGAction::None - }, + } Character('q') => BGAction::Quit, x => { - self.win.addstr(format!("{:?}",x)); + self.win.addstr(format!("{:?}", x)); self.win.refresh(); BGAction::None } @@ -108,14 +113,13 @@ impl BoardGui { } struct MoveEntry { - win : Window, + win: Window, } impl MoveEntry { - fn init(&self) { - self.win.attrset(COLOR_PAIR(3)|A_BOLD); // ???? - self.win.mvaddstr(0,0,"Enter move: ashtsaht"); + self.win.attrset(COLOR_PAIR(3) | A_BOLD); // ???? + self.win.mvaddstr(0, 0, "Enter move: ashtsaht"); // self.win.mvchgat(0,12,10,A_NORMAL,COLOR_PAIR(1)); self.win.refresh(); } @@ -126,7 +130,9 @@ fn main() { screen.nodelay(true); screen.keypad(true); - if has_colors() { start_color(); } + if has_colors() { + start_color(); + } cbreak(); noecho(); @@ -135,22 +141,28 @@ fn main() { screen.refresh(); - let bgw = newwin(25,40,0,0); - let mew = newwin(1,30,25,0); + let bgw = newwin(25, 40, 0, 0); + let mew = newwin(1, 30, 25, 0); - let mut bg = BoardGui { win : bgw, size : 5, cell_height : 4, - cell_width : 7, hoff : 3, - cur_cell : Position { x : 0, y : 0 } + let mut bg = BoardGui { + win: bgw, + size: 5, + cell_height: 4, + cell_width: 7, + hoff: 3, + cur_cell: Position { x: 0, y: 0 }, }; - let me = MoveEntry { win : mew }; + let me = MoveEntry { win: mew }; bg.init(); me.init(); loop { if let Some(inp) = screen.getch() { - if bg.handle_key(inp) == BGAction::Quit {break} + if bg.handle_key(inp) == BGAction::Quit { + break; + } } } endwin(); |
