Skip to content

Commit

Permalink
Merge pull request #19 from lsd-ucsc/fix-ttt-user-brain
Browse files Browse the repository at this point in the history
handle invalid digit in the tic-tac-toe example
  • Loading branch information
shumbo authored Oct 3, 2023
2 parents b37d457 + 9fb59ee commit ca0d9e5
Showing 1 changed file with 3 additions and 0 deletions.
3 changes: 3 additions & 0 deletions chorus_lib/examples/tic-tac-toe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,14 +129,17 @@ impl Brain for UserBrain {
board.draw();
let mut pos = String::new();
loop {
pos.clear();
println!("Player {}: Enter the number", self.player);
std::io::stdin().read_line(&mut pos).unwrap();
if let Ok(pos) = pos.trim().parse::<usize>() {
if pos >= 9 {
println!("{}: Invalid number: {}", self.player, pos);
continue;
}
if board.board[pos] != std::char::from_digit(pos as u32, 10).unwrap() {
println!("{}: Position already taken: {}", self.player, pos);
continue;
}
// Valid position
let mut new_board = board.clone();
Expand Down

0 comments on commit ca0d9e5

Please sign in to comment.