From 2febd8de67e93ab9d68dfe665303b4cf0e9b2f5d Mon Sep 17 00:00:00 2001 From: Mariano Riefolo Date: Fri, 13 Sep 2024 08:43:38 +0200 Subject: [PATCH] Wrote program for Day 50 --- README.md | 2 +- Week-08/Day-50_Tic-Tac-Toe/day50/Cargo.toml | 6 +++ Week-08/Day-50_Tic-Tac-Toe/day50/src/lib.rs | 48 +++++++++++++++++++ Week-08/Day-50_Tic-Tac-Toe/day50/src/main.rs | 34 +++++++++++++ .../day50/tests/examples.rs | 20 ++++++++ 5 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 Week-08/Day-50_Tic-Tac-Toe/day50/Cargo.toml create mode 100644 Week-08/Day-50_Tic-Tac-Toe/day50/src/lib.rs create mode 100644 Week-08/Day-50_Tic-Tac-Toe/day50/src/main.rs create mode 100644 Week-08/Day-50_Tic-Tac-Toe/day50/tests/examples.rs diff --git a/README.md b/README.md index 5a0adbb..b7de00e 100644 --- a/README.md +++ b/README.md @@ -96,7 +96,7 @@ We encourage you to share your progress and ask questions in the Discussions sec | Day #47 | [Zip It](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-07/Day-47_Zip-It) | :white_check_mark: | | Day #48 | [Christmas Tree](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-07/Day-48_Christmas-Tree) | :white_check_mark: | | Day #49 | [Swimming Pool](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-07/Day-49_Swimming-Pool) | :white_check_mark: | -| Day #50 | [Tic Tac Toe](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-08/Day-50_Tic-Tac-Toe) | :white_large_square: | +| Day #50 | [Tic Tac Toe](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-08/Day-50_Tic-Tac-Toe) | :white_check_mark: | | Day #51 | [Asteroid Collision](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-08/Day-51_Asteroid-Collision) | :white_large_square: | | Day #52 | [Switch On The Gravity](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-08/Day-52_Switch-On-The-Gravity) | :white_large_square: | | Day #53 | [Javelin Parabolic Throw](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-08/Day-53_Javelin-Parabolic-Throw) | :white_large_square: | diff --git a/Week-08/Day-50_Tic-Tac-Toe/day50/Cargo.toml b/Week-08/Day-50_Tic-Tac-Toe/day50/Cargo.toml new file mode 100644 index 0000000..80cb5da --- /dev/null +++ b/Week-08/Day-50_Tic-Tac-Toe/day50/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "day50" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/Week-08/Day-50_Tic-Tac-Toe/day50/src/lib.rs b/Week-08/Day-50_Tic-Tac-Toe/day50/src/lib.rs new file mode 100644 index 0000000..ea83c62 --- /dev/null +++ b/Week-08/Day-50_Tic-Tac-Toe/day50/src/lib.rs @@ -0,0 +1,48 @@ +#[derive(Debug, PartialEq)] +pub enum Player { + X, + O, +} + +pub fn tic_tac_toe(board: &[[char; 3]; 3]) -> Option { + let winning_patterns = [ + // Horizontal + [(0, 0), (0, 1), (0, 2)], + [(1, 0), (1, 1), (1, 2)], + [(2, 0), (2, 1), (2, 2)], + // Vertical + [(0, 0), (1, 0), (2, 0)], + [(0, 1), (1, 1), (2, 1)], + [(0, 2), (1, 2), (2, 2)], + // Diagonal + [(0, 0), (1, 1), (2, 2)], + [(0, 2), (1, 1), (2, 0)], + ]; + + let mut winner = None; + + for pattern in &winning_patterns { + let mut x_count = 0; + let mut o_count = 0; + for (i, j) in pattern { + match board[*i][*j] { + 'X' => x_count += 1, + 'O' => o_count += 1, + _ => (), + } + } + if x_count == 3 { + if winner.is_some() { + return None; + } + winner = Some(Player::X); + } else if o_count == 3 { + if winner.is_some() { + return None; + } + winner = Some(Player::O); + } + } + + winner +} diff --git a/Week-08/Day-50_Tic-Tac-Toe/day50/src/main.rs b/Week-08/Day-50_Tic-Tac-Toe/day50/src/main.rs new file mode 100644 index 0000000..0f7b683 --- /dev/null +++ b/Week-08/Day-50_Tic-Tac-Toe/day50/src/main.rs @@ -0,0 +1,34 @@ +use day50::tic_tac_toe; + +fn read_board() -> Result<[[char; 3]; 3], Box> { + let mut board = [['#'; 3]; 3]; + for row in board.iter_mut() { + let mut line = String::new(); + std::io::stdin().read_line(&mut line)?; + let cells: Vec = line + .split_whitespace() + .map(|cell| cell.chars().next().unwrap()) + .collect(); + if cells.len() != 3 { + return Err("Invalid number of cells".into()); + } + row.copy_from_slice(&cells); + } + Ok(board) +} + +fn main() { + println!("Enter the tic tac toe board row by row using X, O, # (empty) separated by spaces"); + let board = match read_board() { + Ok(board) => board, + Err(e) => { + eprintln!("Error reading board: {}", e); + return; + } + }; + let winner = tic_tac_toe(&board); + match winner { + Some(winner) => println!("Winner: {:?}", winner), + None => println!("No winner"), + } +} diff --git a/Week-08/Day-50_Tic-Tac-Toe/day50/tests/examples.rs b/Week-08/Day-50_Tic-Tac-Toe/day50/tests/examples.rs new file mode 100644 index 0000000..d1a3594 --- /dev/null +++ b/Week-08/Day-50_Tic-Tac-Toe/day50/tests/examples.rs @@ -0,0 +1,20 @@ +#[cfg(test)] +mod examples { + use day50::{tic_tac_toe, Player}; + + #[test] + fn example1() { + let board = [['X', 'O', 'O'], ['O', 'X', 'O'], ['O', '#', 'X']]; + assert_eq!(tic_tac_toe(&board), Some(Player::X)); + } + #[test] + fn example2() { + let board = [['X', 'O', 'O'], ['O', 'X', 'O'], ['X', '#', 'O']]; + assert_eq!(tic_tac_toe(&board), Some(Player::O)); + } + #[test] + fn example3() { + let board = [['X', 'X', 'O'], ['O', 'X', 'O'], ['X', 'O', '#']]; + assert_eq!(tic_tac_toe(&board), None); + } +}