Skip to content

Instantly share code, notes, and snippets.

@JaiSuryaPrabu
Last active August 20, 2024 15:14
Show Gist options
  • Save JaiSuryaPrabu/7ac1f2b1a513659f2249b320ac43f27f to your computer and use it in GitHub Desktop.
Save JaiSuryaPrabu/7ac1f2b1a513659f2249b320ac43f27f to your computer and use it in GitHub Desktop.
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);
while !is_won && !game_over && count < 9 {
let player : String = if let true = count % 2 == 0 { String::from("X") } else { String::from("O") };
println!("Current Player - {} ",player);
let choice = get_input();
let index : usize = choice.parse().unwrap_or(0);
let mut is_valid : bool = true;
if choice != String::from("e"){
(is_valid,is_won) = plot_player(&player,&mut board,&index);
display_board(&board);
} else if choice == String::from("e"){
end_game();
game_over = true;
}
if is_valid {
count += 1;
}
if is_won{
println!("{} won the game",player);
game_over = true;
}
}
end_game();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment