diff options
| author | tslil clingman <> | 2020-01-19 15:44:43 -0800 |
|---|---|---|
| committer | tslil clingman <> | 2020-01-19 15:44:43 -0800 |
| commit | 4d18fad08c185e07d030f6574963ec6de8441ee5 (patch) | |
| tree | ffec38b939318c2adbad7a9628efbfe0571633f7 /src | |
| parent | 3b9c74fa818026b7489a1ad0f8e122f9388d739a (diff) | |
reading string action entries works
Diffstat (limited to 'src')
| -rw-r--r-- | src/game.rs | 18 | ||||
| -rw-r--r-- | src/main.rs | 135 |
2 files changed, 102 insertions, 51 deletions
diff --git a/src/game.rs b/src/game.rs index e05a807..931a15f 100644 --- a/src/game.rs +++ b/src/game.rs @@ -188,12 +188,30 @@ impl GameState { // Unconditional and so could lead to invalid state, but private fn place_stone(&self, player: Player, pos: &Position, stone: Stone) -> GameState { + // Place stone let mut copy = self.copy(); let idx = self.pos_to_idx(&pos); copy.board[idx].push(Piece { player: player, stone: stone, }); + // Decrease count + match player { + Player::Black => { + if stone == Stone::Capstone { + copy.black_capstones -= 1; + } else { + copy.black_flats -= 1; + } + } + Player::White => { + if stone == Stone::Capstone { + copy.white_capstones -= 1; + } else { + copy.white_flats -= 1; + } + } + } copy } diff --git a/src/main.rs b/src/main.rs index fd946fc..043702a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -152,41 +152,41 @@ impl BoardGUI { } } - pub fn handle_key(&mut self, key: Input) -> BGAction { - match key { - KeyDown => { - if 0 < self.cur_cell.y { - self.cur_cell.y -= 1; - self.mv_to_cell(&self.cur_cell, 0); - } - BGAction::None - } - KeyUp => { - if self.cur_cell.y + 1 < self.size { - self.cur_cell.y += 1; - self.mv_to_cell(&self.cur_cell, 0); - } - BGAction::None - } - KeyLeft => { - if 0 < self.cur_cell.x { - self.cur_cell.x -= 1; - self.mv_to_cell(&self.cur_cell, 0); - } - BGAction::None - } - KeyRight => { - if self.cur_cell.x + 1 < self.size { - self.cur_cell.x += 1; - self.mv_to_cell(&self.cur_cell, 0); + fn read_action(&mut self) -> BGAction { + loop { + if let Some(key) = self.board_window.getch() { + match key { + KeyDown => { + if 0 < self.cur_cell.y { + self.cur_cell.y -= 1; + self.mv_to_cell(&self.cur_cell, 0); + } + } + KeyUp => { + if self.cur_cell.y + 1 < self.size { + self.cur_cell.y += 1; + self.mv_to_cell(&self.cur_cell, 0); + } + } + KeyLeft => { + if 0 < self.cur_cell.x { + self.cur_cell.x -= 1; + self.mv_to_cell(&self.cur_cell, 0); + } + } + KeyRight => { + if self.cur_cell.x + 1 < self.size { + self.cur_cell.x += 1; + self.mv_to_cell(&self.cur_cell, 0); + } + } + Character('q') => return BGAction::Quit, + + x => { + self.board_window.addstr(format!("{:?}", x)); + self.board_window.refresh(); + } } - BGAction::None - } - Character('q') => BGAction::Quit, - x => { - self.board_window.addstr(format!("{:?}", x)); - self.board_window.refresh(); - BGAction::None } } } @@ -227,6 +227,9 @@ impl BoardGUI { white_colour: white_colour, }; + bg.board_window.keypad(true); + bg.board_window.nodelay(false); + bg.draw_grid(); // for i in 0..s * s { @@ -246,30 +249,66 @@ impl BoardGUI { pub struct ActionEntry { window: Window, + max_len: usize, + xstart: i32, } impl ActionEntry { - pub fn new(ypos: i32, xpos: i32, height: i32, width: i32) -> ActionEntry { + pub fn new(ypos: i32, xpos: i32, max_len: usize) -> ActionEntry { + let txt = "Action entry: "; + let xstart = txt.len() as i32; + let width = max_len as i32 + xstart + 1; + let ae = ActionEntry { - window: newwin(height, width, ypos, xpos), + window: newwin(1, width, ypos, xpos), + max_len: max_len, + xstart: xstart, }; - ae.window.addstr("Enter action: "); - ae.window.attrset(Attribute::Reverse); + + ae.window.nodelay(false); + ae.window.keypad(true); + + ae.window.addstr(txt); + + for _i in 0..max_len { + ae.window.addch('_'); + } + + ae.window.mv(0, xstart); ae.window.refresh(); ae } fn read_action(&self) -> String { + self.window.mv(0, self.xstart); + self.window.refresh(); + let mut result = String::new(); loop { if let Some(inp) = self.window.getch() { match inp { - Character(c) => result.push(c), - _ => break, + Character('\n') => return result, + KeyEnter => return result, // TODO: is this ever used? + Character(c) => { + if result.len() < self.max_len { + result.push(c); + self.window.addch(c); + } + } + KeyBackspace => { + if !result.is_empty() { + result.pop(); + let (y, x) = (self.window.get_cur_y(), self.window.get_cur_x()); + self.window.mv(y, x - 1); + self.window.addch('_'); + self.window.mv(y, x - 1); + } + } + _ => (), }; } + self.window.refresh(); } - result } } @@ -304,8 +343,6 @@ impl MessageLog { fn main() { let screen = initscr(); - // screen.nodelay(true); - screen.keypad(true); cbreak(); noecho(); curs_set(2); @@ -326,7 +363,7 @@ fn main() { let mut board_gui = BoardGUI::new(0, 0, 5, 4, 8, 3, has_colours, white_colour, black_colour); let game_log = GameLog::new(0, 5 * 8 + 3 + 3, 100, 20); let message_log = MessageLog::new(26, 0, 3, 70, has_colours, red_colour); - let action_entry = ActionEntry::new(25, 0, 1, 30); + let action_entry = ActionEntry::new(25, 0, 10); let log = |s| message_log.log_error(s); let mut game = Game::new(5, "tslil", "taktician", log); @@ -339,16 +376,12 @@ fn main() { board_gui.update(&game); game_log.update(&game); + log(action_entry.read_action()); + // game_state.perform_action(&action); // game_state.is_legal_action(&Action::Move(Player::White, pos, Direction::Up, vec![0, 1])); // bg.draw_game_state(&game_state); - loop { - if let Some(inp) = screen.getch() { - if board_gui.handle_key(inp) == BGAction::Quit { - break; - } - } - } + board_gui.read_action(); endwin(); } |
