I just started learning PHP @ school and for the third assignment we have to create a TicTacToe game. I have followed a turorial video on youtube and have made a game that is playable at the moment. But it doesnt know whos turn it is. IE: you can keep pressing the submit button and the computer will keep filling in O's without the player to have to fill in a X.
Please can someone explain how i can make the script know who's turn it is? I want to know the logic behind it. So only a code wont help me at all, please explain how you check if the player has filled in a X before you switch turn for instance.
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Boter, Kaas & Eieren</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<?php
$winner = 'niemand';
$box = array('','','','','','','','','');
if (isset($_POST["submit"])){ //When the player hits submit, we retrieve the data
$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); //kijken in welke array, wat is ingevuld
//check if the player has won
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[4]=='x' && $box[8]=='x') ||
($box[2]=='x' && $box[4]=='x' && $box[6]=='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')){
$winner = 'x';
echo "Speler wint";
}
//check if X has played and switch turn to O
$blank = 0; //assume there is no empty box
//check for an empty box
for ($i=0; $i<=8; $i++){
if ($box[$i]==''){
$blank=1;
}
}
//if there is an empty box and no winner yet its O's turn
if ($blank == 1 && $winner == 'niemand'){
$i = rand(0,8);
while ($box[$i]!=''){ //keep looking for an empty box if $i isnt empty
$i = rand(0,8);
}
$box[$i] = "o";
//check if O has won
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[4]=='o' && $box[8]=='o') ||
($box[2]=='o' && $box[4]=='o' && $box[6]=='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')){
$winner = "o";
echo "KI wint";
}
}
//check if it is a draw
if ($blank == 0 && $winner == 'niemand'){
echo "Gelijkspel!";
}
}
?>
<div id="beurt">
<p>
<form action="destroy.php" method="get">
<input type="submit" id="destroy" onClick="windows.location.href'index.php'" value="Begin opnieuw!">
</form>
</p>
</div>
<form id="game" name="tictactoe" method="post">
<?php
//create the grid to play
for ($i=0; $i<=8; $i++){
echo "<input class=\"box\" type=\"text\" name=\"box$i\" value=\"$box[$i]\">";
if ($i==2||$i==5||$i==8){ //put in a break if $i is 2,5 or 8
echo "<br>";
}
}
if ($winner == 'niemand'){
echo "<br><input type=\"submit\" name=\"submit\" id=\"submit\" value=\"Spelen!\"><br></form>";
}
?>
</body>
</html>
Please help me out.