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