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; } } function buildCountFilter($num) { return function ($count, $face) use ($num) { if ($count == $num) return $face; }; } 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; } $this->pairs[] = array_filter($this->hands, buildCountFilter(2), ARRAY_FILTER_USE_BOTH); $this->toaks[] = array_filter($this->hands, buildCountFilter(3), ARRAY_FILTER_USE_BOTH); $this->foaks[] = array_filter($this->hands, buildCountFilter(4), ARRAY_FILTER_USE_BOTH); } 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/32Hqr
function name:  (null)
number of ops:  14
compiled vars:  !0 = $h
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  115     0  E >   NEW                                              $1      'Hand'
          1        SEND_VAL_EX                                              '6h+6d+6s+2c+2h'
          2        DO_FCALL                                      0          
          3        ASSIGN                                                   !0, $1
  116     4        INIT_METHOD_CALL                                         !0, 'testMethods'
          5        DO_FCALL                                      0          
  118     6        ECHO                                                     '%0A%0ASecond+hand%3A%0A'
  119     7        NEW                                              $5      'Hand'
          8        SEND_VAL_EX                                              '6h+7d+8s+9c+10h'
          9        DO_FCALL                                      0          
         10        ASSIGN                                                   !0, $5
  120    11        INIT_METHOD_CALL                                         !0, 'testMethods'
         12        DO_FCALL                                      0          
         13      > RETURN                                                   1

Function buildcountfilter:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/32Hqr
function name:  buildCountFilter
number of ops:  5
compiled vars:  !0 = $num
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   32     0  E >   RECV                                             !0      
   33     1        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2F32Hqr%3A33%240'
          2        BIND_LEXICAL                                             ~1, !0
          3      > RETURN                                                   ~1
   34     4*     > RETURN                                                   null

End of function buildcountfilter

Function %00%7Bclosure%7D%2Fin%2F32Hqr%3A33%240:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 6
Branch analysis from position: 5
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 6
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/32Hqr
function name:  {closure}
number of ops:  7
compiled vars:  !0 = $count, !1 = $face, !2 = $num
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   33     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        BIND_STATIC                                              !2
          3        IS_EQUAL                                                 !0, !2
          4      > JMPZ                                                     ~3, ->6
          5    > > RETURN                                                   !1
          6    > > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2F32Hqr%3A33%240

Function %00%7Bclosure%7D%2Fin%2F32Hqr%3A49%241:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/32Hqr
function name:  {closure}
number of ops:  5
compiled vars:  !0 = $card
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   49     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%2F32Hqr%3A49%241

Class Card:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/32Hqr
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/32Hqr
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/32Hqr
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
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 61
Branch analysis from position: 30
filename:       /in/32Hqr
function name:  __construct
number of ops:  98
compiled vars:  !0 = $hand, !1 = $cards, !2 = $card, !3 = $val, !4 = $suit
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   40     0  E >   RECV                                             !0      
   41     1        INIT_FCALL                                               'explode'
          2        SEND_VAL                                                 '+'
          3        SEND_VAR                                                 !0
          4        DO_ICALL                                         $5      
          5        ASSIGN                                                   !1, $5
   43     6      > FE_RESET_R                                       $7      !1, ->30
          7    > > FE_FETCH_R                                               $7, !2, ->30
   44     8    >   INIT_FCALL                                               'substr'
          9        SEND_VAR                                                 !2
         10        SEND_VAL                                                 0
         11        SEND_VAL                                                 -1
         12        DO_ICALL                                         $8      
         13        ASSIGN                                                   !3, $8
   45    14        INIT_FCALL                                               'strtoupper'
         15        INIT_FCALL                                               'substr'
         16        SEND_VAR                                                 !2
         17        SEND_VAL                                                 -1
         18        DO_ICALL                                         $10     
         19        SEND_VAR                                                 $10
         20        DO_ICALL                                         $11     
         21        ASSIGN                                                   !4, $11
   46    22        NEW                                              $15     'Card'
         23        SEND_VAR_EX                                              !3
         24        SEND_VAR_EX                                              !4
         25        DO_FCALL                                      0          
         26        FETCH_OBJ_W                                      $13     'cards'
         27        ASSIGN_DIM                                               $13
         28        OP_DATA                                                  $15
   43    29      > JMP                                                      ->7
         30    >   FE_FREE                                                  $7
   49    31        INIT_FCALL                                               'array_map'
         32        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2F32Hqr%3A49%241'
         33        SEND_VAL                                                 ~18
         34        FETCH_OBJ_R                                      ~19     'cards'
         35        SEND_VAL                                                 ~19
         36        DO_ICALL                                         $20     
         37        ASSIGN_OBJ                                               'values'
         38        OP_DATA                                                  $20
   50    39        ASSIGN_OBJ                                       ~23     'foaks'
         40        OP_DATA                                                  <array>
         41        ASSIGN_OBJ                                       ~22     'toaks'
         42        OP_DATA                                                  ~23
         43        ASSIGN_OBJ                                               'pairs'
         44        OP_DATA                                                  ~22
   51    45        ASSIGN_OBJ                                               'possibleStraight'
         46        OP_DATA                                                  <true>
   53    47        INIT_FCALL                                               'array_count_values'
         48        FETCH_OBJ_R                                      ~26     'values'
         49        SEND_VAL                                                 ~26
         50        DO_ICALL                                         $27     
         51        ASSIGN_OBJ                                               'hands'
         52        OP_DATA                                                  $27
   55    53        INIT_FCALL                                               'max'
         54        FETCH_OBJ_R                                      ~28     'hands'
         55        SEND_VAL                                                 ~28
         56        DO_ICALL                                         $29     
         57        IS_SMALLER                                               1, $29
         58      > JMPZ                                                     ~30, ->61
   56    59    >   ASSIGN_OBJ                                               'possibleStraight'
         60        OP_DATA                                                  <false>
   58    61    >   INIT_FCALL                                               'array_filter'
         62        FETCH_OBJ_R                                      ~34     'hands'
         63        SEND_VAL                                                 ~34
         64        INIT_FCALL                                               'buildcountfilter'
         65        SEND_VAL                                                 2
         66        DO_FCALL                                      0  $35     
         67        SEND_VAR                                                 $35
         68        SEND_VAL                                                 1
         69        DO_ICALL                                         $36     
         70        FETCH_OBJ_W                                      $32     'pairs'
         71        ASSIGN_DIM                                               $32
         72        OP_DATA                                                  $36
   59    73        INIT_FCALL                                               'array_filter'
         74        FETCH_OBJ_R                                      ~39     'hands'
         75        SEND_VAL                                                 ~39
         76        INIT_FCALL                                               'buildcountfilter'
         77        SEND_VAL                                                 3
         78        DO_FCALL                                      0  $40     
         79        SEND_VAR                                                 $40
         80        SEND_VAL                                                 1
         81        DO_ICALL                                         $41     
         82        FETCH_OBJ_W                                      $37     'toaks'
         83        ASSIGN_DIM                                               $37
         84        OP_DATA                                                  $41
   60    85        INIT_FCALL                                               'array_filter'
         86        FETCH_OBJ_R                                      ~44     'hands'
         87        SEND_VAL                                                 ~44
         88        INIT_FCALL                                               'buildcountfilter'
         89        SEND_VAL                                                 4
         90        DO_FCALL                                      0  $45     
         91        SEND_VAR                                                 $45
         92        SEND_VAL                                                 1
         93        DO_ICALL                                         $46     
         94        FETCH_OBJ_W                                      $42     'foaks'
         95        ASSIGN_DIM                                               $42
         96        OP_DATA                                                  $46
   61    97      > 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/32Hqr
function name:  testMethods
number of ops:  31
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   64     0  E >   INIT_FCALL                                               'var_dump'
          1        INIT_METHOD_CALL                                         'getHighCard'
          2        DO_FCALL                                      0  $0      
          3        SEND_VAR                                                 $0
          4        DO_ICALL                                                 
   65     5        INIT_FCALL                                               'var_dump'
          6        INIT_METHOD_CALL                                         'getPairs'
          7        DO_FCALL                                      0  $2      
          8        SEND_VAR                                                 $2
          9        DO_ICALL                                                 
   66    10        INIT_FCALL                                               'var_dump'
         11        INIT_METHOD_CALL                                         'getToaks'
         12        DO_FCALL                                      0  $4      
         13        SEND_VAR                                                 $4
         14        DO_ICALL                                                 
   67    15        INIT_FCALL                                               'var_dump'
         16        INIT_METHOD_CALL                                         'getFoaks'
         17        DO_FCALL                                      0  $6      
         18        SEND_VAR                                                 $6
         19        DO_ICALL                                                 
   68    20        INIT_FCALL                                               'var_dump'
         21        INIT_METHOD_CALL                                         'isFullHouse'
         22        DO_FCALL                                      0  $8      
         23        SEND_VAR                                                 $8
         24        DO_ICALL                                                 
   69    25        INIT_FCALL                                               'var_dump'
         26        INIT_METHOD_CALL                                         'isStraight'
         27        DO_FCALL                                      0  $10     
         28        SEND_VAR                                                 $10
         29        DO_ICALL                                                 
   70    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/32Hqr
function name:  getCards
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   73     0  E >   FETCH_OBJ_R                                      ~0      'cards'
          1      > RETURN                                                   ~0
   74     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/32Hqr
function name:  getValues
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   77     0  E >   FETCH_OBJ_R                                      ~0      'values'
          1      > RETURN                                                   ~0
   78     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/32Hqr
function name:  getHighCard
number of ops:  7
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   81     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
   82     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/32Hqr
function name:  getPairs
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   85     0  E >   FETCH_OBJ_R                                      ~0      'pairs'
          1      > RETURN                                                   ~0
   86     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/32Hqr
function name:  getToaks
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   89     0  E >   FETCH_OBJ_R                                      ~0      'toaks'
          1      > RETURN                                                   ~0
   90     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/32Hqr
function name:  getFoaks
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   93     0  E >   FETCH_OBJ_R                                      ~0      'foaks'
          1      > RETURN                                                   ~0
   94     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/32Hqr
function name:  hasAce
number of ops:  4
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   97     0  E >   FETCH_OBJ_IS                                     ~0      'hands'
          1        ISSET_ISEMPTY_DIM_OBJ                         0  ~1      ~0, 14
          2      > RETURN                                                   ~1
   98     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/32Hqr
function name:  isFullHouse
number of ops:  10
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  101     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
  102     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/32Hqr
function name:  isStraight
number of ops:  8
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  104     0  E >   FETCH_OBJ_R                                      ~0      'possibleStraight'
          1        BOOL_NOT                                         ~1      ~0
          2      > JMPZ                                                     ~1, ->4
  105     3    > > RETURN                                                   <false>
  109     4    >   INIT_METHOD_CALL                                         'hasAce'
          5        DO_FCALL                                      0  $2      
          6      > JMPZ                                                     $2, ->7
  112     7    > > RETURN                                                   null

End of function isstraight

End of class Hand.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
171.36 ms | 1423 KiB | 32 Q