-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcards.php
More file actions
executable file
·238 lines (193 loc) · 6.11 KB
/
cards.php
File metadata and controls
executable file
·238 lines (193 loc) · 6.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<h1>PHP war demo</h1>
<link rel="stylesheet" type="text/css" href="cardstyle.css">
<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL | E_STRICT);
class Card{
public $value_num;
public $suit_num;
public static $suits = array("spades", "hearts", "clubs", "diamonds");
public static $values = array( "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "jack", "queen", "king", "ace");
function __construct($value_num, $suit_num){
$this->value_num = $value_num;
$this->suit_num = $suit_num;
}
// Compare cards
// return 1 if this card is greater, -1 if other_card is greater, 0 if same
function compare($other_card){
// This greater
if (($this->value_num) > ($other_card->value_num)) {
return 1;
}
// Other greater
else if (($this->value_num) < ($other_card->value_num)) {
return -1;
}
// Equal
else{
return 0;
}
}
//Print Card info
function to_str(){
return "The " . $this->card_number() . " of " . $this->card_suit();
}
// Get string of number
function card_number(){
return Card::$values[$this->value_num];
}
// Get string of name
function card_suit(){
return Card::$suits[$this->suit_num];
}
}
class Deck{
private $cards;
public $points;
function __construct($full_deck){
$this->cards = array();
$this->points = 0;
// Create each card in a deck if needed
if($full_deck){
for ($i=0; $i < count(Card::$suits); $i++) {
for ($j=0; $j < count(Card::$values); $j++) {
$new_card = new Card($j, $i);
array_push($this->cards, $new_card);
}
}
}
}
// Deal cards in each deck
function deal($left_deck, $right_deck){
while (count($this->cards) > 1) {
$card1 = array_pop($this->cards);
$card2 = array_pop($this->cards);
array_push($left_deck->cards, $card1);
array_push($right_deck->cards, $card2);
}
}
// Print deck info for dev purposes
function deck_dump(){
echo "Card count: " . count($this->cards) . "<br/>";
foreach ($this->cards as $card) {
echo $card->to_str();
}
}
// Add a point to the deck's score
function add_point(){
$this->points = $this->points + 1;
}
// Shuffle deck
function shuffle(){
shuffle($this->cards);
}
// Draw a card
function draw(){
return array_pop($this->cards);
}
// Add card to deck
function add_card($card){
array_unshift($this->cards, $card);
}
// Give spoils to winning deck
function give_spoils($spoils){
while (count($spoils) > 0) {
$card = array_pop($spoils);
array_push($this->cards, $card);
}
}
// Give cards to victor
function forfeit($victor){
while (count($this->cards) > 0 ) {
$card = $this->draw();
$victor->add_card($card);
}
}
// Count cards in deck
function card_count(){
return count($this->cards);
}
}
// source deck
$source_deck = new Deck(true);
// player decks
$left_deck = new Deck(false);
$right_deck = new Deck(false);
$source_deck->shuffle();
$source_deck->deal($left_deck, $right_deck);
// Play war
$i = 0;
while ($left_deck->card_count() > 0 && $right_deck->card_count()) {
$i++;
$left_card = $left_deck->draw();
$right_card = $right_deck->draw();
echo "<div class ='match'> Match " . $i . ": " . $left_card->to_str() . " vs. " . $right_card->to_str() . "<br/>";
// Left wins basic fight
if ($left_card->compare($right_card) == 1) {
echo "<span class='left'>Left Deck wins</span></div>";
$left_deck->add_card($left_card);
$left_deck->add_card($right_card);
}
// Right wins basic fight
else if ($left_card->compare($right_card) == -1){
echo "<span class='right'>Right Deck wins</span></div>";
$right_deck->add_card($right_card);
$right_deck->add_card($left_card);
}
// War
else{
echo "It's <span class='war'>WAR</span>!</div>";
$spoils = array();
// Add initial cards to the pile
array_push($spoils, $left_card);
array_push($spoils, $right_card);
// Make sure each side can fight
if ($left_deck->card_count() > 3 && $right_deck->card_count() > 3) {
// Throw three extras into the pile
for ($j=0; $j < 3; $j++) {
$left_tribute = $left_deck->draw();
$right_tribute = $right_deck->draw();
echo "<div><span class='left'>Left Deck</span> adds " . $left_tribute->to_str() . ", <span class='right'>Right Deck</span> adds " . $right_tribute->to_str() . "</div>";
array_push($spoils, $left_tribute);
array_push($spoils, $right_tribute);
}
// Cards to represent eeach deck in war
$left_card = $left_deck->draw();
$right_card = $right_deck->draw();
echo "<div class ='match'> War: " . $left_card->to_str() . " vs. " . $right_card->to_str() . "<br/>";
// Left wins the war
if ($left_card->compare($right_card) == 1){
echo "<span class='left'>Left Deck wins</span></div>";
array_push($spoils, $left_card);
array_push($spoils, $right_card);
$left_deck->give_spoils($spoils);
// Right wins the war
}else{
echo "<span class='right'>Right Deck wins</span></div>";
array_push($spoils, $left_card);
array_push($spoils, $right_card);
$right_deck->give_spoils($spoils);
}
}
// Someone can't fight
else if ($left_deck->card_count() <= 3){
echo "but left deck can't fight!";
$right_deck->give_spoils($spoils);
$left_deck->forfeit($right_deck);
}
else{
echo "but right deck can't fight!";
$left_deck->give_spoils($spoils);
$right_deck->forfeit($left_deck);
}
}
echo "<div class='score'> Standings: <span class='left'>Left Deck " . $left_deck->card_count() . " cards</span>, <span class='right'>Right Deck " . $right_deck->card_count() . " cards</span></div>";
}
if ($left_deck->card_count()) {
echo "<h2><span class='left'>Left Deck wins!</span></h2>";
}elseif ($right_deck->card_count()) {
echo "<h2><span class='right'>Right Deck wins!</span></h2>";
}else{
echo "<h2><span class='tie'>It was a Tie!</span></h2>";
}
?>