Wrote program for Day 50
This commit is contained in:
parent
dfc105e2c8
commit
2febd8de67
@ -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: |
|
||||||
|
6
Week-08/Day-50_Tic-Tac-Toe/day50/Cargo.toml
Normal file
6
Week-08/Day-50_Tic-Tac-Toe/day50/Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
[package]
|
||||||
|
name = "day50"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
48
Week-08/Day-50_Tic-Tac-Toe/day50/src/lib.rs
Normal file
48
Week-08/Day-50_Tic-Tac-Toe/day50/src/lib.rs
Normal 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
|
||||||
|
}
|
34
Week-08/Day-50_Tic-Tac-Toe/day50/src/main.rs
Normal file
34
Week-08/Day-50_Tic-Tac-Toe/day50/src/main.rs
Normal 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"),
|
||||||
|
}
|
||||||
|
}
|
20
Week-08/Day-50_Tic-Tac-Toe/day50/tests/examples.rs
Normal file
20
Week-08/Day-50_Tic-Tac-Toe/day50/tests/examples.rs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user