3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Card { private $suit; private $val; public function __construct($val, $suit) { $values = ['J' => 11, 'Q' => 12, 'K' => 13, 'A' => 14]; $suits = array( 'H' => 'Hearts', 'C' => 'Clubs', 'D' => 'Diamonds', 'S' => 'Spades' ); $this->suit = $suits[$suit]; $this->val = $values[$val] ?? $val; } public function getSuit() { return $this->suit; } public function getVal() { return $this->val; } } class Hand { private $cards; private $matches = array(); public function __construct($hand) { $cards = explode(' ', $hand); foreach ($cards as $card) { $val = substr($card, 0, -1); $suit = strtoupper(substr($card, -1)); $this->cards[] = new Card($val, $suit); } $this->values = array_map(function ($card) { return $card->getVal(); }, $this->cards); $this->pairs = $this->toaks = $this->foaks = []; // _T_hree _O_f _A_ _K_ind and _F_our _O_f _A_ _K_ind $this->possibleStraight = true; $this->hands = array_count_values($this->values); if (max($this->hands) > 1) { $this->possibleStraight = false; } foreach($this->hands as $face => $count) { if ($count === 2) { $this->pairs[] = $face; } if ($count === 3) { $this->toaks[] = $face; } if ($count === 4) { $this->foaks[] = $face; } } } public function testMethods() { var_dump($this->getHighCard()); var_dump($this->getPairs()); var_dump($this->getToaks()); var_dump($this->getFoaks()); var_dump($this->isFullHouse()); var_dump($this->isStraight()); } public function getCards() { return $this->cards; } public function getValues() { return $this->values; } public function getHighCard() { return max($this->getValues()); } public function getPairs(){ return $this->pairs; } public function getToaks(){ return $this->toaks; } public function getFoaks() { return $this->foaks; } public function hasAce() { return isset($this->hands['14']); } public function isFullHouse() { return count($this->pairs) == 1 && count($this->toaks) == 1; } public function isStraight(){ if (! $this->possibleStraight) { return false; } // the rest is up to you if ($this->hasAce()) { } } } $h = new Hand('6h 6d 6s 2c 2h'); $h->testMethods(); echo "\n\nSecond hand:\n"; $h = new Hand('6h 7d 8s 9c 10h'); $h->testMethods();
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/UrYau
function name:  (null)
number of ops:  14
compiled vars:  !0 = $h
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  123     0  E >   NEW                                              $1      'Hand'
          1        SEND_VAL_EX                                              '6h+6d+6s+2c+2h'
          2        DO_FCALL                                      0          
          3        ASSIGN                                                   !0, $1
  124     4        INIT_METHOD_CALL                                         !0, 'testMethods'
          5        DO_FCALL                                      0          
  126     6        ECHO                                                     '%0A%0ASecond+hand%3A%0A'
  127     7        NEW                                              $5      'Hand'
          8        SEND_VAL_EX                                              '6h+7d+8s+9c+10h'
          9        DO_FCALL                                      0          
         10        ASSIGN                                                   !0, $5
  128    11        INIT_METHOD_CALL                                         !0, 'testMethods'
         12        DO_FCALL                                      0          
         13      > RETURN                                                   1

Function %00%7Bclosure%7D%2Fin%2FUrYau%3A45%240:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/UrYau
function name:  {closure}
number of ops:  5
compiled vars:  !0 = $card
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   45     0  E >   RECV                                             !0      
          1        INIT_METHOD_CALL                                         !0, 'getVal'
          2        DO_FCALL                                      0  $1      
          3      > RETURN                                                   $1
          4*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FUrYau%3A45%240

Class Card:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/UrYau
function name:  __construct
number of ops:  13
compiled vars:  !0 = $val, !1 = $suit, !2 = $values, !3 = $suits
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    7     0  E >   RECV                                             !0      
          1        RECV                                             !1      
    8     2        ASSIGN                                                   !2, <array>
   10     3        ASSIGN                                                   !3, <array>
   17     4        FETCH_DIM_R                                      ~7      !3, !1
          5        ASSIGN_OBJ                                               'suit'
          6        OP_DATA                                                  ~7
   18     7        FETCH_DIM_IS                                     ~9      !2, !0
          8        COALESCE                                         ~10     ~9
          9        QM_ASSIGN                                        ~10     !0
         10        ASSIGN_OBJ                                               'val'
         11        OP_DATA                                                  ~10
   19    12      > RETURN                                                   null

End of function __construct

Function getsuit:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/UrYau
function name:  getSuit
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   22     0  E >   FETCH_OBJ_R                                      ~0      'suit'
          1      > RETURN                                                   ~0
   23     2*     > RETURN                                                   null

End of function getsuit

Function getval:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/UrYau
function name:  getVal
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   26     0  E >   FETCH_OBJ_R                                      ~0      'val'
          1      > RETURN                                                   ~0
   27     2*     > RETURN                                                   null

End of function getval

End of class Card.

Class Hand:
Function __construct:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 7, Position 2 = 30
Branch analysis from position: 7
2 jumps found. (Code = 78) Position 1 = 8, Position 2 = 30
Branch analysis from position: 8
1 jumps found. (Code = 42) Position 1 = 7
Branch analysis from position: 7
Branch analysis from position: 30
2 jumps found. (Code = 43) Position 1 = 59, Position 2 = 61
Branch analysis from position: 59
2 jumps found. (Code = 77) Position 1 = 63, Position 2 = 81
Branch analysis from position: 63
2 jumps found. (Code = 78) Position 1 = 64, Position 2 = 81
Branch analysis from position: 64
2 jumps found. (Code = 43) Position 1 = 67, Position 2 = 70
Branch analysis from position: 67
2 jumps found. (Code = 43) Position 1 = 72, Position 2 = 75
Branch analysis from position: 72
2 jumps found. (Code = 43) Position 1 = 77, Position 2 = 80
Branch analysis from position: 77
1 jumps found. (Code = 42) Position 1 = 63
Branch analysis from position: 63
Branch analysis from position: 80
Branch analysis from position: 75
Branch analysis from position: 70
Branch analysis from position: 81
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 81
Branch analysis from position: 61
Branch analysis from position: 30
filename:       /in/UrYau
function name:  __construct
number of ops:  83
compiled vars:  !0 = $hand, !1 = $cards, !2 = $card, !3 = $val, !4 = $suit, !5 = $count, !6 = $face
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   36     0  E >   RECV                                             !0      
   37     1        INIT_FCALL                                               'explode'
          2        SEND_VAL                                                 '+'
          3        SEND_VAR                                                 !0
          4        DO_ICALL                                         $7      
          5        ASSIGN                                                   !1, $7
   39     6      > FE_RESET_R                                       $9      !1, ->30
          7    > > FE_FETCH_R                                               $9, !2, ->30
   40     8    >   INIT_FCALL                                               'substr'
          9        SEND_VAR                                                 !2
         10        SEND_VAL                                                 0
         11        SEND_VAL                                                 -1
         12        DO_ICALL                                         $10     
         13        ASSIGN                                                   !3, $10
   41    14        INIT_FCALL                                               'strtoupper'
         15        INIT_FCALL                                               'substr'
         16        SEND_VAR                                                 !2
         17        SEND_VAL                                                 -1
         18        DO_ICALL                                         $12     
         19        SEND_VAR                                                 $12
         20        DO_ICALL                                         $13     
         21        ASSIGN                                                   !4, $13
   42    22        NEW                                              $17     'Card'
         23        SEND_VAR_EX                                              !3
         24        SEND_VAR_EX                                              !4
         25        DO_FCALL                                      0          
         26        FETCH_OBJ_W                                      $15     'cards'
         27        ASSIGN_DIM                                               $15
         28        OP_DATA                                                  $17
   39    29      > JMP                                                      ->7
         30    >   FE_FREE                                                  $9
   45    31        INIT_FCALL                                               'array_map'
         32        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FUrYau%3A45%240'
         33        SEND_VAL                                                 ~20
         34        FETCH_OBJ_R                                      ~21     'cards'
         35        SEND_VAL                                                 ~21
         36        DO_ICALL                                         $22     
         37        ASSIGN_OBJ                                               'values'
         38        OP_DATA                                                  $22
   46    39        ASSIGN_OBJ                                       ~25     'foaks'
         40        OP_DATA                                                  <array>
         41        ASSIGN_OBJ                                       ~24     'toaks'
         42        OP_DATA                                                  ~25
         43        ASSIGN_OBJ                                               'pairs'
         44        OP_DATA                                                  ~24
   47    45        ASSIGN_OBJ                                               'possibleStraight'
         46        OP_DATA                                                  <true>
   49    47        INIT_FCALL                                               'array_count_values'
         48        FETCH_OBJ_R                                      ~28     'values'
         49        SEND_VAL                                                 ~28
         50        DO_ICALL                                         $29     
         51        ASSIGN_OBJ                                               'hands'
         52        OP_DATA                                                  $29
   51    53        INIT_FCALL                                               'max'
         54        FETCH_OBJ_R                                      ~30     'hands'
         55        SEND_VAL                                                 ~30
         56        DO_ICALL                                         $31     
         57        IS_SMALLER                                               1, $31
         58      > JMPZ                                                     ~32, ->61
   52    59    >   ASSIGN_OBJ                                               'possibleStraight'
         60        OP_DATA                                                  <false>
   55    61    >   FETCH_OBJ_R                                      ~34     'hands'
         62      > FE_RESET_R                                       $35     ~34, ->81
         63    > > FE_FETCH_R                                       ~36     $35, !5, ->81
         64    >   ASSIGN                                                   !6, ~36
   56    65        IS_IDENTICAL                                             !5, 2
         66      > JMPZ                                                     ~38, ->70
   57    67    >   FETCH_OBJ_W                                      $39     'pairs'
         68        ASSIGN_DIM                                               $39
         69        OP_DATA                                                  !6
   60    70    >   IS_IDENTICAL                                             !5, 3
         71      > JMPZ                                                     ~41, ->75
   61    72    >   FETCH_OBJ_W                                      $42     'toaks'
         73        ASSIGN_DIM                                               $42
         74        OP_DATA                                                  !6
   64    75    >   IS_IDENTICAL                                             !5, 4
         76      > JMPZ                                                     ~44, ->80
   65    77    >   FETCH_OBJ_W                                      $45     'foaks'
         78        ASSIGN_DIM                                               $45
         79        OP_DATA                                                  !6
   55    80    > > JMP                                                      ->63
         81    >   FE_FREE                                                  $35
   69    82      > RETURN                                                   null

End of function __construct

Function testmethods:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/UrYau
function name:  testMethods
number of ops:  31
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   72     0  E >   INIT_FCALL                                               'var_dump'
          1        INIT_METHOD_CALL                                         'getHighCard'
          2        DO_FCALL                                      0  $0      
          3        SEND_VAR                                                 $0
          4        DO_ICALL                                                 
   73     5        INIT_FCALL                                               'var_dump'
          6        INIT_METHOD_CALL                                         'getPairs'
          7        DO_FCALL                                      0  $2      
          8        SEND_VAR                                                 $2
          9        DO_ICALL                                                 
   74    10        INIT_FCALL                                               'var_dump'
         11        INIT_METHOD_CALL                                         'getToaks'
         12        DO_FCALL                                      0  $4      
         13        SEND_VAR                                                 $4
         14        DO_ICALL                                                 
   75    15        INIT_FCALL                                               'var_dump'
         16        INIT_METHOD_CALL                                         'getFoaks'
         17        DO_FCALL                                      0  $6      
         18        SEND_VAR                                                 $6
         19        DO_ICALL                                                 
   76    20        INIT_FCALL                                               'var_dump'
         21        INIT_METHOD_CALL                                         'isFullHouse'
         22        DO_FCALL                                      0  $8      
         23        SEND_VAR                                                 $8
         24        DO_ICALL                                                 
   77    25        INIT_FCALL                                               'var_dump'
         26        INIT_METHOD_CALL                                         'isStraight'
         27        DO_FCALL                                      0  $10     
         28        SEND_VAR                                                 $10
         29        DO_ICALL                                                 
   78    30      > RETURN                                                   null

End of function testmethods

Function getcards:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/UrYau
function name:  getCards
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   81     0  E >   FETCH_OBJ_R                                      ~0      'cards'
          1      > RETURN                                                   ~0
   82     2*     > RETURN                                                   null

End of function getcards

Function getvalues:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/UrYau
function name:  getValues
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   85     0  E >   FETCH_OBJ_R                                      ~0      'values'
          1      > RETURN                                                   ~0
   86     2*     > RETURN                                                   null

End of function getvalues

Function gethighcard:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/UrYau
function name:  getHighCard
number of ops:  7
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   89     0  E >   INIT_FCALL                                               'max'
          1        INIT_METHOD_CALL                                         'getValues'
          2        DO_FCALL                                      0  $0      
          3        SEND_VAR                                                 $0
          4        DO_ICALL                                         $1      
          5      > RETURN                                                   $1
   90     6*     > RETURN                                                   null

End of function gethighcard

Function getpairs:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/UrYau
function name:  getPairs
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   93     0  E >   FETCH_OBJ_R                                      ~0      'pairs'
          1      > RETURN                                                   ~0
   94     2*     > RETURN                                                   null

End of function getpairs

Function gettoaks:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/UrYau
function name:  getToaks
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   97     0  E >   FETCH_OBJ_R                                      ~0      'toaks'
          1      > RETURN                                                   ~0
   98     2*     > RETURN                                                   null

End of function gettoaks

Function getfoaks:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/UrYau
function name:  getFoaks
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  101     0  E >   FETCH_OBJ_R                                      ~0      'foaks'
          1      > RETURN                                                   ~0
  102     2*     > RETURN                                                   null

End of function getfoaks

Function hasace:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/UrYau
function name:  hasAce
number of ops:  4
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  105     0  E >   FETCH_OBJ_IS                                     ~0      'hands'
          1        ISSET_ISEMPTY_DIM_OBJ                         0  ~1      ~0, 14
          2      > RETURN                                                   ~1
  106     3*     > RETURN                                                   null

End of function hasace

Function isfullhouse:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 46) Position 1 = 4, Position 2 = 8
Branch analysis from position: 4
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 8
filename:       /in/UrYau
function name:  isFullHouse
number of ops:  10
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  109     0  E >   FETCH_OBJ_R                                      ~0      'pairs'
          1        COUNT                                            ~1      ~0
          2        IS_EQUAL                                         ~2      ~1, 1
          3      > JMPZ_EX                                          ~2      ~2, ->8
          4    >   FETCH_OBJ_R                                      ~3      'toaks'
          5        COUNT                                            ~4      ~3
          6        IS_EQUAL                                         ~5      ~4, 1
          7        BOOL                                             ~2      ~5
          8    > > RETURN                                                   ~2
  110     9*     > RETURN                                                   null

End of function isfullhouse

Function isstraight:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 3, Position 2 = 4
Branch analysis from position: 3
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 7, Position 2 = 7
Branch analysis from position: 7
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 7
filename:       /in/UrYau
function name:  isStraight
number of ops:  8
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  112     0  E >   FETCH_OBJ_R                                      ~0      'possibleStraight'
          1        BOOL_NOT                                         ~1      ~0
          2      > JMPZ                                                     ~1, ->4
  113     3    > > RETURN                                                   <false>
  117     4    >   INIT_METHOD_CALL                                         'hasAce'
          5        DO_FCALL                                      0  $2      
          6      > JMPZ                                                     $2, ->7
  120     7    > > RETURN                                                   null

End of function isstraight

End of class Hand.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
176.96 ms | 1416 KiB | 27 Q