Created
November 9, 2023 03:39
-
-
Save susanBuck/dc87524eb36045adcfac68366f9fedc1 to your computer and use it in GitHub Desktop.
Example for dpurser-dev
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
session_start(); | |
# Check if a game is in progress | |
if (!isset($_SESSION['game_board'])) { | |
# If not, set up a new game board | |
$_SESSION['game_board'] = | |
array( | |
array(0,0,0), | |
array(0,0,0), | |
array(0,0,0) | |
); | |
$_SESSION['turn_count'] = 0; | |
} | |
# Retrieve the game board from the session data | |
$game_board = $_SESSION['game_board']; | |
# Load the view | |
require 'index-view.php'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
session_start(); | |
# Get the game board from the session storage | |
$game_board = $_SESSION['game_board']; | |
$turn_count = $_SESSION['turn_count']; | |
# Take the coordinates from the player's input | |
$row = $_POST['row']; | |
$col = $_POST['col']; | |
# Update the game board with the player's move | |
$game_board[$row][$col] = "X"; | |
# Generate the computer's move (except when there are no moves left) | |
if($turn_count < 4) { | |
while(!isset($found_available_move)) { | |
# Generate a random set of coordinates | |
$row = rand(0, 2); | |
$col = rand(0, 2); | |
# Check that this is a valid move | |
if ($game_board[$row][$col] == 0) { | |
# If so, update the board and the exit the loop | |
$game_board[$row][$col] = "O"; | |
$found_available_move = true; | |
} | |
}; | |
} | |
$_SESSION['game_board'] = $game_board; | |
$_SESSION['turn_count']++; | |
# Load the view | |
header('Location: index.php'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment