Skip to content

Instantly share code, notes, and snippets.

View JaiSuryaPrabu's full-sized avatar
🎯
Focusing

Jaisurya JaiSuryaPrabu

🎯
Focusing
View GitHub Profile
@JaiSuryaPrabu
JaiSuryaPrabu / main.rs
Created August 20, 2024 15:32
Tic Tac Toe function for checking the player is won or not
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 {
@JaiSuryaPrabu
JaiSuryaPrabu / main.rs
Created August 20, 2024 15:28
Tic Tac Toe function for plotting player mark in the board
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);
@JaiSuryaPrabu
JaiSuryaPrabu / main.rs
Created August 20, 2024 15:25
Tic Tac Toe function for getting player input
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
@JaiSuryaPrabu
JaiSuryaPrabu / main.rs
Created August 20, 2024 15:16
Tic Tac Toe function for displaying the board
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]);
}
@JaiSuryaPrabu
JaiSuryaPrabu / main.rs
Last active August 20, 2024 15:14
Tic Tac Toe play game functionality definition
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);