Skip to content
Snippets Groups Projects
Select Git revision
  • 49fa57308a339cde3ab1db2e7cfa99e63b7f30a3
  • main default protected
  • 11-flip-coin
  • 10-generics
  • 09.5-before-main-generic
  • 09-modules
  • 08-Display
  • 07-trait-derive
  • 06-trait
  • 05-unit-tests
  • 04-impl-for-board
  • 03-board-struct
  • 02-typed-squares
  • 01-simple-board
14 results

main.rs

Blame
  • main.rs 1.45 KiB
    mod board;
    
    use board::tictactoe::Board as TicTacToe;
    use board::Game;
    use std::io;
    use std::io::prelude::*;
    
    fn prompt<Player>(turn: Player)
    where
        Player: std::fmt::Display,
    {
        print!("{}>", turn);
        io::stdout().flush().unwrap();
    }
    
    fn play<G, Move, Player>(mut board: G)
    where
        G: Game<Move, Player> + std::fmt::Display,
        Player: Copy + std::fmt::Display + Default + std::ops::Not<Output = Player>,
        Move: std::str::FromStr,
    {
        let mut stdin = std::io::stdin().lock();
        let mut player = Player::default();
    
        println!("Let start");
        println!("{board}");
        prompt(player);
    
        loop {
            let mut input = String::default();
            stdin.read_line(&mut input).unwrap();
            let str_move = input.trim_end();
            if let Ok(m) = str_move.parse() {
                board.do_move(player, &m);
                println!("{board}");
                if board.has_won(player) {
                    println!("Victory!");
                    return;
                }
                player = !player;
            }
            prompt(player);
        }
    }
    
    fn main() {
        let board = TicTacToe::new();
        //let board = board::coin_flip::FlipCoin::new(42);
        play(board);
    }
    
    // Will only be build in 'test' configuration
    #[cfg(test)]
    // A module where the tests are located, more on modules later
    mod tests {
        #[test]
        fn should_pass() {
            assert!(true);
        }
        #[test]
        #[should_panic]
        fn should_panic() {
            panic!("Doing wrong things on purpose here!");
        }
    }