3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Card { private $suit; private $val; public function __construct($val, $suit) { $values = array( '2' => 2, '3' => 3, '4' => 4, '5' => 5, '6' => 6, '7' => 7, '8' => 8, '9' => 9, '10' => 10, '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]; } public function getSuit() { return $this->suit; } public function getVal() { return $this->val; } } class Hand { private $cards; private $matches; 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->isFlush(); $this->isStraight(); $this->isFourKind(); $this->isThreeKind(); $this->isTwoPair(); $this->isOnePair(); $this->isHighCard(); } public function getCards(){ return $this->cards; } public function getValues(){ foreach($this->cards as $card){ $output[] = $card->getVal(); } return $output; } public function getSuits(){ foreach($this->cards as $card){ $output[] = $card->getSuit(); } return $output; } public function isHighCard(){ $output = max($this->getValues()); return $output; } public function isPair(){ $this->matches = array(); foreach(array_count_values($this->getValues()) as $value => $count){ if ($count == 2 && count($this->matches) < 2) { $this->matches[] = $value; } } } public function isOnePair(){ $this->isPair(); if (count($this->matches) == 1) { echo "One pair"; } } public function isTwoPair(){ $this->isPair(); if (count($this->matches) == 2) { echo "Two pair"; } } public function isThreeKind(){ $count = max(array_count_values($this->getValues())); if($count == 3){ echo "Three of a kind"; } } public function isFourKind(){ $count = max(array_count_values($this->getValues())); if($count == 4){ echo "Four of a kind"; } } public function isStraight(){ $min = min($this->getValues()); } public function isFlush(){ foreach (array_count_values($this->getSuits()) as $value => $count){ if ($count == 5) { echo "Flush"; } } } public function isStraightFlush(){ } } $h = new Hand('Kh Kc 3s 10h 2d'); // var_dump($x);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/WbDZZ
function name:  (null)
number of ops:  5
compiled vars:  !0 = $h
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  162     0  E >   NEW                                              $1      'Hand'
          1        SEND_VAL_EX                                              'Kh+Kc+3s+10h+2d'
          2        DO_FCALL                                      0          
          3        ASSIGN                                                   !0, $1
  164     4      > RETURN                                                   1

Class Card:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/WbDZZ
function name:  __construct
number of ops:  11
compiled vars:  !0 = $val, !1 = $suit, !2 = $values, !3 = $suits
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    8     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   10     2        ASSIGN                                                   !2, <array>
   26     3        ASSIGN                                                   !3, <array>
   33     4        FETCH_DIM_R                                      ~7      !3, !1
          5        ASSIGN_OBJ                                               'suit'
          6        OP_DATA                                                  ~7
   34     7        FETCH_DIM_R                                      ~9      !2, !0
          8        ASSIGN_OBJ                                               'val'
          9        OP_DATA                                                  ~9
   35    10      > 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/WbDZZ
function name:  getSuit
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   38     0  E >   FETCH_OBJ_R                                      ~0      'suit'
          1      > RETURN                                                   ~0
   39     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/WbDZZ
function name:  getVal
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   42     0  E >   FETCH_OBJ_R                                      ~0      'val'
          1      > RETURN                                                   ~0
   43     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
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 30
filename:       /in/WbDZZ
function name:  __construct
number of ops:  46
compiled vars:  !0 = $hand, !1 = $cards, !2 = $card, !3 = $val, !4 = $suit
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   53     0  E >   RECV                                             !0      
   54     1        INIT_FCALL                                               'explode'
          2        SEND_VAL                                                 '+'
          3        SEND_VAR                                                 !0
          4        DO_ICALL                                         $5      
          5        ASSIGN                                                   !1, $5
   56     6      > FE_RESET_R                                       $7      !1, ->30
          7    > > FE_FETCH_R                                               $7, !2, ->30
   57     8    >   INIT_FCALL                                               'substr'
          9        SEND_VAR                                                 !2
         10        SEND_VAL                                                 0
         11        SEND_VAL                                                 -1
         12        DO_ICALL                                         $8      
         13        ASSIGN                                                   !3, $8
   58    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
   59    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
   56    29      > JMP                                                      ->7
         30    >   FE_FREE                                                  $7
   62    31        INIT_METHOD_CALL                                         'isFlush'
         32        DO_FCALL                                      0          
   63    33        INIT_METHOD_CALL                                         'isStraight'
         34        DO_FCALL                                      0          
   64    35        INIT_METHOD_CALL                                         'isFourKind'
         36        DO_FCALL                                      0          
   65    37        INIT_METHOD_CALL                                         'isThreeKind'
         38        DO_FCALL                                      0          
   66    39        INIT_METHOD_CALL                                         'isTwoPair'
         40        DO_FCALL                                      0          
   67    41        INIT_METHOD_CALL                                         'isOnePair'
         42        DO_FCALL                                      0          
   68    43        INIT_METHOD_CALL                                         'isHighCard'
         44        DO_FCALL                                      0          
   72    45      > RETURN                                                   null

End of function __construct

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

End of function getcards

Function getvalues:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 2, Position 2 = 8
Branch analysis from position: 2
2 jumps found. (Code = 78) Position 1 = 3, Position 2 = 8
Branch analysis from position: 3
1 jumps found. (Code = 42) Position 1 = 2
Branch analysis from position: 2
Branch analysis from position: 8
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 8
filename:       /in/WbDZZ
function name:  getValues
number of ops:  11
compiled vars:  !0 = $card, !1 = $output
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   80     0  E >   FETCH_OBJ_R                                      ~2      'cards'
          1      > FE_RESET_R                                       $3      ~2, ->8
          2    > > FE_FETCH_R                                               $3, !0, ->8
   81     3    >   INIT_METHOD_CALL                                         !0, 'getVal'
          4        DO_FCALL                                      0  $5      
          5        ASSIGN_DIM                                               !1
          6        OP_DATA                                                  $5
   80     7      > JMP                                                      ->2
          8    >   FE_FREE                                                  $3
   83     9      > RETURN                                                   !1
   84    10*     > RETURN                                                   null

End of function getvalues

Function getsuits:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 2, Position 2 = 8
Branch analysis from position: 2
2 jumps found. (Code = 78) Position 1 = 3, Position 2 = 8
Branch analysis from position: 3
1 jumps found. (Code = 42) Position 1 = 2
Branch analysis from position: 2
Branch analysis from position: 8
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 8
filename:       /in/WbDZZ
function name:  getSuits
number of ops:  11
compiled vars:  !0 = $card, !1 = $output
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   88     0  E >   FETCH_OBJ_R                                      ~2      'cards'
          1      > FE_RESET_R                                       $3      ~2, ->8
          2    > > FE_FETCH_R                                               $3, !0, ->8
   89     3    >   INIT_METHOD_CALL                                         !0, 'getSuit'
          4        DO_FCALL                                      0  $5      
          5        ASSIGN_DIM                                               !1
          6        OP_DATA                                                  $5
   88     7      > JMP                                                      ->2
          8    >   FE_FREE                                                  $3
   91     9      > RETURN                                                   !1
   92    10*     > RETURN                                                   null

End of function getsuits

Function ishighcard:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/WbDZZ
function name:  isHighCard
number of ops:  8
compiled vars:  !0 = $output
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   95     0  E >   INIT_FCALL                                               'max'
          1        INIT_METHOD_CALL                                         'getValues'
          2        DO_FCALL                                      0  $1      
          3        SEND_VAR                                                 $1
          4        DO_ICALL                                         $2      
          5        ASSIGN                                                   !0, $2
   96     6      > RETURN                                                   !0
   97     7*     > RETURN                                                   null

End of function ishighcard

Function ispair:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 8, Position 2 = 21
Branch analysis from position: 8
2 jumps found. (Code = 78) Position 1 = 9, Position 2 = 21
Branch analysis from position: 9
2 jumps found. (Code = 46) Position 1 = 12, Position 2 = 16
Branch analysis from position: 12
2 jumps found. (Code = 43) Position 1 = 17, Position 2 = 20
Branch analysis from position: 17
1 jumps found. (Code = 42) Position 1 = 8
Branch analysis from position: 8
Branch analysis from position: 20
Branch analysis from position: 16
Branch analysis from position: 21
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 21
filename:       /in/WbDZZ
function name:  isPair
number of ops:  23
compiled vars:  !0 = $count, !1 = $value
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  100     0  E >   ASSIGN_OBJ                                               'matches'
          1        OP_DATA                                                  <array>
  101     2        INIT_FCALL                                               'array_count_values'
          3        INIT_METHOD_CALL                                         'getValues'
          4        DO_FCALL                                      0  $3      
          5        SEND_VAR                                                 $3
          6        DO_ICALL                                         $4      
          7      > FE_RESET_R                                       $5      $4, ->21
          8    > > FE_FETCH_R                                       ~6      $5, !0, ->21
          9    >   ASSIGN                                                   !1, ~6
  102    10        IS_EQUAL                                         ~8      !0, 2
         11      > JMPZ_EX                                          ~8      ~8, ->16
         12    >   FETCH_OBJ_R                                      ~9      'matches'
         13        COUNT                                            ~10     ~9
         14        IS_SMALLER                                       ~11     ~10, 2
         15        BOOL                                             ~8      ~11
         16    > > JMPZ                                                     ~8, ->20
  103    17    >   FETCH_OBJ_W                                      $12     'matches'
         18        ASSIGN_DIM                                               $12
         19        OP_DATA                                                  !1
  101    20    > > JMP                                                      ->8
         21    >   FE_FREE                                                  $5
  107    22      > RETURN                                                   null

End of function ispair

Function isonepair:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 6, Position 2 = 7
Branch analysis from position: 6
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 7
filename:       /in/WbDZZ
function name:  isOnePair
number of ops:  8
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  110     0  E >   INIT_METHOD_CALL                                         'isPair'
          1        DO_FCALL                                      0          
  112     2        FETCH_OBJ_R                                      ~1      'matches'
          3        COUNT                                            ~2      ~1
          4        IS_EQUAL                                                 ~2, 1
          5      > JMPZ                                                     ~3, ->7
  113     6    >   ECHO                                                     'One+pair'
  115     7    > > RETURN                                                   null

End of function isonepair

Function istwopair:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 6, Position 2 = 7
Branch analysis from position: 6
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 7
filename:       /in/WbDZZ
function name:  isTwoPair
number of ops:  8
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  118     0  E >   INIT_METHOD_CALL                                         'isPair'
          1        DO_FCALL                                      0          
  120     2        FETCH_OBJ_R                                      ~1      'matches'
          3        COUNT                                            ~2      ~1
          4        IS_EQUAL                                                 ~2, 2
          5      > JMPZ                                                     ~3, ->7
  121     6    >   ECHO                                                     'Two+pair'
  124     7    > > RETURN                                                   null

End of function istwopair

Function isthreekind:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 11, Position 2 = 12
Branch analysis from position: 11
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 12
filename:       /in/WbDZZ
function name:  isThreeKind
number of ops:  13
compiled vars:  !0 = $count
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  128     0  E >   INIT_FCALL                                               'max'
          1        INIT_FCALL                                               'array_count_values'
          2        INIT_METHOD_CALL                                         'getValues'
          3        DO_FCALL                                      0  $1      
          4        SEND_VAR                                                 $1
          5        DO_ICALL                                         $2      
          6        SEND_VAR                                                 $2
          7        DO_ICALL                                         $3      
          8        ASSIGN                                                   !0, $3
  129     9        IS_EQUAL                                                 !0, 3
         10      > JMPZ                                                     ~5, ->12
  130    11    >   ECHO                                                     'Three+of+a+kind'
  132    12    > > RETURN                                                   null

End of function isthreekind

Function isfourkind:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 11, Position 2 = 12
Branch analysis from position: 11
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 12
filename:       /in/WbDZZ
function name:  isFourKind
number of ops:  13
compiled vars:  !0 = $count
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  134     0  E >   INIT_FCALL                                               'max'
          1        INIT_FCALL                                               'array_count_values'
          2        INIT_METHOD_CALL                                         'getValues'
          3        DO_FCALL                                      0  $1      
          4        SEND_VAR                                                 $1
          5        DO_ICALL                                         $2      
          6        SEND_VAR                                                 $2
          7        DO_ICALL                                         $3      
          8        ASSIGN                                                   !0, $3
  135     9        IS_EQUAL                                                 !0, 4
         10      > JMPZ                                                     ~5, ->12
  136    11    >   ECHO                                                     'Four+of+a+kind'
  138    12    > > RETURN                                                   null

End of function isfourkind

Function isstraight:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/WbDZZ
function name:  isStraight
number of ops:  7
compiled vars:  !0 = $min
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  141     0  E >   INIT_FCALL                                               'min'
          1        INIT_METHOD_CALL                                         'getValues'
          2        DO_FCALL                                      0  $1      
          3        SEND_VAR                                                 $1
          4        DO_ICALL                                         $2      
          5        ASSIGN                                                   !0, $2
  144     6      > RETURN                                                   null

End of function isstraight

Function isflush:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 6, Position 2 = 12
Branch analysis from position: 6
2 jumps found. (Code = 78) Position 1 = 7, Position 2 = 12
Branch analysis from position: 7
2 jumps found. (Code = 43) Position 1 = 10, Position 2 = 11
Branch analysis from position: 10
1 jumps found. (Code = 42) Position 1 = 6
Branch analysis from position: 6
Branch analysis from position: 11
Branch analysis from position: 12
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 12
filename:       /in/WbDZZ
function name:  isFlush
number of ops:  14
compiled vars:  !0 = $count, !1 = $value
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  147     0  E >   INIT_FCALL                                               'array_count_values'
          1        INIT_METHOD_CALL                                         'getSuits'
          2        DO_FCALL                                      0  $2      
          3        SEND_VAR                                                 $2
          4        DO_ICALL                                         $3      
          5      > FE_RESET_R                                       $4      $3, ->12
          6    > > FE_FETCH_R                                       ~5      $4, !0, ->12
          7    >   ASSIGN                                                   !1, ~5
  148     8        IS_EQUAL                                                 !0, 5
          9      > JMPZ                                                     ~7, ->11
  149    10    >   ECHO                                                     'Flush'
  147    11    > > JMP                                                      ->6
         12    >   FE_FREE                                                  $4
  153    13      > RETURN                                                   null

End of function isflush

Function isstraightflush:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/WbDZZ
function name:  isStraightFlush
number of ops:  1
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  157     0  E > > RETURN                                                   null

End of function isstraightflush

End of class Hand.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
175.99 ms | 1420 KiB | 25 Q