Jump to content

HELP ASAP PLEASE WITH THIS GODFORSAKEN CODE - TIC TAC TOE UNBEATBLE


s0n

Recommended Posts

can someone please look at this and tell me whats wrong, getting an offset error and nothing seems to work.

someone please look at this asap. THANK YOU.

 

<html>
  <head>
    <title>U can't win Tic-Tac-Toe</title>
    <script language="javascript"type="text/javascript" src="tictactoe.js"></script>
   	<link rel="stylesheet" type="text/css" href="tictactoe.css"
  </head>
  <body>
<?php
  $x = "X";
  $o = "O";	

//My Tic Tac Toe Array
    $squares = array("", "", "", "", "", "", "", "", "");

//My Post Variables
    if($HTTP_POST_VARS) 
{
        for($i = 0; $i < count($squares); $i++) 
	{
            $squares[$i]= $HTTP_POST_VARS['move'.$i];
        }
    }

    //Runs the check for win functions and check for finish function.
$winner = checkForWin($squares);
    $finish = checkfinish($squares);


    if($winner == '' && !$finish) 
{
        if(!isFirstMove($squares)) 
	{
            $move = makeMove($squares);
            $squares[$move] = 'o';
            $winner = checkForWin($squares);
        }
    }

//Generates My Table for Tic Tac Toe
    echo "<form name=tictactoe method=post action=tictactoe.php>";
    echo "<input type=hidden name=play value=".$winner.">";
    echo "<table cellspacing=0 cellpadding=0 border=0 width=300 height=300>";

    for($i = 0; $i < count($squares); $i++) 
{
        if(($i+1) % 3 == 1)
        echo '<tr>';
        echo '<td>';
        echo "<input value='" .$squares[$i]."'type=button name=move class=table onclick=makeMove('move".$i."')>";
        echo '<input type=hidden name=\'move'.$i.'\' value=\''.$squares[$i].'\'>';
        echo '</td>';
        if(($i+1) % 3 == 0)
            echo '</tr>';
    }

	echo "</table>";
	echo "</form>";
	echo "<br>";
	echo "<font size=6";
    

//Message that comes up after a tie or loss
    if($winner == 'o') 
	{
		echo 'O Wins, You n00b';
		echo "<br>";
		echo "<a href=http://www.3nfconsulting.com/ftp/web2/sunny/tictactoe.php>Play Again</a>";
	}
    else if($winner == '' && $finish) 
	{
		echo 'Tie Game';
		echo "<br>";
		echo "<a href=http://www.3nfconsulting.com/ftp/web2/sunny/tictactoe.php>Play Again</a>";
	}
    echo "</font></center>";

   
    // check if someone won, it checks by make sure by checking
//each node in the array to see if it matches with the 
//other node and if they match it returns a winner
//it goes through all 8 scenarios of winning
    function checkForWin($squaresarray) 
{
        $winner = '';

        if($squaresarray[0] == $squaresarray[1] && $squaresarray[1] == $squaresarray[2])
            $winner = $squaresarray[0];
        else if($squaresarray[3] == $squaresarray[4] && $squaresarray[4] == $squaresarray[5])
            $winner = $squaresarray[3];
        else if($squaresarray[6] == $squaresarray[7] && $squaresarray[7] == $squaresarray[8])
            $winner = $squaresarray[6];
        else if($squaresarray[0] == $squaresarray[3] && $squaresarray[3] == $squaresarray[6])
            $winner = $squaresarray[0];
        else if($squaresarray[1] == $squaresarray[4] && $squaresarray[4] == $squaresarray[7])
            $winner = $squaresarray[1];
        else if($squaresarray[2] == $squaresarray[5] && $squaresarray[5] == $squaresarray[8])
            $winner = $squaresarray[2];
        else if($squaresarray[0] == $squaresarray[4] && $squaresarray[4] == $squaresarray[8])
            $winner = $squaresarray[0];
        else if($squaresarray[2] == $squaresarray[4] && $squaresarray[4] == $squaresarray[6])
            $winner = $squaresarray[2];

        return $winner;
    }

function isFirstMove($squarearray) 
{
        // check if this is the first move
        $firstmove = true;
        for($i = 0; $i < count($squarearray); $i++) 
	{
            if($squarearray[$i] != '')
                $firstmove = false;
        }
        return $firstmove;
    }

    function makeMove($squarearray) 
{
global $x;
global $o;

	if($squarearray[4] != $x)
	{
		$squarearray[4] = $o;
	}
	else
	{
		$squarearray[6] = $o;
	}
}		

function checkfinish($squaresarray) 
{
        $finish = true;
        for($i = 0; $i < count($squaresarray); $i++) 
	{
            if($squaresarray[$i] == '')
                $finish = false;
        }
        return $finish;
    }


function blockMoves()
  {
  global $x;
  global $o;
    
    if( (($squaresarray[0] == "") && ($squaresarray[3] ==$x) && ($squaresarray[6] == $x)) || 
        (($squaresarray[0] == "") && ($squaresarray[4] ==$x) && ($squaresarray[8] == $x)) ||
        (($squaresarray[0] == "") && ($squaresarray[1] ==$x) && ($squaresarray[2] == $x))
      )
    {
      $squaresarray[0] = 'o';
    }
    else
    {
      if( (($squaresarray[1] == "") && ($squaresarray[0] ==$x) && ($squaresarray[2] == $x)) || 
        (($squaresarray[1] == "") && ($squaresarray[4] ==$x) && ($squaresarray[7] == $x)) 
        )
      {
        $squaresarray[1] = 'o';
      }
      else
      {
        if( (($squaresarray[2] == "") && ($squaresarray[0] ==$x) && ($squaresarray[1] == $x)) || 
            (($squaresarray[2] == "") && ($squaresarray[5] ==$x) && ($squaresarray[8] == $x)) ||
            (($squaresarray[2] == "") && ($squaresarray[4] ==$x) && ($squaresarray[6] == $x))
          )
        {
          $squaresarray[2] = 'o';
        }
        else
        {
          if( (($squaresarray[3] == "") && ($squaresarray[0] ==$x) && ($squaresarray[6] == $x)) || 
              (($squaresarray[3] == "") && ($squaresarray[4] ==$x) && ($squaresarray[5] == $x)) 
            )
          {
            $squaresarray[3] = 'o';
          }
          else
          {
           if( (($squaresarray[4] == "") && ($squaresarray[3] ==$x) && ($squaresarray[5] == $x)) || 
               (($squaresarray[4] == "") && ($squaresarray[1] ==$x) && ($squaresarray[7] == $x)) 
             )
            {
              $squaresarray[4] = 'o';
            }
            else
            {
             if( (($squaresarray[5] == "") && ($squaresarray[2] ==$x) && ($squaresarray[8] == $x)) || 
                 (($squaresarray[5] == "") && ($squaresarray[3] == $x) && ($squaresarray[4] == $x)) 
               )
              {
                $squaresarray[5] = 'o';
              }
              else
              {
               if( (($squaresarray[6] == "") && ($squaresarray[0] ==$x) && ($squaresarray[3] == $x)) || 
                   (($squaresarray[6] == "") && ($squaresarray[2] ==$x) && ($squaresarray[4] == $x)) ||
                   (($squaresarray[6] == "") && ($squaresarray[7] ==$x) && ($squaresarray[8] == $x))
                 )
                {
                  $squaresarray[6] = 'o';
                }
                else
                {
                  if( (($squaresarray[7] == "") && ($squaresarray[1] ==$x) && ($squaresarray[4] == $x)) || 
                     (($squaresarray[7] == "") && ($squaresarray[6] == $x) && ($squaresarray[8] == $x)) 
                    )
                  {
                    $squaresarray[7] = 'o';
                  }
                  else
                  {
                   if( (($squaresarray[8] == "") && ($squaresarray[6] ==$x) && ($squaresarray[7] == $x)) || 
                       (($squaresarray[8] == "") && ($squaresarray[4] ==$x) && ($squaresarray[0] == $x)) ||
                       (($squaresarray[8] == "") && ($squaresarray[2] ==$x) && ($squaresarray[5] == $x))
                     )
                    {
                      $squaresarray[8] ='o';
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }


?>
</body>
</html>

 

THANK YOU

 

(edited by kenrbnsn to add the


tags)

One Problem here...  Tic Tac Toe by its nature can be tied everytime no matter what if every move gets seen.  The checks you have coded in mean that the computer is always going to see the blocks.  However the human mind does not run if-else checks quite so flawlessly.  Without some random chance of failure in the check the computer is gonna beat(or at least tie) you every time. No matter what.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.