COB logoCOB honor code logo

BA372 — LAMP Lab

<form action="tictactoe.php" method="get">
<?php
   //Check for winner
   $winner = winner($cells);
   if ($winner == "no winner" && $_GET["move_count"] < 9)
   {
      //Turn message
      echo "Turn: ";
      if ($on_turn == $CROSS)
         echo "CROSSES";
      else
         echo "NOUGHTS";
      echo " ... Choose a field (1-9):<br />\n";
      echo "<input type=\"text\" name=\"user_input\">\n";
      echo "<input type=\"submit\" value=\"Submit Move\">\n";
      echo "<input type=\"hidden\" name=\"1\" value=\"".$cells[0]."\">\n";
      echo "<input type=\"hidden\"
name=\"2\" value=\"".$cells[1]."\">\n";
      echo "
<input type=\"hidden\" name=\"3\" value=\"".$cells[2]."\">\n";
      echo "
<input type=\"hidden\" name=\"4\" value=\"".$cells[3]."\">\n";
      echo "
<input type=\"hidden\" name=\"5\" value=\"".$cells[4]."\">\n";
      echo "
<input type=\"hidden\" name=\"6\" value=\"".$cells[5]."\">\n";
      echo "
<input type=\"hidden\" name=\"7\" value=\"".$cells[6]."\">\n";
      echo "
<input type=\"hidden\" name=\"8\" value=\"".$cells[7]."\">\n";
      echo "
<input type=\"hidden\" name=\"9\" value=\"".$cells[8]."\">\n";
      echo "
<input type=\"hidden\" name=\"move_count\" value=\"".$move_number."\">\n";
      echo "
<input type=\"hidden\" name=\"turn\" value=\"".$on_turn."\">\n";
      echo "
<input type=\"hidden\" name=\"valid\" value=\"".$move_valid."\">\n";
   }
   elseif ($winner == "no winner" && $_GET["move_count"] == 9)
   {
      echo "It's a draw!<br />";
      echo "<input type=\"submit\" value=\"Play Again\">\n";
   }
   else
   {
      echo "The winner is ".$winner."<br />";
      echo "<input type=\"submit\" value=\"Play Again\">\n";
   }
?>
</form>

<?php
   function render_cell($number)
   {
      switch ($number)
      {
        case 0:
           return "&nbsp;";
           break;
        case 1:
           return "X";
           break;
        case -1:
           return "O";
           break;
      }
   }

   function winner($cells)
   {
      $winner = $EMPTY;
      //Check for row winners
      for($r = 0; $r <= 6; $r += 3)
         if ($cells[$r] != $EMPTY && $cells[$r] == $cells[$r+1] && $cells[$r] == $cells[$r+2])
            $winner = $cells[$r];

      //Check for column winners
      for($c = 0; $c <= 2; $c += 1)
         if ($cells[$c] != $EMPTY && $cells[$c] == $cells[$c+3] && $cells[$c+3] == $cells[$c+6])
            $winner = $cells[$c];

      //Check for diagonal winners
      if ($cells[0] != $EMPTY && $cells[0] == $cells[4] && $cells[0] == $cells[8])
         $winner = $cells[0];

      if ($cells[2] != $EMPTY && $cells[2] == $cells[4] && $cells[2] == $cells[6])
         $winner = $cells[2];

      switch ($winner)
      {
        case 0:
           return "no winner";
           break;
        case 1:
           return "CROSSES";
           break;
        case -1:
           return "NOUGHTS";
           break;
      }
   }
?>
</body></html>