0% found this document useful (0 votes)
6 views3 pages

BlackJack Assignment

This PHP script implements a simple betting game where users can place bets, draw cards, and stop the game. It manages user sessions to track money, hand, and betting status, and includes functions to handle bet placement, card drawing, and game stopping. The game resets if the user exceeds a count of 21 or after each round of play.

Uploaded by

Joao Pedro
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

BlackJack Assignment

This PHP script implements a simple betting game where users can place bets, draw cards, and stop the game. It manages user sessions to track money, hand, and betting status, and includes functions to handle bet placement, card drawing, and game stopping. The game resets if the user exceeds a count of 21 or after each round of play.

Uploaded by

Joao Pedro
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

<?

php
session_start(); // Start the session to track money and hand across requests

// Initialize money if it doesn't exist yet


if (!isset($_SESSION['money'])) {
$_SESSION['money'] = 100; // Starting money
}

// Initialize the hand if it doesn't exist yet


if (!isset($_SESSION['hand'])) {
$_SESSION['hand'] = []; // Empty hand at the start
}

// Initialize bet_ready if it doesn't exist yet


if (!isset($_SESSION['bet_ready'])) {
$_SESSION['bet_ready'] = 0; // Starting bet_ready status as false
}

if (!isset($_SESSION['count'])) {
$_SESSION['count'] = 0;
}

if (!isset($_SESSION['dealer'])) {
$_SESSION['dealer'] = rand(14, 21);
}

// Ensure bet_placed is tracked in the session


if (!isset($_SESSION['bet_placed'])) {
$_SESSION['bet_placed'] = 0;
}

$money = $_SESSION['money']; // Get the current money value from the session
$deck = [
"2C", "3C", "4C", "5C", "6C", "7C", "8C", "9C", "10C", "JC", "QC", "KC", "AC",
"2D", "3D", "4D", "5D", "6D", "7D", "8D", "9D", "10D", "JD", "QD", "KD", "AD",
"2H", "3H", "4H", "5H", "6H", "7H", "8H", "9H", "10H", "JH", "QH", "KH", "AH",
"2S", "3S", "4S", "5S", "6S", "7S", "8S", "9S", "10S", "JS", "QS", "KS", "AS"
];

// Process the bet when the form is submitted


function handleBetPlacement($postData, &$session, $money) {
if (isset($postData['placeBet']) && $session['bet_ready'] == 0) {
$bet = isset($postData['bet']) ? intval($postData['bet']) : 0; // Bet amount (default to 0)

// Check if the bet is a valid numeric value and if the user has enough money
if (is_numeric($bet) && $bet > 0) {
if ($bet <= $money) {
$session['money'] -= $bet; // Subtract bet from money
$session['bet_placed'] = $bet; // Store the bet in the session
$session['bet_ready'] = 1; // Mark the bet as placed
} elseif ($money == 0) {
echo "You lost.<br>";
$session['money'] = 100; // Reset money if lost
} else {
echo "You don't have enough money to place this bet.<br>";
}
}
}
}

// Handle the draw action


function handleDrawCard($postData, &$session, &$deck) {
if (isset($postData["draw"])) {
if ($session['bet_ready'] == 1) { // Ensure a bet is placed before drawing
// Pick a random card
$cardIndex = array_rand($deck); // Get a random index
$card = $deck[$cardIndex]; // Get the card value at that index

// Add card to the hand


$session['hand'][] = $card;

// Add the value of the card to the count


if (is_numeric($card[0])) {
$session['count'] += intval($card[0]);
} elseif ($card[0] === "A") {
$session['count'] += ($session['count'] + 11 <= 21) ? 11 : 1;
} else { // For face cards
$session['count'] += 10;
}

// If count exceeds 21, reset hand and count


if ($session['count'] > 21) {
echo "You exceeded 21.<br>";
$session['hand'] = []; // Clear hand
$session['count'] = 0; // Reset count
$session['bet_ready'] = 0; // Reset bet status
$session['dealer'] = rand(14, 21);
}

// Display the current hand (all drawn cards)


echo "Your hand: " . implode(", ", $session['hand']);
echo "<br>Current count: " . $session['count'];
} else {
echo "Please place a bet before drawing.<br>";
}
}
}

// Handle the stop action


function handleStopGame($postData, &$session) {
if (isset($postData["stop"])) {
if ($session['bet_ready'] == 1) {
if ($session['count'] > $session['dealer'] && $session['count'] <= 21) {
echo "You won!<br>";
echo "Dealer had: " . $session['dealer'] . "<br>";
$session['money'] += $session['bet_placed'] * 2; // Add winnings
} else {
echo "You lost!<br>";
echo "Dealer had: " . $session['dealer'] . "<br>";
}

// Reset the game state


$session['bet_ready'] = 0;
$session['count'] = 0;
$session['hand'] = [];
$session['dealer'] = rand(14, 21);
$session['bet_placed'] = 0; // Reset the bet
}
}
}

// Calling the functions


handleBetPlacement($_POST, $_SESSION, $_SESSION['money']);
handleDrawCard($_POST, $_SESSION, $deck);
handleStopGame($_POST, $_SESSION);

?>

<!-- HTML Form to interact with the game -->


<form action="index.php" method="post">
<label id="Money">Your money: <?php echo $_SESSION['money']; ?></label>
<input type="text" name="bet" id="bet" placeholder="BET">
<input type="submit" id="placeBet" name="placeBet" value="Place Bet"><br><br>
<input type="submit" id="draw" name="draw" value="Draw">
<input type="submit" id="stop" name="stop" value="Stop">
</form>

</body>
</html>

You might also like