This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn play_game(){ | |
println!("TIC TAC TOE Game"); | |
println!("Enter 1 to 9"); | |
let mut is_won = false; | |
let mut game_over = false; | |
let mut count = 0; | |
let mut board = vec![String::from(" ");9]; // creates 9 elements of String that contains a space | |
display_board(&board); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn display_board(board : &Vec<String>){ | |
println!(" {} | {} | {} ",board[0],board[1],board[2]); | |
println!(" ---| --- | ---"); | |
println!(" {} | {} | {} ",board[3],board[4],board[5]); | |
println!(" ---| --- | ---"); | |
println!(" {} | {} | {} ",board[6],board[7],board[8]); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn get_input() -> String { | |
println!("Enter your choice : "); | |
let mut input = String::new(); | |
io::stdout().flush().unwrap(); | |
io::stdin().read_line(&mut input).expect("Enter correct input"); | |
Command::new("clear").status().unwrap(); // clears the screen | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn plot_player(player : &String, board : &mut Vec<String>, index : &usize) -> (bool,bool) { | |
let mut is_won = false; | |
let mut is_valid = true; | |
if board[index-1] != String::from("X") && board[index-1] != String::from("O") && !is_won { | |
board[index-1] = player.to_string(); | |
is_won = check_win(player.to_string(),board.to_vec()); | |
} else { | |
is_valid = false; | |
} | |
return (is_valid,is_won); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn check_win(player : String, board : Vec<String>) -> bool{ | |
let mut is_win = false; | |
if board[0] == player && board[1] == player && board[2] == player { | |
is_win = true; | |
} else if board[3] == player && board[4] == player && board[5] == player { | |
is_win = true; | |
} else if board[6] == player && board[7] == player && board[8] == player { | |
is_win = true; | |
} else if board[0] == player && board[3] == player && board[6] == player { |