<head> <title>Hello COMP4711</title> </head> <body> <?php $game = new Game($_GET['board']); // $game->display(); if ($game->winner('o')) { echo 'You win. Lucky guesses! '; echo '<a href="?board=---------">Replay?</a>'; } else { if ($game->is_draw()) { echo 'It\'s a draw! '; echo '<a href="?board=---------">Replay?</a>'; } else { if ($game->pick_move()) { echo 'I win. Muahahahaha! '; echo '<a href="?board=---------">Replay?</a>'; } else { if ($game->is_draw()) { echo 'It\'s a draw! '; echo '<a href="?board=---------">Replay?</a>'; } } } } $game->display(); class Game { var $position; function __construct($squares)
// check if the board is set if (isset($_GET['board'])) { $position = $_GET['board']; $game = new Game($position); // check different states of the board if ($game->winner('x')) { $won = true; echo 'You win. Lucky guesses!'; } else { if ($game->winner('o')) { $won = true; echo 'I win. Muahahahaha'; } else { $won = false; echo 'No winner yet, but you are losing.'; $game->pick_move(); // no winner so the computer is picking a move } } $game->display(); } class Game { var $position; function __construct($squares) { $this->position = str_split($squares); } public function winner($token) { //check if there is a win
// start new game if no board given, else continue from board state if (isset($_GET['board'])) { $squares = $_GET['board']; } else { $squares = '---------'; } $game = new Game($squares); // assumed player is o and AI is x if ($game->winner('o')) { echo "You win. Lucky guesses!"; } else { if ($game->winner('x')) { echo "I win. Muahahahaha"; } else { // pick_move returns board state after AI makes its move $squares = $game->pick_move(); $game = new Game($squares); if ($game->winner('x')) { echo "I win. Muahahahaha"; } } } $game->display(); ?> </body> </html> <?php class Game { var $position; // couldn't figure out how to overload constructors
$numbers = [0, 0, 0, 0, 0, 0, 0, 0]; //set up an initlal board convertered to numbers. for ($i = 0; $i < 9; $i++) { if ($squares[$i] == 'x') { $numbers[$i] = 1; } else { if ($squares[$i] == 'o') { $numbers[$i] = '3'; } else { $numbers[$i] = 0; } } //otherwise put 0 } $game = new Game($squares); $squares[$game->pick_move($numbers)] = 'o'; //make the AI play $game = new Game($squares); $game->display(); if ($game->winner('x', $squares)) { echo 'You win.'; } else { if ($game->winner('o', $squares)) { echo 'I win.'; } else { echo 'No winner yet.'; } } ?> </body> </html>
<?php $squares = $_GET['board']; $game = new Game($squares); echo '<h1>Tic Tac Toe</h1>'; echo '<h3>World Championship Simulator</h3>'; echo "<h4>You are 'x'</h4>"; if ($game->is_empty()) { echo 'You have the first move.'; } else { if ($game->winner('x')) { echo 'You win. Lucky guesses!'; } else { if ($game->winner('o')) { echo 'I win. Muahahaaa'; } else { if ($game->pick_move() && $game->winner('o')) { echo 'I win. Muahahahaha'; } else { if ($game->is_full()) { echo "Game over. It's a tie this time...."; } else { echo 'So far so good. Your move, if you dare...'; } } } } } $game->display(); echo '<br/>'; echo '<a href="./?board=---------">New Game</a>'; // 0 1 2
echo "<h1>You are player " . $player . "</h1>"; if (isset($_GET['board'])) { $position = $_GET['board']; } else { $position = "---------"; } $game = new Game($position); $game->display(); if ($game->winner('x')) { echo "X wins"; } else { if ($game->winner('o')) { echo "O wins"; } else { echo "No winner yet. "; echo $game->pick_move($player); } } ?> <h1><a href="?board=---------&player=o">Start a new game!</a></h1> </body> </html> <?php //Game class, handles all the game functions class Game { var $position; //Constructor requires the squares of the game in the format of a string //being either dashes or x/o
<title>Tic-Tac-Toe</title> <link rel="stylesheet" type="text/css" href="Styles/style.css"> </head> <body> <h1>Tic-Tac-Toe Game</h1> <?php if (!isset($_GET['board'])) { $squares = '---------'; } else { $squares = $_GET['board']; } $game = new Game($squares); if ($squares == '---------') { $game->display(); } else { if ($game->pick_move() == -1) { echo '<div class="msg">A Tie!</div>'; } else { if ($game->winner('x')) { echo '<div class="msg">You win!</div>'; } else { if ($game->winner('o')) { echo '<div class="msg">I win!</div>'; } else { $game->display(); } } } } ?> <br />