UI tweaks

master
Mark 2024-03-04 15:49:40 -08:00
parent a2a264163a
commit 207665dda4
Signed by: Mark
GPG Key ID: C6D63995FE72FD80
1 changed files with 71 additions and 21 deletions

View File

@ -1,8 +1,5 @@
use anyhow::Result;
use std::{
io::{stdin, stdout, StdoutLock, Write},
num::NonZeroU8,
};
use std::io::{stdin, stdout, StdoutLock, Write};
use termion::{
color::{self},
cursor::HideCursor,
@ -34,6 +31,13 @@ fn play(
let mut is_first = true;
let mut print_board = true;
// For human player UI
let symbols = [
'1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '+', '-', '×', '÷',
];
let mut selected_symbol = 0;
'outer: loop {
// Computer turn
if board.current_player() == Player::Computer && !board.is_done() {
@ -67,12 +71,24 @@ fn play(
is_first = false;
}
while board.contains(Symb::from_char(&symbols[selected_symbol]).unwrap()) {
if selected_symbol == symbols.len() - 1 {
selected_symbol = 0;
} else {
selected_symbol += 1;
}
}
print!(
"\r{}╙{}{}{}{}{}╜",
" ".repeat(cursor_offset),
" ".repeat(cursor),
color::Fg(board.current_player().color()),
if board.is_done() { ' ' } else { '^' },
if board.is_done() {
' '
} else {
symbols[selected_symbol]
},
color::Fg(color::Reset),
" ".repeat(cursor_max - cursor),
);
@ -86,6 +102,7 @@ fn play(
let stdin = stdin();
for c in stdin.keys() {
print_board = match c.unwrap() {
Key::Char('q') => break 'outer,
Key::Right => {
cursor = cursor_max.min(cursor + 1);
false
@ -96,21 +113,54 @@ fn play(
}
false
}
Key::Char('q') => break 'outer,
Key::Char('1') => board.play(cursor, Symb::Number(NonZeroU8::new(1).unwrap())),
Key::Char('2') => board.play(cursor, Symb::Number(NonZeroU8::new(2).unwrap())),
Key::Char('3') => board.play(cursor, Symb::Number(NonZeroU8::new(3).unwrap())),
Key::Char('4') => board.play(cursor, Symb::Number(NonZeroU8::new(4).unwrap())),
Key::Char('5') => board.play(cursor, Symb::Number(NonZeroU8::new(5).unwrap())),
Key::Char('6') => board.play(cursor, Symb::Number(NonZeroU8::new(6).unwrap())),
Key::Char('7') => board.play(cursor, Symb::Number(NonZeroU8::new(7).unwrap())),
Key::Char('8') => board.play(cursor, Symb::Number(NonZeroU8::new(8).unwrap())),
Key::Char('9') => board.play(cursor, Symb::Number(NonZeroU8::new(9).unwrap())),
Key::Char('0') => board.play(cursor, Symb::Zero),
Key::Char('+') => board.play(cursor, Symb::Plus),
Key::Char('-') => board.play(cursor, Symb::Minus),
Key::Char('*') => board.play(cursor, Symb::Times),
Key::Char('/') => board.play(cursor, Symb::Div),
Key::Up => {
if selected_symbol == 0 {
selected_symbol = symbols.len() - 1;
} else {
selected_symbol -= 1;
}
while board.contains(Symb::from_char(&symbols[selected_symbol]).unwrap()) {
if selected_symbol == 0 {
selected_symbol = symbols.len() - 1;
} else {
selected_symbol -= 1;
}
}
false
}
Key::Down => {
if selected_symbol == symbols.len() - 1 {
selected_symbol = 0;
} else {
selected_symbol += 1;
}
while board.contains(Symb::from_char(&symbols[selected_symbol]).unwrap()) {
if selected_symbol == symbols.len() - 1 {
selected_symbol = 0;
} else {
selected_symbol += 1;
}
}
false
}
Key::Char('\n') => {
let s = Symb::from_char(&symbols[selected_symbol]);
if let Some(s) = s {
board.play(cursor, s)
} else {
false
}
}
Key::Char(c) => {
let s = Symb::from_char(&c);
if let Some(s) = s {
board.play(cursor, s)
} else {
false
}
}
_ => false,
};
continue 'outer;
@ -124,7 +174,7 @@ fn main() -> Result<()> {
let stdout = HideCursor::from(stdout().into_raw_mode().unwrap());
let mut stdout = stdout.lock();
let mut agent = agents::DiffuseAgent {};
let mut agent = agents::MinMaxTree {};
let a = play(&mut stdout, true, &mut agent)?;
if a.is_done() {
println!(