diff options
Diffstat (limited to 'examples/tictactoe.rs')
-rw-r--r-- | examples/tictactoe.rs | 29 |
1 files changed, 15 insertions, 14 deletions
diff --git a/examples/tictactoe.rs b/examples/tictactoe.rs index c8ffdc5..4409155 100644 --- a/examples/tictactoe.rs +++ b/examples/tictactoe.rs @@ -11,9 +11,11 @@ const EMPTY: &'static str = " "; const ROUND: &'static str = "O"; fn main() { - let mut table = table![[EMPTY, EMPTY, EMPTY], - [EMPTY, EMPTY, EMPTY], - [EMPTY, EMPTY, EMPTY]]; + let mut table = table![ + [EMPTY, EMPTY, EMPTY], + [EMPTY, EMPTY, EMPTY], + [EMPTY, EMPTY, EMPTY] + ]; table.printstd(); let stdin = io::stdin(); let mut stdout = io::stdout(); @@ -37,7 +39,7 @@ fn main() { let x = (i - 1) % 3; let y = (i - 1) / 3; { - let mut row = table.get_mut_row(y).unwrap(); + let row = table.get_mut_row(y).unwrap(); if row.get_cell(x).unwrap().to_string() != EMPTY { println!("There's already someone there"); continue; @@ -58,12 +60,10 @@ fn main() { fn get(table: &Table, x: usize, y: usize) -> String { match table.get_row(y) { - Some(r) => { - match r.get_cell(x) { - Some(c) => c.to_string(), - _ => EMPTY.to_string(), - } - } + Some(r) => match r.get_cell(x) { + Some(c) => c.to_string(), + _ => EMPTY.to_string(), + }, _ => EMPTY.to_string(), } } @@ -82,10 +82,11 @@ fn check(table: &Table) -> bool { } let current = get(table, x, y); let c = current.as_str(); - if is(table, c, x + 1, y) && is(table, c, x + 2, y) || - is(table, c, x + 1, y + 1) && is(table, c, x + 2, y + 2) || - x >= 2 && is(table, c, x - 1, y + 1) && is(table, c, x - 2, y + 2) || - is(table, c, x, y + 1) && is(table, c, x, y + 2) { + if is(table, c, x + 1, y) && is(table, c, x + 2, y) + || is(table, c, x + 1, y + 1) && is(table, c, x + 2, y + 2) + || x >= 2 && is(table, c, x - 1, y + 1) && is(table, c, x - 2, y + 2) + || is(table, c, x, y + 1) && is(table, c, x, y + 2) + { println!("Game is over. {} is the winner", current); return true; } |