Wrote program for Day 50

This commit is contained in:
Mariano Riefolo 2024-09-13 08:43:38 +02:00
parent dfc105e2c8
commit 2febd8de67
5 changed files with 109 additions and 1 deletions

View File

@ -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 #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 #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 #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 #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 #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: | | 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: |

View File

@ -0,0 +1,6 @@
[package]
name = "day50"
version = "0.1.0"
edition = "2021"
[dependencies]

View File

@ -0,0 +1,48 @@
#[derive(Debug, PartialEq)]
pub enum Player {
X,
O,
}
pub fn tic_tac_toe(board: &[[char; 3]; 3]) -> Option<Player> {
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
}

View File

@ -0,0 +1,34 @@
use day50::tic_tac_toe;
fn read_board() -> Result<[[char; 3]; 3], Box<dyn std::error::Error>> {
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<char> = 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"),
}
}

View File

@ -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);
}
}