thomas-mauran / chess-tui

A chess TUI implementation in rust 🦀
https://crates.io/crates/chess-tui
MIT License
361 stars 20 forks source link

Add ASCII mode #60

Closed pSnehanshu closed 6 months ago

pSnehanshu commented 6 months ago

Add ASCII mode

Description

Fixes #58

image

image

How Has This Been Tested?

I have tested this manually, and all the existing test cases pass. However, I haven't written any tests for board rendering, since there doesn't seem to be any such existing tests.

Checklist:

pSnehanshu commented 6 months ago

Output of cargo clippy

    Checking chess-tui v1.2.0 (/home/snehanshu/projects/chess-tui)
warning: equality checks against false can be replaced by a negation
   --> src/board.rs:318:28
    |
318 |                         if self.is_promotion == false {
    |                            ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try simplifying it as shown: `!self.is_promotion`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#bool_comparison
    = note: `#[warn(clippy::bool_comparison)]` on by default

warning: this match could be written as a `let` statement
   --> src/board.rs:366:17
    |
366 | /                 match (
367 | |                     get_piece_type(self.board, [i, j]),
368 | |                     get_piece_color(self.board, [i, j]),
369 | |                 ) {
...   |
397 | |                     }
398 | |                 }
    | |_________________^
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#match_single_binding
    = note: `#[warn(clippy::match_single_binding)]` on by default
help: consider using a `let` statement
    |
366 ~                 let (piece_type, piece_color) = (
367 +                     get_piece_type(self.board, [i, j]),
368 +                     get_piece_color(self.board, [i, j]),
369 +                 );
370 +                 match PieceType::piece_to_fen_enum(piece_type, piece_color) {
371 +                     // Pattern match directly on the result of piece_to_fen_enum
372 +                     "" => {
373 +                         // Check if the string is not empty before using chars().last()
374 +                         if let Some(last_char) = result.chars().last() {
375 +                             if last_char.is_ascii_digit() {
376 +                                 let incremented_char = char::from_digit(
377 +                                     last_char.to_digit(10).unwrap_or(0) + 1,
378 +                                     10,
379 +                                 )
380 +                                 .unwrap_or_default();
381 +                                 // Remove the old number and add the new incremented one
382 +                                 result.pop();
383 +                                 result.push_str(incremented_char.to_string().as_str());
384 +                             } else {
385 +                                 result.push('1');
386 +                             }
387 +                         } else {
388 +                             result.push('1');
389 +                         }
390 +                     }
391 +                     letter => {
392 +                         // If the result is not an empty string, push '1'
393 +                         result.push_str(letter);
394 +                     }
395 +                 }
    |

warning: calling `push_str()` using a single-character string literal
   --> src/board.rs:419:17
    |
419 |                 result.push_str("q");
    |                 ^^^^^^^^^^^^^^^^^^^^ help: consider using `push` with a character literal: `result.push('q')`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#single_char_add_str
    = note: `#[warn(clippy::single_char_add_str)]` on by default

warning: calling `push_str()` using a single-character string literal
   --> src/board.rs:441:21
    |
441 |                     result.push_str(" ");
    |                     ^^^^^^^^^^^^^^^^^^^^ help: consider using `push` with a character literal: `result.push(' ')`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#single_char_add_str

warning: calling `push_str()` using a single-character string literal
   --> src/board.rs:449:9
    |
449 |         result.push_str(" ");
    |         ^^^^^^^^^^^^^^^^^^^^ help: consider using `push` with a character literal: `result.push(' ')`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#single_char_add_str

warning: calling `push_str()` using a single-character string literal
   --> src/board.rs:452:9
    |
452 |         result.push_str(" ");
    |         ^^^^^^^^^^^^^^^^^^^^ help: consider using `push` with a character literal: `result.push(' ')`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#single_char_add_str

warning: unneeded `return` statement
   --> src/board.rs:470:17
    |
470 |                 return false;
    |                 ^^^^^^^^^^^^
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_return
    = note: `#[warn(clippy::needless_return)]` on by default
help: remove `return`
    |
470 -                 return false;
470 +                 false
    |

warning: `if` chain can be rewritten with `match`
   --> src/board.rs:552:13
    |
552 | /             if distance > 0 {
553 | |                 row_index_rook = 3;
554 | |                 if self.is_game_against_bot && self.player_turn == PieceColor::Black {
555 | |                     to_x = 0;
...   |
561 | |                 }
562 | |             }
    | |_____________^
    |
    = help: consider rewriting the `if` chain to use `cmp` and `match`
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#comparison_chain
    = note: `#[warn(clippy::comparison_chain)]` on by default

warning: `chess-tui` (lib) generated 8 warnings (run `cargo clippy --fix --lib -p chess-tui` to apply 6 suggestions)
    Finished dev [unoptimized + debuginfo] target(s) in 0.69s
snehanshu@SM-PF3WY6M6:~/projects/chess-tui$ cargo clippy > clippy
warning: equality checks against false can be replaced by a negation
   --> src/board.rs:318:28
    |
318 |                         if self.is_promotion == false {
    |                            ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try simplifying it as shown: `!self.is_promotion`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#bool_comparison
    = note: `#[warn(clippy::bool_comparison)]` on by default

warning: this match could be written as a `let` statement
   --> src/board.rs:366:17
    |
366 | /                 match (
367 | |                     get_piece_type(self.board, [i, j]),
368 | |                     get_piece_color(self.board, [i, j]),
369 | |                 ) {
...   |
397 | |                     }
398 | |                 }
    | |_________________^
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#match_single_binding
    = note: `#[warn(clippy::match_single_binding)]` on by default
help: consider using a `let` statement
    |
366 ~                 let (piece_type, piece_color) = (
367 +                     get_piece_type(self.board, [i, j]),
368 +                     get_piece_color(self.board, [i, j]),
369 +                 );
370 +                 match PieceType::piece_to_fen_enum(piece_type, piece_color) {
371 +                     // Pattern match directly on the result of piece_to_fen_enum
372 +                     "" => {
373 +                         // Check if the string is not empty before using chars().last()
374 +                         if let Some(last_char) = result.chars().last() {
375 +                             if last_char.is_ascii_digit() {
376 +                                 let incremented_char = char::from_digit(
377 +                                     last_char.to_digit(10).unwrap_or(0) + 1,
378 +                                     10,
379 +                                 )
380 +                                 .unwrap_or_default();
381 +                                 // Remove the old number and add the new incremented one
382 +                                 result.pop();
383 +                                 result.push_str(incremented_char.to_string().as_str());
384 +                             } else {
385 +                                 result.push('1');
386 +                             }
387 +                         } else {
388 +                             result.push('1');
389 +                         }
390 +                     }
391 +                     letter => {
392 +                         // If the result is not an empty string, push '1'
393 +                         result.push_str(letter);
394 +                     }
395 +                 }
    |

warning: calling `push_str()` using a single-character string literal
   --> src/board.rs:419:17
    |
419 |                 result.push_str("q");
    |                 ^^^^^^^^^^^^^^^^^^^^ help: consider using `push` with a character literal: `result.push('q')`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#single_char_add_str
    = note: `#[warn(clippy::single_char_add_str)]` on by default

warning: calling `push_str()` using a single-character string literal
   --> src/board.rs:441:21
    |
441 |                     result.push_str(" ");
    |                     ^^^^^^^^^^^^^^^^^^^^ help: consider using `push` with a character literal: `result.push(' ')`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#single_char_add_str

warning: calling `push_str()` using a single-character string literal
   --> src/board.rs:449:9
    |
449 |         result.push_str(" ");
    |         ^^^^^^^^^^^^^^^^^^^^ help: consider using `push` with a character literal: `result.push(' ')`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#single_char_add_str

warning: calling `push_str()` using a single-character string literal
   --> src/board.rs:452:9
    |
452 |         result.push_str(" ");
    |         ^^^^^^^^^^^^^^^^^^^^ help: consider using `push` with a character literal: `result.push(' ')`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#single_char_add_str

warning: unneeded `return` statement
   --> src/board.rs:470:17
    |
470 |                 return false;
    |                 ^^^^^^^^^^^^
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_return
    = note: `#[warn(clippy::needless_return)]` on by default
help: remove `return`
    |
470 -                 return false;
470 +                 false
    |

warning: `if` chain can be rewritten with `match`
   --> src/board.rs:552:13
    |
552 | /             if distance > 0 {
553 | |                 row_index_rook = 3;
554 | |                 if self.is_game_against_bot && self.player_turn == PieceColor::Black {
555 | |                     to_x = 0;
...   |
561 | |                 }
562 | |             }
    | |_____________^
    |
    = help: consider rewriting the `if` chain to use `cmp` and `match`
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#comparison_chain
    = note: `#[warn(clippy::comparison_chain)]` on by default

warning: `chess-tui` (lib) generated 8 warnings (run `cargo clippy --fix --lib -p chess-tui` to apply 6 suggestions)
    Finished dev [unoptimized + debuginfo] target(s) in 0.02s
pSnehanshu commented 6 months ago

How does this look?

image

thomas-mauran commented 6 months ago

How does this look?

image

I like a lot this version, I just saw that ratatui got a underline and bold style option for text can we try that for the elements of the current player to make it even easier to see on the board

thomas-mauran commented 6 months ago

Output of cargo clippy

    Checking chess-tui v1.2.0 (/home/snehanshu/projects/chess-tui)
warning: equality checks against false can be replaced by a negation
   --> src/board.rs:318:28
    |
318 |                         if self.is_promotion == false {
    |                            ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try simplifying it as shown: `!self.is_promotion`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#bool_comparison
    = note: `#[warn(clippy::bool_comparison)]` on by default

warning: this match could be written as a `let` statement
   --> src/board.rs:366:17
    |
366 | /                 match (
367 | |                     get_piece_type(self.board, [i, j]),
368 | |                     get_piece_color(self.board, [i, j]),
369 | |                 ) {
...   |
397 | |                     }
398 | |                 }
    | |_________________^
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#match_single_binding
    = note: `#[warn(clippy::match_single_binding)]` on by default
help: consider using a `let` statement
    |
366 ~                 let (piece_type, piece_color) = (
367 +                     get_piece_type(self.board, [i, j]),
368 +                     get_piece_color(self.board, [i, j]),
369 +                 );
370 +                 match PieceType::piece_to_fen_enum(piece_type, piece_color) {
371 +                     // Pattern match directly on the result of piece_to_fen_enum
372 +                     "" => {
373 +                         // Check if the string is not empty before using chars().last()
374 +                         if let Some(last_char) = result.chars().last() {
375 +                             if last_char.is_ascii_digit() {
376 +                                 let incremented_char = char::from_digit(
377 +                                     last_char.to_digit(10).unwrap_or(0) + 1,
378 +                                     10,
379 +                                 )
380 +                                 .unwrap_or_default();
381 +                                 // Remove the old number and add the new incremented one
382 +                                 result.pop();
383 +                                 result.push_str(incremented_char.to_string().as_str());
384 +                             } else {
385 +                                 result.push('1');
386 +                             }
387 +                         } else {
388 +                             result.push('1');
389 +                         }
390 +                     }
391 +                     letter => {
392 +                         // If the result is not an empty string, push '1'
393 +                         result.push_str(letter);
394 +                     }
395 +                 }
    |

warning: calling `push_str()` using a single-character string literal
   --> src/board.rs:419:17
    |
419 |                 result.push_str("q");
    |                 ^^^^^^^^^^^^^^^^^^^^ help: consider using `push` with a character literal: `result.push('q')`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#single_char_add_str
    = note: `#[warn(clippy::single_char_add_str)]` on by default

warning: calling `push_str()` using a single-character string literal
   --> src/board.rs:441:21
    |
441 |                     result.push_str(" ");
    |                     ^^^^^^^^^^^^^^^^^^^^ help: consider using `push` with a character literal: `result.push(' ')`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#single_char_add_str

warning: calling `push_str()` using a single-character string literal
   --> src/board.rs:449:9
    |
449 |         result.push_str(" ");
    |         ^^^^^^^^^^^^^^^^^^^^ help: consider using `push` with a character literal: `result.push(' ')`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#single_char_add_str

warning: calling `push_str()` using a single-character string literal
   --> src/board.rs:452:9
    |
452 |         result.push_str(" ");
    |         ^^^^^^^^^^^^^^^^^^^^ help: consider using `push` with a character literal: `result.push(' ')`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#single_char_add_str

warning: unneeded `return` statement
   --> src/board.rs:470:17
    |
470 |                 return false;
    |                 ^^^^^^^^^^^^
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_return
    = note: `#[warn(clippy::needless_return)]` on by default
help: remove `return`
    |
470 -                 return false;
470 +                 false
    |

warning: `if` chain can be rewritten with `match`
   --> src/board.rs:552:13
    |
552 | /             if distance > 0 {
553 | |                 row_index_rook = 3;
554 | |                 if self.is_game_against_bot && self.player_turn == PieceColor::Black {
555 | |                     to_x = 0;
...   |
561 | |                 }
562 | |             }
    | |_____________^
    |
    = help: consider rewriting the `if` chain to use `cmp` and `match`
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#comparison_chain
    = note: `#[warn(clippy::comparison_chain)]` on by default

warning: `chess-tui` (lib) generated 8 warnings (run `cargo clippy --fix --lib -p chess-tui` to apply 6 suggestions)
    Finished dev [unoptimized + debuginfo] target(s) in 0.69s
snehanshu@SM-PF3WY6M6:~/projects/chess-tui$ cargo clippy > clippy
warning: equality checks against false can be replaced by a negation
   --> src/board.rs:318:28
    |
318 |                         if self.is_promotion == false {
    |                            ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try simplifying it as shown: `!self.is_promotion`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#bool_comparison
    = note: `#[warn(clippy::bool_comparison)]` on by default

warning: this match could be written as a `let` statement
   --> src/board.rs:366:17
    |
366 | /                 match (
367 | |                     get_piece_type(self.board, [i, j]),
368 | |                     get_piece_color(self.board, [i, j]),
369 | |                 ) {
...   |
397 | |                     }
398 | |                 }
    | |_________________^
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#match_single_binding
    = note: `#[warn(clippy::match_single_binding)]` on by default
help: consider using a `let` statement
    |
366 ~                 let (piece_type, piece_color) = (
367 +                     get_piece_type(self.board, [i, j]),
368 +                     get_piece_color(self.board, [i, j]),
369 +                 );
370 +                 match PieceType::piece_to_fen_enum(piece_type, piece_color) {
371 +                     // Pattern match directly on the result of piece_to_fen_enum
372 +                     "" => {
373 +                         // Check if the string is not empty before using chars().last()
374 +                         if let Some(last_char) = result.chars().last() {
375 +                             if last_char.is_ascii_digit() {
376 +                                 let incremented_char = char::from_digit(
377 +                                     last_char.to_digit(10).unwrap_or(0) + 1,
378 +                                     10,
379 +                                 )
380 +                                 .unwrap_or_default();
381 +                                 // Remove the old number and add the new incremented one
382 +                                 result.pop();
383 +                                 result.push_str(incremented_char.to_string().as_str());
384 +                             } else {
385 +                                 result.push('1');
386 +                             }
387 +                         } else {
388 +                             result.push('1');
389 +                         }
390 +                     }
391 +                     letter => {
392 +                         // If the result is not an empty string, push '1'
393 +                         result.push_str(letter);
394 +                     }
395 +                 }
    |

warning: calling `push_str()` using a single-character string literal
   --> src/board.rs:419:17
    |
419 |                 result.push_str("q");
    |                 ^^^^^^^^^^^^^^^^^^^^ help: consider using `push` with a character literal: `result.push('q')`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#single_char_add_str
    = note: `#[warn(clippy::single_char_add_str)]` on by default

warning: calling `push_str()` using a single-character string literal
   --> src/board.rs:441:21
    |
441 |                     result.push_str(" ");
    |                     ^^^^^^^^^^^^^^^^^^^^ help: consider using `push` with a character literal: `result.push(' ')`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#single_char_add_str

warning: calling `push_str()` using a single-character string literal
   --> src/board.rs:449:9
    |
449 |         result.push_str(" ");
    |         ^^^^^^^^^^^^^^^^^^^^ help: consider using `push` with a character literal: `result.push(' ')`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#single_char_add_str

warning: calling `push_str()` using a single-character string literal
   --> src/board.rs:452:9
    |
452 |         result.push_str(" ");
    |         ^^^^^^^^^^^^^^^^^^^^ help: consider using `push` with a character literal: `result.push(' ')`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#single_char_add_str

warning: unneeded `return` statement
   --> src/board.rs:470:17
    |
470 |                 return false;
    |                 ^^^^^^^^^^^^
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_return
    = note: `#[warn(clippy::needless_return)]` on by default
help: remove `return`
    |
470 -                 return false;
470 +                 false
    |

warning: `if` chain can be rewritten with `match`
   --> src/board.rs:552:13
    |
552 | /             if distance > 0 {
553 | |                 row_index_rook = 3;
554 | |                 if self.is_game_against_bot && self.player_turn == PieceColor::Black {
555 | |                     to_x = 0;
...   |
561 | |                 }
562 | |             }
    | |_____________^
    |
    = help: consider rewriting the `if` chain to use `cmp` and `match`
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#comparison_chain
    = note: `#[warn(clippy::comparison_chain)]` on by default

warning: `chess-tui` (lib) generated 8 warnings (run `cargo clippy --fix --lib -p chess-tui` to apply 6 suggestions)
    Finished dev [unoptimized + debuginfo] target(s) in 0.02s

to fix the lint with clippy I often use this command: cargo clippy --fix --allow-dirty --allow-staged you can try running that if you can

pSnehanshu commented 6 months ago

to fix the lint with clippy I often use this command: cargo clippy --fix --allow-dirty --allow-staged you can try running that if you can

@thomas-mauran sure, but those warnings aren't related to my changes, so I don't think that's relevant in this PR.

thomas-mauran commented 6 months ago

to fix the lint with clippy I often use this command: cargo clippy --fix --allow-dirty --allow-staged you can try running that if you can

@thomas-mauran sure, but those warnings aren't related to my changes, so I don't think that's relevant in this PR.

ah yes my bad, I will fix them in another small lint pr when adding the lint test to the ci 👍

pSnehanshu commented 6 months ago

image

I underlined the current user's pieces. I didn't bold anything because I think it looks fine without it.

thomas-mauran commented 6 months ago

image

I underlined the current user's pieces. I didn't bold anything because I think it looks fine without it.

looking great like that mate !