Skip to content

Commit

Permalink
refactor: creating the game struct
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Mauran <[email protected]>
  • Loading branch information
thomas-mauran committed Nov 23, 2024
1 parent 356b249 commit 1943b31
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 59 deletions.
37 changes: 18 additions & 19 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use toml::Value;

use crate::{
constants::{DisplayMode, Pages},
game::board::Board,
game::{board::Board, game::Game},
pieces::PieceColor,
};
use std::{
Expand All @@ -20,7 +20,7 @@ pub struct App {
/// Is the application running?
pub running: bool,
/// board
pub board: Board,
pub game: Game,
/// Current page to render
pub current_page: Pages,
/// Used to show the help popup during the game or in the home menu
Expand All @@ -39,7 +39,7 @@ impl Default for App {
fn default() -> Self {
Self {
running: true,
board: Board::default(),
game: Game::new(),
current_page: Pages::Home,
show_help_popup: false,
show_color_popup: false,
Expand All @@ -64,7 +64,6 @@ impl App {

pub fn go_to_home(&mut self) {
self.current_page = Pages::Home;
self.board = Board::default();
}

/// Handles the tick event of the terminal.
Expand Down Expand Up @@ -117,25 +116,25 @@ impl App {
// if the selected Color is Black, we need to switch the board
if let Some(color) = self.selected_color {
if color == PieceColor::Black {
self.board.is_bot_starting = true;
self.board.bot_move();
self.board.player_turn = PieceColor::Black;
self.game.board.is_bot_starting = true;
self.game.board.bot_move();
self.game.board.player_turn = PieceColor::Black;
}
}
}

pub fn restart(&mut self) {
if self.board.is_draw || self.board.is_checkmate {
let is_bot_starting = self.board.is_bot_starting;
let engine = self.board.engine.clone();
let game_is_against_bot = self.board.is_game_against_bot;
self.board = Board::default();
self.board.engine = engine;
self.board.is_game_against_bot = game_is_against_bot;
if self.game.board.is_draw || self.game.board.is_checkmate {
let is_bot_starting = self.game.board.is_bot_starting;
let engine = self.game.board.engine.clone();
let game_is_against_bot = self.game.board.is_game_against_bot;
self.game.board = Board::default();
self.game.board.engine = engine;
self.game.board.is_game_against_bot = game_is_against_bot;
if is_bot_starting {
self.board.is_bot_starting = true;
self.board.bot_move();
self.board.player_turn = PieceColor::Black;
self.game.board.is_bot_starting = true;
self.game.board.bot_move();
self.game.board.player_turn = PieceColor::Black;
}
}
}
Expand All @@ -148,7 +147,7 @@ impl App {
self.current_page = Pages::Bot
}
2 => {
self.board.display_mode = match self.board.display_mode {
self.game.board.display_mode = match self.game.board.display_mode {
DisplayMode::ASCII => DisplayMode::DEFAULT,
DisplayMode::DEFAULT => DisplayMode::ASCII,
};
Expand All @@ -173,7 +172,7 @@ impl App {
if let Some(table) = config.as_table_mut() {
table.insert(
"display_mode".to_string(),
Value::String(self.board.display_mode.to_string()),
Value::String(self.game.board.display_mode.to_string()),
);
}

Expand Down
28 changes: 28 additions & 0 deletions src/game/game.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use super::board::{Board, GameBoard};
use crate::pieces::{PieceColor, PieceMove};

pub struct Game {
pub board: Board,
pub move_history: Vec<PieceMove>,
pub current_turn: PieceColor,
pub is_king_checked: bool,
pub is_king_checkmated: bool,
pub is_stalemate: bool,
pub is_draw: bool,
pub is_game_over: bool,
}

impl Game {
pub fn new() -> Self {
Self {
board: Board::default(),
move_history: Vec::new(),
current_turn: PieceColor::White,
is_king_checked: false,
is_king_checkmated: false,
is_stalemate: false,
is_draw: false,
is_game_over: false,
}
}
}
1 change: 1 addition & 0 deletions src/game/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod board;
pub mod game;
12 changes: 6 additions & 6 deletions src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,29 +27,29 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> {
if app.current_page == Pages::Bot && app.selected_color.is_none() {
app.menu_cursor_right(2);
} else {
app.board.cursor_right();
app.game.board.cursor_right();
}
}
KeyCode::Left | KeyCode::Char('h') => {
// When we are in the color selection menu
if app.current_page == Pages::Bot && app.selected_color.is_none() {
app.menu_cursor_left(2);
} else {
app.board.cursor_left();
app.game.board.cursor_left();
}
}
KeyCode::Up | KeyCode::Char('k') => {
if app.current_page == Pages::Home {
app.menu_cursor_up(Pages::variant_count() as u8);
} else {
app.board.cursor_up();
app.game.board.cursor_up();
}
}
KeyCode::Down | KeyCode::Char('j') => {
if app.current_page == Pages::Home {
app.menu_cursor_down(Pages::variant_count() as u8);
} else {
app.board.cursor_down();
app.game.board.cursor_down();
}
}
KeyCode::Char(' ') | KeyCode::Enter => {
Expand All @@ -58,7 +58,7 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> {
} else if app.current_page == Pages::Home {
app.menu_select();
} else {
app.board.select_cell();
app.game.board.select_cell();
}
}
KeyCode::Char('?') => {
Expand All @@ -77,7 +77,7 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> {
app.show_color_popup = false;
app.menu_cursor = 0;
}
app.board.unselect_cell();
app.game.board.unselect_cell();
}
KeyCode::Char('b') => {
app.go_to_home();
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ fn main() -> AppResult<()> {
}
// Set the display mode based on the configuration file
if let Some(display_mode) = config.get("display_mode") {
app.board.display_mode = match display_mode.as_str() {
app.game.board.display_mode = match display_mode.as_str() {
Some("ASCII") => DisplayMode::ASCII,
_ => DisplayMode::DEFAULT,
};
Expand Down
52 changes: 30 additions & 22 deletions src/ui/popups.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,43 +116,51 @@ pub fn render_promotion_popup(frame: &mut Frame, app: &App) {
)
.split(inner_popup_layout_vertical[1]);

let display_mode = &app.board.display_mode;
let display_mode = &app.game.board.display_mode;

let queen_p = Paragraph::new(Queen::to_string(display_mode))
.block(Block::default())
.alignment(Alignment::Center)
.style(Style::default().bg(if app.board.promotion_cursor == 0 {
Color::LightBlue
} else {
Color::Reset // Set to the default background color when the condition is false
}));
.style(
Style::default().bg(if app.game.board.promotion_cursor == 0 {
Color::LightBlue
} else {
Color::Reset // Set to the default background color when the condition is false
}),
);
frame.render_widget(queen_p, inner_popup_layout_horizontal[0]);
let rook_p = Paragraph::new(Rook::to_string(display_mode))
.block(Block::default())
.alignment(Alignment::Center)
.style(Style::default().bg(if app.board.promotion_cursor == 1 {
Color::LightBlue
} else {
Color::Reset // Set to the default background color when the condition is false
}));
.style(
Style::default().bg(if app.game.board.promotion_cursor == 1 {
Color::LightBlue
} else {
Color::Reset // Set to the default background color when the condition is false
}),
);
frame.render_widget(rook_p, inner_popup_layout_horizontal[1]);
let bishop_p = Paragraph::new(Bishop::to_string(display_mode))
.block(Block::default())
.alignment(Alignment::Center)
.style(Style::default().bg(if app.board.promotion_cursor == 2 {
Color::LightBlue
} else {
Color::Reset // Set to the default background color when the condition is false
}));
.style(
Style::default().bg(if app.game.board.promotion_cursor == 2 {
Color::LightBlue
} else {
Color::Reset // Set to the default background color when the condition is false
}),
);
frame.render_widget(bishop_p, inner_popup_layout_horizontal[2]);
let knight_p = Paragraph::new(Knight::to_string(display_mode))
.block(Block::default())
.alignment(Alignment::Center)
.style(Style::default().bg(if app.board.promotion_cursor == 3 {
Color::LightBlue
} else {
Color::Reset // Set to the default background color when the condition is false
}));
.style(
Style::default().bg(if app.game.board.promotion_cursor == 3 {
Color::LightBlue
} else {
Color::Reset // Set to the default background color when the condition is false
}),
);
frame.render_widget(knight_p, inner_popup_layout_horizontal[3]);
}

Expand Down Expand Up @@ -300,7 +308,7 @@ pub fn render_color_selection_popup(frame: &mut Frame, app: &App) {
)
.split(inner_popup_layout_vertical[1]);

let display_mode = &app.board.display_mode;
let display_mode = &app.game.board.display_mode;

let white_pawn = Paragraph::new(Pawn::to_string(display_mode))
.block(Block::default())
Expand Down
26 changes: 15 additions & 11 deletions src/ui/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ pub fn render(app: &mut App, frame: &mut Frame) {
if app.current_page == Pages::Solo {
render_game_ui(frame, app, main_area);
} else if app.current_page == Pages::Bot {
if app.board.engine.is_none() {
if app.game.board.engine.is_none() {
match &app.chess_engine_path {
Some(path) => {
app.board.set_engine(path);
app.game.board.set_engine(path);
if app.selected_color.is_none() {
app.show_color_popup = true;
} else {
Expand Down Expand Up @@ -108,7 +108,7 @@ pub fn render_menu_ui(frame: &mut Frame, app: &App, main_area: Rect) {

// Determine the "display mode" text
let display_mode_menu = {
let display_mode = match app.board.display_mode {
let display_mode = match app.game.board.display_mode {
DisplayMode::DEFAULT => "Default",
DisplayMode::ASCII => "ASCII",
};
Expand Down Expand Up @@ -188,31 +188,35 @@ pub fn render_game_ui(frame: &mut Frame, app: &App, main_area: Rect) {
frame.render_widget(board_block.clone(), main_layout_vertical[1]);

// We make the inside of the board
app.board
app.game
.board
.board_render(board_block.inner(main_layout_vertical[1]), frame);

//top box for white material
app.board
app.game
.board
.black_material_render(board_block.inner(right_box_layout[0]), frame);

// We make the inside of the board
app.board
app.game
.board
.history_render(board_block.inner(right_box_layout[1]), frame);

//bottom box for black matetrial
app.board
app.game
.board
.white_material_render(board_block.inner(right_box_layout[2]), frame);

if app.board.is_promotion {
if app.game.board.is_promotion {
render_promotion_popup(frame, app);
}

if app.board.is_draw {
if app.game.board.is_draw {
render_end_popup(frame, "That's a draw");
}

if app.board.is_checkmate {
let victorious_player = app.board.player_turn.opposite();
if app.game.board.is_checkmate {
let victorious_player = app.game.board.player_turn.opposite();

let string_color = match victorious_player {
PieceColor::White => "White",
Expand Down

0 comments on commit 1943b31

Please sign in to comment.