#!/usr/bin/perl 
# Title:        3.11Exercises.pl
# Purpose:   	Work on control strucutres and statement blocks.
#	 	<STDIN> input,  data checking, sting, arrays 

# Splash greeting
print "Pick a number between 1 and 10 and I'll guess it.\n";
print "If you trust me, you can type in the number and press ENTER when ready.\n";

#-------------------------------------------------------#
#Input the Answer form the user, or they just hit enter.
#-------------------------------------------------------#
$Answer=<STDIN>;chop($Answer);

#if ( "$Answer" eq ""){
#	# They don't trust me
#	Interactive="yes";
#} else {
#	# They trust me
#	Interactive="no";
#}

#-------------------------------------------------------#
# Guess the answer 5 times and then quit 
# (by testing $#array  size)
#-------------------------------------------------------#
@wrong_guesses=(); $scroe=0;
print "Working on try $#wrong_guesses\n";
while ( $#wrong_guesses < 4 ){
  #
  #  Make a random number guess.
  #
  $guess=int(rand(10));
  print "I am guessing $guess\n";
  #
  # Did we use this guess before?
  #
  foreach $wrong_guess (@wrong_guesses){
    print "checking if guess is unique\n";
    if ( "$wrong_guess" =~ "$guess" ){
      #
      # I guessed it already, pick another number.
      #
      return;   	# I want it to go back to the while statement at line 29.
     }  
  }
  #
  #  If the user entered in the number, 
  #  then don't ask if guess is correct.
  #  
  if ( $Interactive =~ "no"){
    if ( "$guess" =~ "$Answer" ){    
      print "I guessed that $guess is equal to $Answer, ";
      print "and that is correct. \nI will guess ",5-$#," more times.\n";
      exit 1;
    } else {
      print "I guessed that $guess is equal to $Answer ";
      print "and that is not correct.\n Guessing again.\n";
      push (@wrong_guesses, "$guess") ;
    }
  }    
  if ( $Interactive =~ "yes" ){
    # Interactive Test
    print "Is your number ",$guess,"? (type yes or no)\n";
    $good_answer=<STDIN>; chop($good_answer);
    if ( $good_answer =~ "[y,Y][e,E][s,S]" ){
      print "That's because I am so smart!!!\n";
      $scroe++ ;
      print "Scroe is $score\n." ;
      return;
    } elsif ( $good_answer =~ "[N,n][O,o]" ){
      push (@wrong_guesses, "$guess" ) ;
      print "Foo!  I will guess ",$#wrong_guesses," more times.\n";
    } else {
      print "You must enter yes or no.  Please try again\n";
      return;
    }	
  }
}
