<!DOCTYPE html> <html> <head> <meta http-equip="Content-Type" content="text/html; charset=UTF-8"> <title></title> </head> <body> <?php /* Sets the game board if unavailable */ $game = new Game(!isset($_GET['board']) ? '---------' : $_GET['board']); if ($game->winner('x')) { echo 'x wins'; } else { if ($game->winner('o')) { echo 'o wins'; } else { $game->pick_move(); echo $game->winner('o') ? 'o wins' : 'no winner'; } } $game->display(); echo '<a href="index.php">play again</a>'; ?> </body> </html> <?php class Game { var $position; // an array that remembers the current game state of the board
return '<td><a href="' . $link . '">-</a></td>'; } function pick_move() { for ($pos = 0; $pos <= 8; $pos++) { if ($this->position[$pos] == '-') { $this->position[$pos] = 'x'; } else { echo 'Your Move, Buddy'; } } } //Main Game Logic $squares = $_GET['board']; echo "Welcome to George, the evil Tic-Tac-Toe Game."; if ($squares == null) { $squares = '---------'; } $game = new Game($squares); if ($game->winner('x')) { echo 'You win. Lucky guesses!'; } else { if ($game->winner('o')) { echo 'I win. Muahahahaha'; } else { echo 'No winner yet, but you are losing.'; } } ?> </body> </html>
* Time: 10:06 AM */ //We check if our board was set if (!isset($_GET['board'])) { //If it is not set then allow the player to start the game echo '<form action="index.php" method="GET" >'; echo 'Tic Tac Toe!</br>'; //We set our board to a blank board echo '<input type="hidden" name="board" value="---------">'; echo '<input type="submit" value="Play">'; echo '</form>'; } else { //Start a new game $game = new Game($_GET['board']); //Check if the computer wins if ($game->winner('x')) { echo 'Computer wins!'; reset_board(); } elseif ($game->winner('o')) { echo 'Player wins!'; reset_board(); } else { //If neither won let the computer take its turn $game->pick_move(); //Check if the computer wins if ($game->winner('x')) { echo 'Computer wins!'; reset_board(); } else { $game->display(); }
$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 class Game {
<html lang="en-CA"> <head> <title>Tic Tac Toe</title> <link rel="stylesheet" href="TTT.css"> </head> <body> <?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...'; } } }
if ($this->position[2] == $token && $this->position[4] == $token && $this->position[6] == $token) { return true; } // return false if game is not yet won return false; } } //gets board state if applicable if (isset($_GET['board'])) { $board = $_GET['board']; } else { $board = "---------"; } $game = new Game($board); $done = false; if ($game->winner('X')) { echo 'You win. Lucky guesses!'; $done = true; } else { if ($game->winner('O')) { echo 'I win. Muahahahaha'; $done = true; } else { echo 'No winner yet, but you are losing.'; } } if (implode($game->position) != '---------') { $game->pick_move(); } $game->display(); //displays restart option when game has been won
/** * @covers \Florin\TicTacToe\Game::winner */ public function testPlayer1Won() { $grid = ['X', 'X', 'O', 'X', 'O', null, 'O', null, null]; $game = new Game(self::getPlayersPositions($grid)); $this->assertEquals(1, $game->winner()); }
} } if ($result == 3) { $winner = true; } } //horizontal bottom left to top right if ($this->position[6] == $token && $this->position[4] == $token && $this->position[2] == $token) { $winner = true; } else { if ($this->position[0] == $token && $this->position[4] == $token && $this->position[8] == $token) { $winner = true; } else { $winner = false; } } return $winner; } } $position = $_GET['board']; $game = new Game($position); $game->display(); if ($game->winner('x') == true) { echo 'You win. Lucky guesses!'; } else { if ($game->winner('o') == true) { echo 'I win. muhahahah!'; } else { echo 'No winner yet.'; } }
<?php /* Designer: Jim Parry & Gabriel Seonghyoung Lee Programmer: Gabriel Seonghyoung Lee Date created: January 12, 2016 Revision: January 12, 2016 - File created January 15, 2016 - Implemented tic-tac-toe */ require "game.php"; // Get the current board state from the URL $squares = $_GET['board']; // Check if the current board state is set in the URL if (isset($_GET['board'])) { // Instantiate new Game object with the current board state $gg = new Game($squares); // Display the board state $gg->display(); echo '<br/>'; // Check to see if anyone has won if ($gg->winner('x')) { echo 'You win. Lucky guesses!'; } else { if ($gg->winner('o')) { echo 'I win. Muahahahaha'; } else { echo 'No winner yet, but you are losing.'; } } }