How to make a real xiangqi board?

const PIECE_NAMES: [&str; 15] = [
    "  ", "帅", "仕", "相", "马", "车", "炮", "兵",
    "将", "士", "象", "马", "车", "炮", "卒"
];

fn red(s: &str) -> String { format!("\x1b[1;31m{}\x1b[0m", s) }
fn blue(s: &str) -> String { format!("\x1b[1;34m{}\x1b[0m", s) }
fn yellow(s: &str) -> String { format!("\x1b[1;33m{}\x1b[0m", s) }
fn green(s: &str) -> String { format!("\x1b[1;32m{}\x1b[0m"");
    let _ = io::stdout().flush();                          
    println!();                                                println!("{}", yellow("      ╔══════════ 中国象棋 ══════════╗"));
    println!();
    println!("         0    1    2    3    4    5    6    7    8");                                                       println!("       +----+----+----+----+----+----+----+----+----+");

    for r in 0..10 {
        print!("     {} │", r);
        for c in 0..9 {                                                let p = b.board[r][c];
            let is_sel = r as i32 == sel_r && c as i32 == sel_c;
            let is_tgt = targets.contains(&(r as i32, c as i32));

            let name = if p == 0 { "  " } else { PIECE_NAMES[p as usize] };

            let mut cell = name.to_string();
            if is_sel {
                cell = bg_select(&cell);
            } else if is_tgt {                                             cell = bg_target(&cell);
            }

            if p != 0 {
                cell = if is_red(p) { red(&cell) } else { blue(&cell) };
            }
            print!(" {} │", cell);
        }
        println!();
        if r < 9 {
            println!("       +----+----+----+----+----+----+----+----+----+");
        }
    }
    println!("       +----+----+----+----+----+----+----+----+----+");
    println!();
    if b.red_turn {
        println!("{}", green("      ► 轮到: 红方 (帅)"));
    } else {
        println!("{}", blue("      ► 轮到: 黑方 (将)"));
    }
    println!();
    let _ = io::stdout().flush();
}

It shows board which puts pieces in cell instead of crossing points . How to modify it to represent a real chess board ?

Regard!

For the record, I believe your post would be easier to understand if you referred to the game as "xiangqi" or "Chinese chess". Just plain "chess" refers to, well, chess, which does not have a board like that.

I think the TUI you have now is ok, but if you really wanted to place the pieces on the intersections you could build the board with 5x3 or 7x5 blocks of cells.

You could also draw the pieces with blocks - like they did in this chess tui.

Rook:

▗▄ ▃ ▄▖
▐█▄█▄█▌
▝▜███▛▘
 ▟███▙ 
▝▀▀▀▀▀▘

For anyone else that might want to play around, I put up a runnable version of the original output code on the playground, though unfortunately it lacks ANSI code support so the output is mangled:


@zMatrix7 - If the only change you want to make is to render the board such that the pieces are on the intersections, it should be a pretty small change. For a toy example, instead of:

+---+---+---+
| 1 |   | 2 |
+---+---+---+
| 3 | 4 |   |
+---+---+---+

You want:

  1---+---2
  |   |   |
  3---4---+

Going line by line, the changes in the output are:

  • you remove the top and bottom lines
  • the lines with the pieces print --- between the positions instead of | before and after them.
  • when a piece is not present, you print + instead of
  • the lines between pieces print | | ... instead of +---+---...

Let me know if any of these changes aren't clear to you!

Of course, a real xiangqi board has a more complex pattern, if you want to try to reproduce that it's probably simpler to just directly print each line with a line of code each, for example:

// each row is always horizontally connected
print_line(&b.board[0]);
// note the leading "r" before the string, this makes the "\" a literal "\" character.
println!(r"|   |   |   | \ | / |   |   |   |");
print_line(&b.board[1]);
println!(r"|   |   |   | / | \ |   |   |   |");
print_line(&b.board[2]);
// ...

Thanks for all your suggestions! It seems hard to align those text on Termux. The original board works if you get accustomed to it .