summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPierre-Henri Symoneaux <pierre.henri.symoneaux@gmail.com>2018-09-23 00:02:53 +0200
committerPierre-Henri Symoneaux <pierre.henri.symoneaux@gmail.com>2018-09-23 00:02:53 +0200
commit118049c28f6fa40f391cb8deaa661eac74113159 (patch)
treedff88c132faed16aa64b60b16179e08b0e6149b7
parent1803803c74ec764a488cd84cb59c145d17639537 (diff)
Updated tictactoe example to change the table in place
-rw-r--r--examples/tictactoe.rs15
1 files changed, 13 insertions, 2 deletions
diff --git a/examples/tictactoe.rs b/examples/tictactoe.rs
index 4409155..727fc8e 100644
--- a/examples/tictactoe.rs
+++ b/examples/tictactoe.rs
@@ -1,5 +1,7 @@
#[macro_use]
extern crate prettytable;
+extern crate term;
+
use prettytable::Table;
use std::io;
@@ -16,24 +18,28 @@ fn main() {
[EMPTY, EMPTY, EMPTY],
[EMPTY, EMPTY, EMPTY]
];
- table.printstd();
+ let mut height = table.printstd();
let stdin = io::stdin();
let mut stdout = io::stdout();
let mut current = CROSS;
+ let mut terminal = term::stdout().unwrap();
loop {
let mut line = String::new();
print!("{} plays > ", current);
+ height += 1;
stdout.flush().unwrap();
stdin.read_line(&mut line).expect("Cannot read input");
let i = match usize::from_str(line.trim()) {
Ok(i) => i,
_ => {
println!("Bad input");
+ height += 1;
continue;
}
};
if i < 1 || i > 9 {
println!("Bad input, should be between 1 and 9");
+ height += 1;
continue;
}
let x = (i - 1) % 3;
@@ -42,11 +48,16 @@ fn main() {
let row = table.get_mut_row(y).unwrap();
if row.get_cell(x).unwrap().to_string() != EMPTY {
println!("There's already someone there");
+ height += 1;
continue;
}
row.set_cell(cell!(current), x).unwrap();
}
- table.printstd();
+ for _ in 0..height {
+ terminal.cursor_up().unwrap();
+ terminal.delete_line().unwrap();
+ }
+ height = table.printstd();
if check(&table) {
return;
}