Created
November 1, 2023 01:48
-
-
Save susanBuck/049b06ce58947705627210cba91e6adc to your computer and use it in GitHub Desktop.
Example for alilienfeld
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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<title>Project 2 High or Low</title> | |
<link href='/styles.css' rel='stylesheet'> | |
</head> | |
<body> | |
<h2>Instructions</h2> | |
<ul> | |
<li>The computer will pull a random card from a standard deck | |
</li> | |
<li>Aces are represented with 1, Jacks are 11, Queens are 12, and Kings are 13.</li> | |
<li>There are no suits but there is a full deck of cards, so four of each card.</li> | |
<li>Player should pick if the next card drawn will be high, low, or the same.</li> | |
<li>The next card will be drawn by the computer.</li> | |
<li>If they player guesses right they win if they guess wrong they lose</li> | |
</ul> | |
<h2>Game Play</h2> | |
<form action='process.php' method='POST'> | |
<label for='guess'>Will the next card be higher, lower or the same as <b><?php echo $current_card ?></b>?</label> | |
<input type='radio' name='guess' id='high' value='high' | |
<?php echo (!isset($guess) or $guess == 'high') ? 'checked' : '' ?>><label for='high'>High</label> | |
<input type='radio' name='guess' id='low' value='low' | |
<?php echo (isset($guess) and $guess == 'low') ? 'checked' : '' ?>><label for='low'>Low</label> | |
<input type='radio' name='guess' id='tie' value='tie' | |
<?php echo (isset($guess) and $guess == 'tie') ? 'checked' : '' ?>><label for='tie'>Same</label> | |
<button type='submit' class='button'>Submit</button> | |
</form> | |
<p> | |
Cards left in the deck: <?php echo count($deck) ?> | |
</p> | |
<?php if (isset($results)) { ?> | |
<h2>Results</h2> | |
<ul class='results'> | |
<li>The last card was <?php echo $last_card ?>. | |
</li> | |
<li>You guessed the next card would be <b><?php echo $guess ?></b>.</li> | |
<li>The next card was <?php echo $next_card ?>.</li> | |
<li>The answer is <b><?php echo $answer ?></b>.</li> | |
<?php if ($winner) { ?> | |
<li class='won'>You win!</li> | |
<?php } else { ?> | |
<li class='lose'>You lose!</li> | |
<?php } ?> | |
<?php } ?> | |
</ul> | |
<p> | |
<a href='reset.php'>Reset</a> | |
</p> | |
</body> | |
</html> |
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(); | |
// If we're just landing on the index page and cards have not yet been picked | |
// redirect the user to the process page where that can happen | |
if(!isset($_SESSION['current_card'])) { | |
header('Location: process.php'); | |
} | |
// Uncomment the following lines when debugging | |
// echo '<pre>'; | |
// var_dump($_SESSION); | |
// echo '</pre>'; | |
// We can assume if we made it here the redirect did not happen and we should have access to the cards as stored in the session from process.php | |
$current_card = $_SESSION['current_card']; | |
$next_card = $_SESSION['next_card']; | |
$deck = $_SESSION['deck']; | |
// We will have results if the form was submitted to process.php | |
if (isset($_SESSION['results'])) { | |
$last_card = $_SESSION['last_card']; | |
$results = $_SESSION['results']; | |
$winner = $results['winner']; | |
$guess = $results['guess']; | |
$answer = $results['answer']; | |
} | |
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(); | |
// If we have a guess, the form was submitted and we should determine the results | |
if(isset($_POST['guess'])) { | |
$next_card = $_SESSION['next_card']; | |
$current_card = $_SESSION['current_card']; | |
$_SESSION['last_card'] = $current_card; | |
// Was the next card low, high, or the same? | |
if ($current_card > $next_card) { | |
$answer = 'low'; | |
} elseif ($current_card < $next_card) { | |
$answer = 'high'; | |
} else { | |
$answer = 'tie'; | |
} | |
// Was the player’s guess correct? | |
$guess = $_POST['guess']; | |
$winner = $answer == $guess; | |
// Store the results so they can be output when we redirect back to index.php | |
$_SESSION['results'] = [ | |
'guess' => $guess, | |
'winner' => $winner, | |
'answer' => $answer, | |
]; | |
} | |
// If deck does not exist OR there are not enough cards to play again - reset the deck | |
if(!isset($_SESSION['deck']) or count($_SESSION['deck']) < 2) { | |
// I simplified the deck for development/debugging purposes so I could quickly play through the game and test the scenario when the deck is empty | |
$deck = [1,2,3,4,5,6,7,8]; | |
// Otherwise, use the existing deck which exists in the session | |
} else { | |
$deck = $_SESSION['deck']; | |
} | |
// Shuffle the deck and pick the cards | |
shuffle($deck); | |
$current_card = array_pop($deck); | |
$next_card = array_pop($deck); | |
// Store this data so it can be accessed in index.php | |
$_SESSION['current_card'] = $current_card; | |
$_SESSION['next_card'] = $next_card; | |
$_SESSION['deck'] = $deck; | |
// Redirect back to index.php | |
header('Location: index.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(); | |
session_unset(); | |
header('Location: index.php'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment