instance = $instance; game::start(); $this->newBoard(); } /** * Purpose: start a new tic tac toe game * Preconditions: none * Postconditions: game is ready to be displayed **/ function newGame() { //setup the game $this->start(); //reset the player $this->player = "X"; $this->totalMoves = 0; //reset the board $this->newBoard(); } function newBoard() { //clear out the board $this->board = array(); //create the board for ($x = 0; $x <= 2; $x++) { for ($y = 0; $y <= 2; $y++) { $this->board[$x][$y] = null; } } } /** * Purpose: run the game until it's tied or someone has won * Preconditions: all $_POST content for this game * Postconditions: game is in play **/ function playGame($gamedata) { if (!$this->isOver() && isset($gamedata[$this->instance . 'move'])) { $this->move($gamedata); } //player pressed the button to start a new game if (isset($gamedata[$this->instance . 'newgame'])) { $this->newGame(); } //display the game $this->displayGame(); } /** * Purpose: display the game interface * Preconditions: none * Postconditions: start a game or keep playing the current game **/ function displayGame() { //while the game isn't over if (!$this->isOver()) { echo "
instance}move\" value=\"Take Turn\" />
It's player {$this->player}'s turn.
instance}newgame\" value=\"New Game\" />
"; } } /** * Purpose: trying to place an X or O on the board * Preconditions: the position they want to make their move * Postconditions: the game data is updated **/ function move($gamedata) { if ($this->isOver()) return; //remove duplicate entries on the board $gamedata = array_unique($gamedata); foreach ($gamedata as $key => $value) { if ($value == $this->player) { //update the board in that position with the player's X or O $coords = explode("_", $key); //make sure we use the data from the right instance if ($coords[0] == $this->instance) { $this->board[$coords[1]][$coords[2]] = $this->player; //change the turn to the next player if ($this->player == "X") $this->player = "O"; else $this->player = "X"; $this->totalMoves++; } } } if ($this->isOver()) return; } /** * Purpose: check for a winner * Preconditions: none * Postconditions: return the winner if found **/ function isOver() { //top row if ($this->board[0][0] && $this->board[0][0] == $this->board[0][1] && $this->board[0][1] == $this->board[0][2]) return $this->board[0][0]; //middle row if ($this->board[1][0] && $this->board[1][0] == $this->board[1][1] && $this->board[1][1] == $this->board[1][2]) return $this->board[1][0]; //bottom row if ($this->board[2][0] && $this->board[2][0] == $this->board[2][1] && $this->board[2][1] == $this->board[2][2]) return $this->board[2][0]; //first column if ($this->board[0][0] && $this->board[0][0] == $this->board[1][0] && $this->board[1][0] == $this->board[2][0]) return $this->board[0][0]; //second column if ($this->board[0][1] && $this->board[0][1] == $this->board[1][1] && $this->board[1][1] == $this->board[2][1]) return $this->board[0][1]; //third column if ($this->board[0][2] && $this->board[0][2] == $this->board[1][2] && $this->board[1][2] == $this->board[2][2]) return $this->board[0][2]; //diagonal 1 if ($this->board[0][0] && $this->board[0][0] == $this->board[1][1] && $this->board[1][1] == $this->board[2][2]) return $this->board[0][0]; //diagonal 2 if ($this->board[0][2] && $this->board[0][2] == $this->board[1][1] && $this->board[1][1] == $this->board[2][0]) return $this->board[0][2]; if ($this->totalMoves >= 9) return "Tie"; } }