aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs94
1 files changed, 64 insertions, 30 deletions
diff --git a/src/main.rs b/src/main.rs
index d664025..9ccefde 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -4,11 +4,40 @@ use pancurses::Input::*;
use pancurses::*;
mod game;
-
use crate::game::*;
-struct BoardGui {
- win: Window,
+pub struct GameStateGUI {
+ action_window: Window,
+ pieces_window: Window,
+}
+
+impl GameStateGUI {
+ pub fn new(ypos: i32, xpos: i32, height: i32, width: i32) -> GameStateGUI {
+ GameStateGUI {
+ pieces_window: newwin(2, width, ypos, xpos),
+ action_window: newwin(height, width, ypos + 3, xpos),
+ }
+ }
+
+ pub fn update<L: Fn(String)>(&self, game: &Game<L>) {
+ self.pieces_window.clear();
+ self.pieces_window.addstr(format!(
+ "W: {:2} B: {:2}\nWC: {} BC: {}",
+ game.query_pieces(Player::White, Stone::Flat),
+ game.query_pieces(Player::Black, Stone::Flat),
+ game.query_pieces(Player::White, Stone::Capstone),
+ game.query_pieces(Player::Black, Stone::Capstone),
+ ));
+ self.pieces_window.refresh();
+
+ self.action_window.clear();
+ self.action_window.addstr(game.query_action_lines());
+ self.action_window.refresh();
+ }
+}
+
+struct BoardGUI {
+ board_window: Window,
cell_windows: Vec<Window>,
size: u8,
cell_height: u8,
@@ -26,7 +55,7 @@ enum BGAction {
None,
}
-impl BoardGui {
+impl BoardGUI {
fn draw_grid(&self) {
let s: i32 = self.size as i32;
let h: i32 = self.cell_height as i32;
@@ -35,26 +64,27 @@ impl BoardGui {
for x in 0..w * s + 1 {
if y % h == 0 {
if x % w == 0 {
- self.win.mvaddch(y, x + self.hoff, '+');
+ self.board_window.mvaddch(y, x + self.hoff, '+');
} else {
- self.win.mvaddch(y, x + self.hoff, '-');
+ self.board_window.mvaddch(y, x + self.hoff, '-');
}
} else if x % w == 0 {
- self.win.mvaddch(y, x + self.hoff, '|');
+ self.board_window.mvaddch(y, x + self.hoff, '|');
}
}
}
for y in 0..s {
- self.win.mvaddstr(y * h + h / 2, 0, format!("{}.", s - y));
+ self.board_window
+ .mvaddstr(y * h + h / 2, 0, format!("{}.", s - y));
}
for x in 0..s {
- self.win.mvaddstr(
+ self.board_window.mvaddstr(
s * h + 1,
w * x + w / 2 + self.hoff,
format!("{:x}.", x + 10),
);
}
- self.win.refresh();
+ self.board_window.refresh();
}
fn pos_to_idx(&self, pos: &Position) -> usize {
@@ -107,16 +137,15 @@ impl BoardGui {
}
}
- // Is this really the best way to do this?
- fn draw_game_state(&self, game_state: &GameState) {
- // ????
- assert!(game_state.get_size() == self.size);
+ fn update<L: Fn(String)>(&self, game: &Game<L>) {
+ // Is this really the `best' way?
+ assert!(game.get_size() == self.size);
let mut pos = Position { x: 0, y: 0 };
for y in 0..self.size {
pos.y = y;
for x in 0..self.size {
pos.x = x;
- if let Some(stack) = game_state.query_square(&pos) {
+ if let Some(stack) = game.query_square(&pos) {
self.draw_stack(&pos, stack);
}
}
@@ -155,8 +184,8 @@ impl BoardGui {
}
Character('q') => BGAction::Quit,
x => {
- self.win.addstr(format!("{:?}", x));
- self.win.refresh();
+ self.board_window.addstr(format!("{:?}", x));
+ self.board_window.refresh();
BGAction::None
}
}
@@ -172,7 +201,7 @@ impl BoardGui {
has_colours: bool,
white_colour: u8,
black_colour: u8,
- ) -> BoardGui {
+ ) -> BoardGUI {
let mut cell_windows: Vec<Window> = Vec::new();
let hoff = hoff as i32;
@@ -185,8 +214,8 @@ impl BoardGui {
cell_windows.push(newwin(h - 1, w - 1, (s - y - 1) * h + 1, hoff + x * w + 1));
}
- let bg = BoardGui {
- win: newwin(h * s + 3, w * s + 3 + hoff, ypos, xpos),
+ let bg = BoardGUI {
+ board_window: newwin(h * s + 3, w * s + 3 + hoff, ypos, xpos),
cell_windows: cell_windows,
size: size,
cell_height: cell_height,
@@ -210,7 +239,7 @@ impl BoardGui {
// }
bg.mv_to_cell(&bg.cur_cell, 0);
- bg.win.refresh();
+ bg.board_window.refresh();
bg
}
}
@@ -226,9 +255,9 @@ impl MoveEntry {
self.win.refresh();
}
- fn read_action(&self) -> &str {
- "a1"
- }
+ // fn read_action(&self) -> &str {
+ // "a1"
+ // }
}
struct MessageLog {
@@ -272,26 +301,31 @@ fn main() {
screen.refresh();
+ let mut bg = BoardGUI::new(0, 0, 5, 4, 8, 3, has_colours, white_colour, black_colour);
+ let gs = GameStateGUI::new(0, 5 * 8 + 3 + 3, 100, 20);
+
let mew = newwin(1, 30, 25, 0);
let mlw = newwin(3, 70, 26, 0);
-
- let mut bg = BoardGui::new(0, 0, 5, 4, 8, 3, has_colours, white_colour, black_colour);
-
let me = MoveEntry { win: mew };
-
let ml = MessageLog {
win: mlw,
has_colours: has_colours,
red_colour: red_colour,
};
-
me.init();
ml.init();
let log = |s| MessageLog::log_error(&ml, s);
- let game_state = GameState::new(5);
+ let mut game = Game::new(5, "tslil", "taktician", log);
+
let pos = Position { x: 0, y: 0 };
let action = Action::Place(Player::White, pos, Stone::Flat);
+
+ game.perform_action(action);
+
+ bg.update(&game);
+ gs.update(&game);
+
// 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);