-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.php
More file actions
80 lines (63 loc) · 2.43 KB
/
game.php
File metadata and controls
80 lines (63 loc) · 2.43 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
<?php
/**
* Additional reference used for this script:
* Playing Card Suit: https://en.wikipedia.org/wiki/Playing_card_suit
* Cards Visual Reference: http://www.milefoot.com/math/discrete/counting/images/cards.png
*/
require_once 'vendor/autoload.php';
// Instantiate Cards
$cards = new Cards();
// Welcome Message
echo "Welcome to this Card Game Simulation!\n";
echo str_repeat('=', 37) . "\n";
// Ask the user how many players
$number_of_players = readline('Enter number of players [2 to 6]: ');
if ( ! is_numeric($number_of_players)) {
echo $number_of_players . " is a not valid number, please enter a value between 2 and 6, try again!\n";
exit;
}
// Validate min/max number of players
if ($number_of_players < 2 OR $number_of_players > 6) {
echo "Game only allows up to 6 players, you entered {$number_of_players}, try again!\n";
exit;
}
echo "{$number_of_players} players on the game\n";
// "Create" players
$players = [];
for ($player_number = 1; $player_number <= $number_of_players; $player_number++) {
$players[] = [
'name' => 'Player ' . $player_number,
'number' => 'p' . $player_number,
'cards' => []
];
}
// Give cards to players
foreach($players as &$player) {
// Take 5 from deck
$player['cards'] = $cards->take_cards();
// Calc total for given cards
$player['card_total'] = array_reduce($player['cards'], function($carry, $item) {
$carry += $item['numeric_value'];
return $carry;
});
// Sort cards by numeric value, desc
array_multisort(array_column($player['cards'], 'numeric_value'), SORT_DESC, $player['cards']);
}
// Assign main player
$main_player = $players[0];
echo "\tYou are playing as " . $main_player['name'] . "\n";
// Pick first player
$first_player_number = GameHelper::decide_who_goes_first($players);
$player_position = array_search($first_player_number, array_column($players, 'number'));
$first_player = $players[$player_position];
echo $first_player['name'] . " starts the game\n";
// Show cards to the main player
echo "\tYour cards are " . implode(' ', array_column($main_player['cards'], 'card')) . "\n";
// Play first card from deck of cards
$played_cards[] = $cards->play_first_card();
echo "First played card is " . $played_cards[0]['card'] . "\n";
// Main Game Control
while (($command = readline("Enter your option or enter 'q' to quit: ")) !== 'q') {
echo "Command is {$command}\n";
}
echo "Thanks for playing, good bye!\n";