I am new to coding and I'm trying to learn PHP by making a Tic Tac Toe game.
The PHP below is an attempt to make the server place an "O" in random in an empty slot each submit.
$empties = array();
$gameBoard = array();
$rand = 1;
for ($i = 0; $i < 9; $i++) {
$gameBoard[$i] = "";
}
if (isset($_POST['gameBoard'])) {
$gameBoard = $_POST['gameBoard'];
for ($i = 0; $i < 9; $i++) {
if ($gameBoard[$rand] !== 'X' and $gameBoard[$rand] !== 'O') {
$gameBoard[$rand] = 'O';
break;
}
$rand = rand(1, 9);
}
However it does ,on random,work for a while and then throws undefined offset error or does not place an "O" at all.
This is the full code: http://phpfiddle.org/main/code/wza8-3niu
Your help is most appreciated.