Last active
October 13, 2023 15:52
-
-
Save susanBuck/2dcfa30e5c4a8ed77ec34a7b5e957c6f to your computer and use it in GitHub Desktop.
War Card Game
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>War (card game) Simulator</title> | |
<meta charset='utf-8'> | |
<style> | |
.card { | |
border: 1px solid black; | |
display: inline-block; | |
padding: 5px; | |
} | |
th, | |
td { | |
border-bottom: 1px solid #ddd; | |
padding: 10px; | |
} | |
tr:nth-child(even) { | |
background-color: #f2f2f2; | |
} | |
td { | |
text-align: center; | |
} | |
</style> | |
</head> | |
<body> | |
<h1>War (card game) Simulator</h1> | |
<h2>Mechanics</h2> | |
<ul> | |
<li>Each player starts with half the deck (26 cards), shuffled in a random order.</li> | |
<li>For each round, a card is picked from the “top” of each player’s cards.</li> | |
<li>Whoevers card is highest wins that round and keeps both cards.</li> | |
<li>If the two cards are of the same value, it’s a tie and those cards are discarded.</li> | |
<li>The player who ends up with 0 cards is the loser.</li> | |
</ul> | |
<h2>Results</h2> | |
<ul> | |
<li>Rounds played: <?= $roundsPlayed ?> | |
</li> | |
<li>Winner: <?= $winner ?> | |
</li> | |
</ul> | |
<h2>Rounds</h2> | |
<table> | |
<tr> | |
<th>Round #</th> | |
<th>Player 1 card</th> | |
<th>Player 2 card</th> | |
<th>Winner</th> | |
<th>Player 1 cards left</th> | |
<th>Player 2 cards left</th> | |
</tr> | |
<?php foreach ($rounds as $round => $details) { ?> | |
<tr> | |
<td><?php echo $round ?> | |
</td> | |
<td><span class='card'><?php echo $details['player1Card']; ?></span> | |
</td> | |
<td><span class='card'><?php echo $details['player2Card']; ?> | |
</td> | |
<td><?php echo $details['winner']; ?> | |
</td> | |
<td><?php echo $details['player1CardCount']; ?> | |
</td> | |
<td><?php echo $details['player2CardCount']; ?> | |
</td> | |
</tr> | |
<?php } ?> | |
</table> | |
</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 | |
$ranked = ['A', 'K', 'Q', 'J', '9', '8', '7', '6', '5', '4', '3', '2']; | |
$suites = ['♠', '♥', '♦', '♣']; | |
foreach ($suites as $suit) { | |
foreach ($ranked as $rank) { | |
$cards[] = $rank .' '. $suit; | |
} | |
} | |
shuffle($cards); | |
$player1Cards = array_slice($cards, 0, 26); | |
$player2Cards = array_slice($cards, 26); | |
while (count($player1Cards) > 0 and count($player2Cards) > 0) { | |
$player1Card = array_pop($player1Cards); | |
$player2Card = array_pop($player2Cards); | |
$player1CardValue = determineValue($player1Card); | |
$player2CardValue = determineValue($player2Card); | |
if ($player1CardValue == $player2CardValue) { | |
$winner = 'Tie'; | |
} elseif ($player1CardValue > $player2CardValue) { | |
$winner = 'Player 1'; | |
array_push($player1Cards, $player1Card, $player2Card); | |
} else { | |
$winner = 'Player 2'; | |
array_push($player2Cards, $player1Card, $player2Card); | |
} | |
# Track our results in a $rounds array so we can output it in the view | |
$rounds[] = [ | |
'player1Card' => $player1Card, | |
'player2Card' => $player2Card, | |
'player1CardCount' => count($player1Cards), | |
'player2CardCount' => count($player2Cards), | |
'winner' => $winner, | |
]; | |
} | |
$roundsPlayed = count($rounds); | |
if (count($player1Cards) == 0) { | |
$winner = 'Player 1'; | |
} else { | |
$winner = 'Player 2'; | |
} | |
function determineValue($card) | |
{ | |
$faceCardValues = ['Ace' => 14, 'King' => 13, 'Queen' => 12, 'Jack' => 11]; | |
$card = explode(' ', $card); | |
$value = $card[0]; | |
if (array_key_exists($value, $faceCardValues)) { | |
$value = $faceCardValues[$value]; | |
} | |
return (int) $value; | |
} | |
require 'index-view.php'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment