#!/usr/bin/perl
#######################
# A two player tic-tac-toe game. Each square is an image that will change to
# display an X or O when clicked on by the appropriate player. 
#######################
use warnings;
use strict;
use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser);

print "Content-type: text/html\n\n";


my $a1= param("a1") || ("blank");
my $a2= param("a2") || ("blank");
my $a3= param("a3") || ("blank");

my $b1= param("b1") || ("blank");
my $b2= param("b2") || ("blank");
my $b3= param("b3") || ("blank");

my $c1= param("c1") || ("blank");
my $c2= param("c2") || ("blank");
my $c3= param("c3") || ("blank");

my $who = newplayer(param("whichplayer"));
my $whowas = whowas(param("whichplayer"));

my @winner = ("$whowas","$whowas","$whowas");

&winner; 

print qq (
 <html>
 <head>
  <title>Buttons</title>
 </head>
 <body>
 	<h4>
 	Current player: $who
 	</h4>
  <form method="post">
  <INPUT TYPE= "hidden" NAME="whichplayer" VALUE="$who"> );
  if ($a1 eq "blank") {
  print qq ( <input type="image" src="pics/$a1.jpg" alt="Button1" name="a1" value = "$who"> );}
  else { print qq ( <img src = "pics/$a1.jpg"> ); }
  print qq (
  <INPUT TYPE= "hidden" NAME="a1" VALUE="$a1">
  <input type="image" src="pics/$a2.jpg" alt="Button2" name="a2" value = "$who">
  <INPUT TYPE= "hidden" NAME="a2" VALUE="$a2">
  <input type="image" src="pics/$a3.jpg" alt="Button3" name="a3" value = "$who">
  <INPUT TYPE= "hidden" NAME="a3" VALUE="$a3">
  <br>
  <input type="image" src="pics/$b1.jpg" alt="Button4" name="b1" value = "$who">
  <INPUT TYPE= "hidden" NAME="b1" VALUE="$b1">
  <input type="image" src="pics/$b2.jpg" alt="Button5" name="b2" value = "$who">
  <INPUT TYPE= "hidden" NAME="b2" VALUE="$b2">
  <input type="image" src="pics/$b3.jpg" alt="Button6" name="b3" value = "$who">
  <INPUT TYPE= "hidden" NAME="b3" VALUE="$b3">
  <br>
  <input type="image" src="pics/$c1.jpg" alt="Button7" name="c1" value = "$who">
  <INPUT TYPE= "hidden" NAME="c1" VALUE="$c1">
  <input type="image" src="pics/$c2.jpg" alt="Button8" name="c2" value = "$who">
  <INPUT TYPE= "hidden" NAME="c2" VALUE="$c2">
  <input type="image" src="pics/$c3.jpg" alt="Button9" name="c3" value = "$who">
  <INPUT TYPE= "hidden" NAME="c3" VALUE="$c3">
  </form>
  <FORM NAME="Reset" method="post">
  <INPUT TYPE="submit" NAME="Reset" VALUE="Reset">
  </form>
 </body>
</html>		);


print "<hr> Parameters passed : <br>\n";
foreach my $name ( param ){
print  $name , " => '" , param($name), "'<br>\n";
}
print "<hr>\n";


sub newplayer {
	my ($who)=@_;
	if ($who eq "o")	{
	return "x";
	}
	
	elsif ($who eq "x")	{
	return "o";
	}
	
	return "x";
};

sub whowas {
	my ($who)=@_;
	if ($who eq "o")	{
	return "x";
	}
	
	elsif ($who eq "x")	{
	return "o";
	}
	
	return "blank";
};


sub winner	{
	my @win1=($a1,$a2,$a3);
	my @win2=($b1,$b2,$b3);
	my @win3=($c1,$c2,$c3);
	my @win4=($a1,$b2,$c3);
	my @win5=($c1,$b2,$a3);

	if(
	
	($winner[0] eq $win1[0] && $winner[1] eq $win1[1] && $winner[2] eq $win1[2])
	) {
		print("$who is the winner!");
    }
}