On the moment the tic tac toe game works perfect, the only thing is, the AI is just checking if there is a square open and if so fill it in. I've been searching for algorithms but and I managed to find some but it is to advanced for this code and to much for my brain to compile.
Here is the code, it's rather simple and as you can see in the code that he will check if there are blank spaces and then fills in a 'o' if it's empty.
<?php
$winner = 'n';
$box = array('','','','','','','','','');
if(isset($_POST["submitbtn"]))
{
$box[0] = $_POST["box0"];
$box[1] = $_POST["box1"];
$box[2] = $_POST["box2"];
$box[3] = $_POST["box3"];
$box[4] = $_POST["box4"];
$box[5] = $_POST["box5"];
$box[6] = $_POST["box6"];
$box[7] = $_POST["box7"];
$box[8] = $_POST["box8"];
//print_r($box);
//check if 'x' wins
if (($box[0] == 'x' && $box[1] == 'x' && $box[2] == 'x')||
($box[3] == 'x' && $box[4] == 'x' && $box[5] == 'x')||
($box[6] == 'x' && $box[7] == 'x' && $box[8] == 'x')||
($box[0] == 'x' && $box[3] == 'x' && $box[6] == 'x')||
($box[1] == 'x' && $box[4] == 'x' && $box[7] == 'x')||
($box[2] == 'x' && $box[5] == 'x' && $box[8] == 'x')||
($box[0] == 'x' && $box[4] == 'x' && $box[8] == 'x')||
($box[2] == 'x' && $box[4] == 'x' && $box[6] == 'x'))
{
$winner = 'x';
echo "x wins";
}
//check if blank
$blank = 0;
for($i=0; $i<=8; $i++)
{
if ($box[$i] == '')
{
$blank = 1; //if blank set true
//print_r($blank);
}
}
if ($blank == 1 && $winner == 'n')
{
$i = rand() % 9;
while ($box[$i] != '')
{
$i = rand() % 9;
}
$box[$i] = 'o'; //place a 'o' in array number
//check if 'o' wins
if (($box[0] == 'o' && $box[1] == 'o' && $box[2] == 'o')||
($box[3] == 'o' && $box[4] == 'o' && $box[5] == 'o')||
($box[6] == 'o' && $box[7] == 'o' && $box[8] == 'o')||
($box[0] == 'o' && $box[3] == 'o' && $box[6] == 'o')||
($box[1] == 'o' && $box[4] == 'o' && $box[7] == 'o')||
($box[2] == 'o' && $box[5] == 'o' && $box[8] == 'o')||
($box[0] == 'o' && $box[4] == 'o' && $box[8] == 'o')||
($box[2] == 'o' && $box[4] == 'o' && $box[6] == 'o'))
{
$winner = 'o';
echo "o wins";
}
}
if ($blank == 0 && $winner != 'o' && $winner != 'x')
{
$winner = 't';
echo "tied game";
}
}
?>
<html>
<html>
<head>
<title>tic tac toe</title>
<style type="text/css">
.box
{
border: 1px solid #000000;
width: 100px;
height: 100px;
font-size: 60px;
text-align: center;
}
.go
{
width: 200px;
font-size: 30px;
}
.playagain
{
width: 200px;
font-size: 30px;
}
</style>
</head>
<body>
<form name="tictactoe" method="post" action="tic.php">
<?php //create the board
for($i=0; $i<=8; $i++)
{
printf('<input type="text" name="box%s" value="%s" class="box">',$i,$box[$i]);
if ($i == 2 || $i == 5 || $i == 8 )
{
echo "<br>";
}
}
if($winner == 'n')
{
echo "<input type='submit' name='submitbtn' value='go' class='go'>";
}
else
{
echo "<input class='playagain' type='submit' name='newgame' value='play again'
onclick = 'window.location.href=\"tic.php\"'>";
}
?>
</form>
</body>
</html>
Please give me some advice, you don't need to complete the code just give me some guidance.
Aucun commentaire:
Enregistrer un commentaire