-
-
Save ealdthryth/ad8661e9e844f07f91f5636f2e9a77da to your computer and use it in GitHub Desktop.
This file contains 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 | |
$gameName = 'War'; | |
class Deck | |
{ | |
/** Assume cards are face down in deck | |
* Botom card is in position 0 in the array | |
*/ | |
public $suits = ['S', 'H', 'C', 'D']; | |
public $indices = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]; | |
public $deck = []; | |
/** | |
* Returns an array of arrays representing cards | |
* in a specific order. | |
*/ | |
public function __construct() | |
{ | |
$this->create(); | |
} | |
protected function create() | |
{ | |
foreach($this->suits as $suit) { | |
foreach($this->indices as $index) { | |
$this->deck[] = $this->createCard($suit, $index); | |
} | |
} | |
return $this; | |
} | |
/** | |
* Returns an array with suit and index _properties_ | |
* of a playing card. | |
*/ | |
public function createCard($suit, $index) | |
{ | |
return ['suit' => $suit, 'index' => $index]; | |
} | |
/** | |
* Iterate through a deck and print each card. | |
* Returns pointer to object. | |
*/ | |
public function printDeck() | |
{ | |
echo '<pre>'; | |
foreach($this->deck as $card) { | |
$this->printCard($card); | |
} | |
echo '</pre>'; | |
return $this; | |
} | |
/** | |
* Echo a card suitable for HTML display | |
*/ | |
public function printCard($card) | |
{ | |
$suit = ''; | |
if ($card['suit'] == 'S') { | |
$suit = '♠'; | |
} | |
if ($card['suit'] == 'D') { | |
$suit = '♦'; | |
} | |
if ($card['suit'] == 'C') { | |
$suit = '♣'; | |
} | |
if ($card['suit'] == 'H') { | |
$suit = '♥'; | |
} | |
$rank = $card['index']; | |
switch ($rank) { | |
case 11: | |
$rank = 'J'; | |
break; | |
case 12: | |
$rank = 'Q'; | |
break; | |
case 13: | |
$rank = 'K'; | |
break; | |
case 14: | |
$rank = 'A'; | |
break; | |
} | |
echo 'card = ' . $rank . $suit . '<br>'; | |
} | |
/** | |
* Shuffles a deck regardless of the deck's current | |
* state and returns the shuffled deck; | |
*/ | |
public function shuffleDeck() | |
{ | |
shuffle($this->deck); | |
return $this; | |
} | |
/** | |
* Return the top card from the deck, shortening | |
* the deck by one card | |
*/ | |
public function getTopCard() | |
{ | |
$card = array_pop($this->deck); | |
return $card; | |
} | |
/** | |
* Returns the number of cards in the deck | |
*/ | |
public function sizeofDeck() | |
{ | |
return count($this->deck); | |
} | |
/** | |
* Separates cards into 2 arrays by by placing top card from deck | |
* into one hand then the other, putting new card on top of hand, | |
* until there are no cards left in the deck | |
* Two arguments are Hand objects | |
*/ | |
public function deal($hand1, $hand2) | |
{ | |
$numcards = $this->sizeofDeck(); | |
for($i=0; $i<$numcards;) { | |
$hand1->deck[] = $this->getTopCard(); | |
$i++; | |
$hand2->deck[] = $this->getTopCard(); | |
$i++; | |
} | |
return; | |
} | |
/** | |
* Add cards to bottom of deck (front of array) | |
* Argument is Hand object. | |
* Returns number of cards added to dec | |
*/ | |
public function addToBottom($morecards) | |
{ | |
$numcards = $morecards->sizeofDeck(); | |
for($i=0; $i<$numcards; $i++) { | |
$this->addCardToBottom($morecards->getTopCard()); | |
} | |
return $numcards; | |
} | |
/** Adds a card to the bottom of the deck | |
* Argument is card to be added | |
*/ | |
public function addCardToBottom ($card) | |
{ | |
array_unshift($this->deck, $card); // add to bottom of stack of cards | |
return; | |
} | |
/** Adds a card to the top of the deck | |
* Argument is card to be added | |
*/ | |
public function addCardToTop ($card) | |
{ | |
$this->deck[] = $card; // add to top of stack of cards | |
return; | |
} | |
} // end of Deck class | |
class Hand extends Deck | |
{ | |
public function __construct() | |
{ | |
return $this; | |
} | |
} // end of Hand class | |
function battle ($player1, $player2, $battlepile) | |
{ | |
// Make sure both players have cards to play | |
if ($player1->sizeofDeck() == 0) { | |
echo 'Player 1 out of cards!' . '<br>'; | |
return; | |
} | |
if ($player2->sizeofDeck() == 0) { | |
echo 'Player 2 out of cards!' . '<br>'; | |
return; | |
} | |
// Play top card from each players hand onto battle pile | |
$card1 = $player1->getTopCard(); | |
$battlepile->addCardToTop($card1); | |
echo 'Player 1 plays '; $player1->printCard($card1); | |
$card2 = $player2->getTopCard(); | |
$battlepile->addCardToTop($card2); | |
echo 'Player 2 plays '; $player2->printCard($card2); | |
// shuffle battle pile so cards are added to winner's hand in random order | |
$battlepile->shuffleDeck(); | |
// Compare cards, declare winner, and | |
// add all cards from battle pile to winner's hand | |
// If both cards have same rank, declare war | |
if ($card1['index'] > $card2['index']) { | |
echo '<strong>' . 'Player 1 wins this battle' . '</strong>' . '<br>'; | |
$player1->addToBottom($battlepile); | |
} elseif ($card2['index'] > $card1['index']) { | |
echo '<strong>' . 'Player 2 wins this battle' . '</strong>' . '<br>'; | |
$player2->addToBottom($battlepile); | |
} else { | |
echo '<strong>' . "It's WAR!" . '</strong>' . '<br>'; | |
war ($player1, $player2, $battlepile); | |
} | |
return; | |
} | |
function war ($player1, $player2, $battlepile) | |
{ | |
// Make sure both players have cards to play | |
if ($player1->sizeofDeck() == 0) { | |
echo 'Player 1 out of cards!' . '<br>'; | |
return; | |
} | |
if ($player2->sizeofDeck() == 0) { | |
echo 'Player 2 out of cards!' . '<br>'; | |
return; | |
} | |
// One card each face down | |
$battlepile->addCardToTop($player1->getTopCard()); | |
$battlepile->addCardToTop($player2->getTopCard()); | |
// Then usual battle | |
battle ($player1, $player2, $battlepile); | |
return; | |
} | |
// Create the deck and shuffle it | |
$startingdeck = (new Deck())->shuffleDeck()->printDeck(); | |
// echo $startingdeck->sizeofDeck() . '<br>'; | |
// Deal two hands, each with half the cards | |
echo 'Deal 2 hands ... ' . '<br>'; | |
$player1 = new Hand(); // create object | |
$player2 = new Hand(); // create object | |
$startingdeck->deal($player1, $player2); | |
//echo 'Player 1 has ' . $player1->sizeofDeck() . ' cards' . '<br>'; | |
//$player1->printDeck(); | |
//echo 'Player 2 has ' . $player2->sizeofDeck() . ' cards' . '<br>'; | |
//$player2->printDeck(); | |
// create hand for played cards | |
$battlepile = new Hand(); | |
// Battle until one player has no cards | |
while (($player1->sizeofDeck() > 0) && ($player2->sizeofDeck() > 0)) { | |
//for ($i=0; $i < 5; $i++) { | |
battle ($player1, $player2, $battlepile); | |
echo 'Player 1 has ' . $player1->sizeofDeck() . ' cards' . '<br>'; | |
// $player1->printDeck(); | |
echo 'Player 2 has ' . $player2->sizeofDeck() . ' cards' . '<br>'; | |
// $player2->printDeck(); | |
} | |
if ($player1->sizeofDeck() > 0) { | |
echo 'Player 1 WINS!!'; | |
} else { | |
echo 'Player 2 WINS!!'; | |
} | |
exit; | |
?> | |
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | |
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags --> | |
<title><?= $gameName ?></title> | |
<!-- Bootstrap --> | |
<link href="css/bootstrap.min.css" rel="stylesheet"> | |
</head> | |
<body> | |
<div class="container"> | |
<h1><?= $gameName ?></h1> | |
<?php | |
$deck = createDeck(); | |
printDeck($deck); | |
$deck = shuffleDeck($deck); | |
printDeck($deck); | |
$result = deal($deck, 2); | |
$hands = $result['hands']; | |
foreach($hands as $num => $hand) { | |
echo '<h2>Hand ' . $num. '</h2>'; | |
printDeck($hand); | |
} | |
?> | |
</div> | |
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) --> | |
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.3/jquery.min.js"></script> | |
<script src="js/bootstrap.min.js"></script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment