Advertisement
Guest User

Yet another simple Tic-tac-toe game on Rust programming lang

a guest
Jul 19th, 2017
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 5.62 KB | None | 0 0
  1. extern crate rand;
  2.  
  3. use std::io;
  4. use std::io::prelude::*;
  5. use std::fmt::{self, Formatter, Display};
  6.  
  7. #[derive(Debug, Clone, Copy, PartialEq)]
  8. enum Values {
  9.     X,
  10.     Y,
  11.     O
  12. }
  13.  
  14. impl Display for Values {
  15.     fn fmt(&self, f: &mut Formatter) -> fmt::Result {
  16.         write!(f, "{}", self)
  17.     }
  18. }
  19.  
  20. #[derive(Debug)]
  21. struct Cell {
  22.     x: i8,
  23.     y: i8,
  24.     value: Values
  25. }
  26.  
  27. #[derive(Debug)]
  28. struct Row(Cell, Cell, Cell);
  29.  
  30. #[derive(Debug)]
  31. struct Map(Row, Row, Row);
  32.  
  33. impl Display for Map {
  34.     fn fmt(&self, f: &mut Formatter) -> fmt::Result {
  35.         write!(f, "1 {:?}[1]    {:?}[2] {:?}[3]\n\
  36.                    2 {:?}[1]    {:?}[2] {:?}[3]\n\
  37.                    3 {:?}[1]    {:?}[2] {:?}[3]",
  38.         &((self.0).0).value, &((self.0).1).value, &((self.0).2).value,
  39.         &((self.1).0).value, &((self.1).1).value, &((self.1).2).value,
  40.         &((self.2).0).value, &((self.2).1).value, &((self.2).2).value
  41.         )
  42.     }
  43. }
  44.  
  45. fn do_step(game_map: &mut Map, input: &str, who: Values) -> bool {
  46.     let x: i8 = match input.chars().nth(0).unwrap() {
  47.         '1' => 1i8,
  48.         '2' => 2i8,
  49.         '3' => 3i8,
  50.         _   => 0i8
  51.     };
  52.     if x == 0 {return false};
  53.     let y: i8 = match input.chars().nth(2).unwrap() {
  54.         '1' => 1i8,
  55.         '2' => 2i8,
  56.         '3' => 3i8,
  57.         _   => 0i8
  58.     };
  59.     if y == 0 {return false};
  60.     let row = &mut game_map.0;
  61.     let mut cell = &mut row.0;
  62.     if x == cell.x && y == cell.y && cell.value == Values::O {
  63.         cell.value = who;
  64.         return true;
  65.     };
  66.     let mut cell = &mut row.1;
  67.     if x == cell.x && y == cell.y && cell.value == Values::O {
  68.         cell.value = who;
  69.         return true;
  70.     };
  71.     let mut cell = &mut row.2;
  72.     if x == cell.x && y == cell.y && cell.value == Values::O {
  73.         cell.value = who;
  74.         return true;
  75.     };
  76.     let row = &mut game_map.1;
  77.     let mut cell = &mut row.0;
  78.     if x == cell.x && y == cell.y && cell.value == Values::O {
  79.         cell.value = who;
  80.         return true;
  81.     };
  82.     let mut cell = &mut row.1;
  83.     if x == cell.x && y == cell.y && cell.value == Values::O {
  84.         cell.value = who;
  85.         return true;
  86.     };
  87.     let mut cell = &mut row.2;
  88.     if x == cell.x && y == cell.y && cell.value == Values::O {
  89.         cell.value = who;
  90.         return true;
  91.     };
  92.     let row = &mut game_map.2;
  93.     let mut cell = &mut row.0;
  94.     if x == cell.x && y == cell.y && cell.value == Values::O {
  95.         cell.value = who;
  96.         return true;
  97.     };
  98.     let mut cell = &mut row.1;
  99.     if x == cell.x && y == cell.y && cell.value == Values::O {
  100.         cell.value = who;
  101.         return true;
  102.     };
  103.     let mut cell = &mut row.2;
  104.     if x == cell.x && y == cell.y && cell.value == Values::O {
  105.         cell.value = who;
  106.         return true;
  107.     };
  108.     false
  109. }
  110.  
  111. fn anyone_wins(game_map: &Map) -> Values {
  112.     let row1 = &game_map.0;
  113.     let row2 = &game_map.1;
  114.     let row3 = &game_map.2;
  115.     if (row1.0).value == (row1.1).value && (row1.0).value == (row1.2).value {
  116.         return (row1.0).value;
  117.     } else if (row2.0).value == (row2.1).value && (row2.0).value == (row2.2).value {
  118.         return (row2.0).value;
  119.     } else if (row3.0).value == (row3.1).value && (row3.0).value == (row3.2).value {
  120.         return (row3.0).value;
  121.     } else if (row1.0).value == (row2.0).value && (row1.0).value == (row3.0).value {
  122.         return (row1.0).value;
  123.     } else if (row1.1).value == (row2.1).value && (row1.1).value == (row3.1).value {
  124.         return (row1.1).value;
  125.     } else if (row1.2).value == (row2.2).value && (row1.2).value == (row3.2).value {
  126.         return (row1.2).value;
  127.     } else if (row1.0).value == (row2.1).value && (row1.0).value == (row3.2).value {
  128.         return (row1.0).value;
  129.     } else if (row1.2).value == (row2.1).value && (row1.2).value == (row3.0).value {
  130.         return (row1.2).value;
  131.     };
  132.     Values::O
  133. }
  134.  
  135. fn main() {
  136.     use Values::{X, Y, O};
  137.     let mut winner = O;
  138.     let mut game_map = Map(
  139.         Row(
  140.             Cell {x: 1, y: 1, value: O},
  141.             Cell {x: 2, y: 1, value: O},
  142.             Cell {x: 3, y: 1, value: O}
  143.         ),
  144.         Row(
  145.             Cell {x: 1, y: 2, value: O},
  146.             Cell {x: 2, y: 2, value: O},
  147.             Cell {x: 3, y: 2, value: O}
  148.         ),
  149.         Row(
  150.             Cell {x: 1, y: 3, value: O},
  151.             Cell {x: 2, y: 3, value: O},
  152.             Cell {x: 3, y: 3, value: O}
  153.         )
  154.     );
  155.     let mut rng = rand::thread_rng();
  156.     let mut counter = 0;
  157.     io::stdout().write(("Hi! You play for `X`.".to_owned() + "\n").as_bytes()).unwrap();
  158.     loop {
  159.         println!("{}", game_map);
  160.         io::stdout().write("Input your move as `x y`, e.g. `1 1` where x = 1 and y = 1 (x: 1, 2, 3; y: 1, 2, 3):\n".as_bytes()).unwrap();
  161.         let mut input = "".to_owned();
  162.         io::stdin().read_line(&mut input)
  163.                    .expect("Failed to read line");
  164.         let step = do_step(&mut game_map, &input.trim(), X);
  165.         if !step {
  166.             let msg = "Invalid input: ".to_owned();
  167.             io::stdout().write((msg + &input.trim() + "\n").as_bytes()).unwrap();
  168.             continue;
  169.         };
  170.        
  171.         counter += 1;
  172.         if counter == 5 {
  173.             break;
  174.         }
  175.  
  176.         loop {
  177.             let mut opp_input = "".to_owned();
  178.             #[allow(unused_variables)]
  179.             for i in 1..3 {
  180.                 let sample = rand::sample(&mut rng, 1..4, 1)[0];
  181.                 let s = match sample {
  182.                     1 => "1",
  183.                     2 => "2",
  184.                     3 => "3",
  185.                     _ => ""
  186.                 };
  187.                 opp_input.push_str(s);
  188.                 opp_input.push_str(" ");
  189.             }
  190.             let step = do_step(&mut game_map, &opp_input.trim(), Y);
  191.             if step {
  192.                 break;
  193.             } else {
  194.                 let msg = "Invalid input from opponent: ".to_owned();
  195.                 io::stdout().write((msg + &opp_input.trim() + "\n").as_bytes()).unwrap();
  196.             };
  197.         }
  198.         if counter > 2 {
  199.             winner = anyone_wins(&game_map);
  200.             if winner != O {break};
  201.         }
  202.     }
  203.     println!("{}", game_map);
  204.     println!("{:?}", "Game over!");
  205.     println!("{:?} win.", winner);
  206. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement