Advertisement
NelloRizzo

[PHP] Carta da gioco francese

May 8th, 2018
364
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.50 KB | None | 0 0
  1. <?php
  2.  
  3. class Card {
  4.     const HEARTS = 0;
  5.     const DIAMONDS = 1;
  6.     const CLUBS = 2;
  7.     const SPADES = 3;
  8.  
  9.     private $value;
  10.     private $seed;
  11.  
  12.     public function __construct($v, $s) {
  13.         $this->value = $v;
  14.         $this->seed = $s;
  15.     }
  16.  
  17.     public function getValue() { return $this->value; }
  18.     public function getSeed() { return $this->seed; }
  19.  
  20.     public function setValue($v) {
  21.         if ($v < 1 || $v > 13) die("Valore $v non ammesso.");
  22.         $this->value = $v;
  23.     }
  24.     public function setSeed($v) {
  25.         if ($v < 0 || $v > 3) die("Valore $v non ammesso.");
  26.         $this->seed = $v;
  27.     }
  28.  
  29.     public function printme() {
  30.         $s = "";
  31.         switch($this->getValue()) {
  32.             case 1:
  33.                 $s = "A";
  34.                 break;
  35.             case 11:
  36.                 $s = "J";
  37.                 break;
  38.             case 12:
  39.                 $s = "Q";
  40.                 break;
  41.             case 13:
  42.                 $s = "K";
  43.                 break;
  44.             default:
  45.                 $s .= $this->getValue();
  46.                 break;
  47.         }
  48.         $seeds = array("Hearts", "Diamonds", "Clubs", "Spades");
  49.         return $s." of ".$seeds[$this->getSeed()];
  50.     }
  51. }
  52.  
  53. $c = new Card(10, Card::DIAMONDS);
  54. echo $c->printme()."<br/>";
  55. echo "Un mazzo di carte francesi:<br/>";
  56. for($s = Card::HEARTS; $s <= Card::SPADES; ++$s)
  57.     for($v = 1; $v < 14; ++$v)
  58.     {
  59.         $c = new Card($v, $s);
  60.         echo $c->printme()."<br/>";
  61.     }
  62.  
  63. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement