Skip to content

Commit

Permalink
feat: add ASCII mode and board styles (#60)
Browse files Browse the repository at this point in the history
* introduce DisplayMode

* Add Display mode menu option

* vertically align center ascii piece

* black/white cell bg in ASCII mode
  • Loading branch information
pSnehanshu authored Mar 25, 2024
1 parent 620fa00 commit 5d032e0
Show file tree
Hide file tree
Showing 12 changed files with 157 additions and 49 deletions.
16 changes: 11 additions & 5 deletions src/app.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{board::Board, constants::Pages};
use crate::{board::Board, board::DisplayMode, constants::Pages};
use std::error;

/// Application result type.
Expand Down Expand Up @@ -66,12 +66,12 @@ impl App {
if self.menu_cursor > 0 {
self.menu_cursor -= 1
} else {
self.menu_cursor = 3
self.menu_cursor = 4
}
}

pub fn menu_cursor_down(&mut self) {
if self.menu_cursor < 3 {
if self.menu_cursor < 4 {
self.menu_cursor += 1
} else {
self.menu_cursor = 0
Expand All @@ -88,8 +88,14 @@ impl App {
match self.menu_cursor {
0 => self.current_page = Pages::Solo,
1 => self.current_page = Pages::Bot,
2 => self.current_page = Pages::Help,
3 => self.current_page = Pages::Credit,
2 => {
self.board.display_mode = match self.board.display_mode {
DisplayMode::ASCII => DisplayMode::DEFAULT,
DisplayMode::DEFAULT => DisplayMode::ASCII,
};
}
3 => self.current_page = Pages::Help,
4 => self.current_page = Pages::Credit,
_ => {}
}
}
Expand Down
38 changes: 22 additions & 16 deletions src/board.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ use crate::{
constants::{BLACK, UNDEFINED_POSITION, WHITE},
pieces::{PieceColor, PieceType},
utils::{
col_to_letter, color_to_ratatui_enum, convert_notation_into_position,
convert_position_into_notation, did_piece_already_move, get_int_from_char,
get_king_coordinates, get_piece_color, get_piece_type, get_player_turn_in_modulo,
is_getting_checked, is_valid,
col_to_letter, convert_notation_into_position, convert_position_into_notation,
did_piece_already_move, get_cell_paragraph, get_int_from_char, get_king_coordinates,
get_piece_color, get_piece_type, get_player_turn_in_modulo, is_getting_checked, is_valid,
},
};
use ratatui::{
Expand All @@ -17,6 +16,11 @@ use ratatui::{
};
use uci::Engine;

pub enum DisplayMode {
DEFAULT,
ASCII,
}

pub struct Board {
pub board: [[Option<(PieceType, PieceColor)>; 8]; 8],
pub cursor_coordinates: [i8; 2],
Expand All @@ -32,6 +36,7 @@ pub struct Board {
pub consecutive_non_pawn_or_capture: i32,
pub engine: Option<Engine>,
pub is_game_against_bot: bool,
pub display_mode: DisplayMode,
}

impl Default for Board {
Expand Down Expand Up @@ -96,6 +101,7 @@ impl Default for Board {
consecutive_non_pawn_or_capture: 0,
engine: None,
is_game_against_bot: false,
display_mode: DisplayMode::DEFAULT,
}
}
}
Expand All @@ -121,6 +127,7 @@ impl Board {
consecutive_non_pawn_or_capture: 0,
engine: None,
is_game_against_bot: false,
display_mode: DisplayMode::DEFAULT,
}
}

Expand Down Expand Up @@ -767,23 +774,22 @@ impl Board {
let cell = Block::default().bg(Color::LightGreen);
frame.render_widget(cell.clone(), square);
} else {
let cell = Block::default().bg(cell_color);
let mut cell = Block::default();
cell = match self.display_mode {
DisplayMode::DEFAULT => cell.bg(cell_color),
DisplayMode::ASCII => match cell_color {
WHITE => cell.bg(Color::White).fg(Color::Black),
BLACK => cell.bg(Color::Black).fg(Color::White),
_ => cell.bg(cell_color),
},
};

frame.render_widget(cell.clone(), square);
}

// We check if the current king is getting checked

// Get piece and color
let piece_color = get_piece_color(self.board, [i, j]);
let piece_type = get_piece_type(self.board, [i, j]);

let color_enum = color_to_ratatui_enum(piece_color);
let piece_enum = PieceType::piece_type_to_string_enum(piece_type);
let paragraph = get_cell_paragraph(self, [i, j], square);

// Place the pieces on the board
let paragraph = Paragraph::new(piece_enum)
.alignment(Alignment::Center)
.fg(color_enum);
frame.render_widget(paragraph, square);
}
}
Expand Down
10 changes: 8 additions & 2 deletions src/pieces/bishop.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::{Movable, PieceColor, PieceType, Position};
use crate::board::DisplayMode;
use crate::utils::{
cleaned_positions, get_piece_color, impossible_positions_king_checked, is_cell_color_ally,
is_piece_opposite_king, is_valid,
Expand Down Expand Up @@ -190,14 +191,19 @@ impl Position for Bishop {
}

impl Bishop {
pub fn to_string() -> &'static str {
"\
pub fn to_string(display_mode: &DisplayMode) -> &'static str {
match display_mode {
DisplayMode::DEFAULT => {
"\
\n\
\n\
█✝█\n\
███\n\
▗█████▖\n\
"
}
DisplayMode::ASCII => "B",
}
}
}

Expand Down
11 changes: 8 additions & 3 deletions src/pieces/king.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use super::{Movable, PieceColor, PieceType, Position};
use crate::board::DisplayMode;
use crate::utils::{
cleaned_positions, did_piece_already_move, get_all_protected_cells, get_piece_type,
is_cell_color_ally, is_valid, is_vec_in_array,
};

pub struct King;

impl Movable for King {
Expand Down Expand Up @@ -104,14 +104,19 @@ impl Position for King {
}

impl King {
pub fn to_string() -> &'static str {
"\
pub fn to_string(display_mode: &DisplayMode) -> &'static str {
match display_mode {
DisplayMode::DEFAULT => {
"\
\n\
▞▀▄▀▚\n\
▙▄█▄▟\n\
▐███▌\n\
▗█████▖\n\
"
}
DisplayMode::ASCII => "K",
}
}

// Check if nothing is in between the king and a rook and if none of those cells are getting checked
Expand Down
10 changes: 8 additions & 2 deletions src/pieces/knight.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::{Movable, PieceColor, PieceType, Position};
use crate::board::DisplayMode;
use crate::utils::{
cleaned_positions, impossible_positions_king_checked, is_cell_color_ally, is_valid,
};
Expand Down Expand Up @@ -74,14 +75,19 @@ impl Position for Knight {
}

impl Knight {
pub fn to_string() -> &'static str {
"\
pub fn to_string(display_mode: &DisplayMode) -> &'static str {
match display_mode {
DisplayMode::DEFAULT => {
"\
\n\
▟▛██▙\n\
▟█████\n\
▀▀▟██▌\n\
▟████\n\
"
}
DisplayMode::ASCII => "N",
}
}
}

Expand Down
18 changes: 11 additions & 7 deletions src/pieces/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use self::{bishop::Bishop, king::King, knight::Knight, pawn::Pawn, queen::Queen, rook::Rook};
use super::board::DisplayMode;

pub mod bishop;
pub mod king;
Expand Down Expand Up @@ -131,14 +132,17 @@ impl PieceType {
}
}

pub fn piece_type_to_string_enum(piece_type: Option<PieceType>) -> &'static str {
pub fn piece_type_to_string_enum(
piece_type: Option<PieceType>,
display_mode: &DisplayMode,
) -> &'static str {
match piece_type {
Some(PieceType::Queen) => Queen::to_string(),
Some(PieceType::King) => King::to_string(),
Some(PieceType::Rook) => Rook::to_string(),
Some(PieceType::Bishop) => Bishop::to_string(),
Some(PieceType::Knight) => Knight::to_string(),
Some(PieceType::Pawn) => Pawn::to_string(),
Some(PieceType::Queen) => Queen::to_string(display_mode),
Some(PieceType::King) => King::to_string(display_mode),
Some(PieceType::Rook) => Rook::to_string(display_mode),
Some(PieceType::Bishop) => Bishop::to_string(display_mode),
Some(PieceType::Knight) => Knight::to_string(display_mode),
Some(PieceType::Pawn) => Pawn::to_string(display_mode),
None => " ",
}
}
Expand Down
10 changes: 8 additions & 2 deletions src/pieces/pawn.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::{Movable, PieceColor, PieceType, Position};
use crate::board::DisplayMode;
use crate::utils::{
cleaned_positions, get_int_from_char, get_latest_move, get_piece_color,
impossible_positions_king_checked, is_cell_color_ally, is_valid,
Expand Down Expand Up @@ -151,14 +152,19 @@ impl Position for Pawn {
}

impl Pawn {
pub fn to_string() -> &'static str {
"\
pub fn to_string(display_mode: &DisplayMode) -> &'static str {
match display_mode {
DisplayMode::DEFAULT => {
"\
\n\
\n\
▟█▙\n\
▜█▛\n\
▟███▙\n\
"
}
DisplayMode::ASCII => "P",
}
}
}

Expand Down
10 changes: 8 additions & 2 deletions src/pieces/queen.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::rook::Rook;
use super::{Movable, PieceColor, PieceType, Position};
use crate::board::DisplayMode;
use crate::pieces::bishop::Bishop;
use crate::utils::{cleaned_positions, impossible_positions_king_checked};

Expand Down Expand Up @@ -62,14 +63,19 @@ impl Position for Queen {
}

impl Queen {
pub fn to_string() -> &'static str {
"\
pub fn to_string(display_mode: &DisplayMode) -> &'static str {
match display_mode {
DisplayMode::DEFAULT => {
"\
\n\
◀█▟█▙█▶\n\
◥█◈█◤\n\
███\n\
▗█████▖\n\
"
}
DisplayMode::ASCII => "Q",
}
}
}

Expand Down
11 changes: 8 additions & 3 deletions src/pieces/rook.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use super::{Movable, PieceColor, PieceType, Position};
use crate::board::DisplayMode;
use crate::utils::{
cleaned_positions, get_piece_color, impossible_positions_king_checked, is_cell_color_ally,
is_piece_opposite_king, is_valid,
};

pub struct Rook;

impl Movable for Rook {
Expand Down Expand Up @@ -195,14 +195,19 @@ impl Position for Rook {
}

impl Rook {
pub fn to_string() -> &'static str {
"\
pub fn to_string(display_mode: &DisplayMode) -> &'static str {
match display_mode {
DisplayMode::DEFAULT => {
"\
\n\
█▟█▙█\n\
▜███▛\n\
▐███▌\n\
▗█████▖\n\
"
}
DisplayMode::ASCII => "R",
}
}
}

Expand Down
10 changes: 6 additions & 4 deletions src/popups.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,9 @@ pub fn render_promotion_popup(frame: &mut Frame, app: &App) {
)
.split(inner_popup_layout_vertical[1]);

let queen_p = Paragraph::new(Queen::to_string())
let display_mode = &app.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 {
Expand All @@ -125,7 +127,7 @@ pub fn render_promotion_popup(frame: &mut Frame, app: &App) {
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())
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 {
Expand All @@ -134,7 +136,7 @@ pub fn render_promotion_popup(frame: &mut Frame, app: &App) {
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())
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 {
Expand All @@ -143,7 +145,7 @@ pub fn render_promotion_popup(frame: &mut Frame, app: &App) {
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())
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 {
Expand Down
Loading

0 comments on commit 5d032e0

Please sign in to comment.