#!/usr/bin/perl

use Term::ANSIColor;

sub ValidNumber
{
if ( $input_number =~ /^\d+$/ &&  $input_number <=10 && $input_number >= 0  )
  {
	$valid_number=$input_number;
	return;
   } else { 	
   	print "You must enter a number between 1 and 10.\n";
   	print "Your input was $input_number, so I am skipping it.\n";
   	print "Sorry.\n";
	$valid_number="false";	
	
   }
}

#----------------------------------------#
#   Method 1 Perl Cookbook, pg 521 	 #
#----------------------------------------#
use Term::Cap ;
$OSPEED=9600;
eval {
	require POSIX;
	my $termios=POSIX::Termios->new();
	$termios->getattr;
	$OSPEED= $termios->getospeed;
};
$terminal = Term::Cap->Tgetent({OSPEED=>$OSPEED});
$terminal->Tputs('cl',1,STDOUT);
$clear=$terminal->Tputs('cl');
$clear= `clear`;
print $clear;

$MyScore=0;
$YourScore=0;
#------------------------------------------#
#   Main loop:				   #
#------------------------------------------#
do {
print $clear;
@bad_guess=();
$guess="";
print 	color("red"),	"###############################################################",color("reset"),"\n", 
	color("white"),	"Presenting:                                                    ",color("reset"),"\n",
	color("white"),	"       Exersize 3.11                                           ",color("reset"),"\n",
	color("red"),	"###############################################################",color("reset"),"\n"	;
print 	color("magenta"),"\tYou pick a number between 1 and 10.",color("reset"),"\n", 
	color("yellow"),"\tI guess the number 5 times befor you win.",color("reset"),"\n",
	color("magenta"),"\tOnce a number has been used, it can't be used again.",color("reset"),"\n"	;
print 	color("red"),	"\tTo Exit Program press :",color("green on_blue blink bold"),"Ctrl-c",color("reset"),"\n\n"	;
print   color("green"),	"YourScore: ",$YourScore,
	color("reset"),"                                          ",
	color("blue"),"MyScore: ",$MyScore,
	color("reset"),"\n";
	
	
print "enter a number\n",color("green");
$input_number=<STDIN>; chomp($input_number);
print color("reset");

ValidNumber();
$answer=$valid_number;

# While the number of unique guesses is less than 5, keep guessing
while ( @bad_guess < 5 ){
	if ( "$answer" =~ "false" ){
		last;
	}

	# Generate random number from 1 to 10
 	$guess=int(rand(10))+1;
	
 	# Show output of what the number is and what our answer is.	
	print "\nComputer chose   ",color("blue"),$guess,color("reset"),"\n";
	
	# Did the computer guess the user's answer?
	if ( "$answer" == "$guess" ){
		print 	color("blue"),"-------------------------------",color("reset"),"\n", 
			color("blue"),"Guessed your number. I win!!!  ",color("reset"),"\n",
			color("blue"),"-------------------------------",color("reset"),"\n";
			$MyScore++;
		last;
	} else {
		# If @bad_guess is empty, add it to the @bad_guess, 
		# This is a hack to get it to add the first element to the array, 
		#  or foreach below will fail.
		
		if ( $#bad_guess == -1 ){
			unshift ( @bad_guess, $guess );	
			print "Bad Guess Array: ",color("green"),"@bad_guess",color("reset"),"\n";
			next;
		} else {
			# See if number was was guessed before.
			# TR pointed me to perlcookbook recipe 4.12
			#  For each bad guess, if the current guess is equal
			#  then found maker = 1, and exit foreach loop.
			#  If the marker does not equal 1, add the guess.
			my $found;
			foreach my $string (@bad_guess){
				if ( "$guess" == "$string" ) {
					$found=1;
					last;
				}
			}
			if ( $found) {
				print "We have guessed that before: $guess\n";
			} else {
				unshift ( @bad_guess, $guess );
				print "Bad Guess Array: ",color("green"),"@bad_guess",color("reset"),"\n";
			}
		}
	}
}      
if ( @bad_guess == 5 ){
		print  "$#bad_guess \n";
		print 	color("red"),"###############################",color("reset"),"\n", 
			color("red"),"Biffed it 5 times.  You  win!!!",color("reset"),"\n",
			color("red"),"###############################",color("reset"),"\n";
			$YourScore++;
}

print "Press Any ENTER to play again\n";
$fo=<STDIN>;

} while (1);

