bssanchez93

BlackJack - CommandLine

Jun 9th, 2013
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 13.44 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4.  * Juego de blackjack en consola
  5.  * programado en php
  6.  *
  7.  * @author The Bolivar Team
  8.  */
  9. class blackjack
  10. {
  11.     private $mazo = array();
  12.     protected $cartas_jug;
  13.     protected $carta_lanzar;
  14.     protected $jugador = array("cartas" => array(), "puntos_c" => array(), "puntos_p" => 0, "puntos_t" => 0);
  15.     protected $tallador = array("cartas" => array(), "carta_1", "puntos_c" => array(), "puntos_p" => 0, "puntos_t" => 0);
  16.  
  17.  
  18.     function __construct() {
  19.         $this->iniciar();
  20.     }
  21.    
  22.     private function iniciar() {
  23.         $this->jugador = array("cartas" => array(), "puntos_c" => array(), "puntos_p" => 0, "puntos_t" => 0);
  24.         $this->tallador = array("cartas" => array(), "carta_1", "puntos_c" => array(), "puntos_p" => 0, "puntos_t" => 0);
  25.         $this->cargarCartas();
  26.        
  27.         $rand = rand(0, (count($this->mazo) - 1));
  28.         $this->jugador['cartas'][] = $this->mazo[$rand];
  29.         $this->jugador['puntos_c'][] = $this->puntos($this->mazo[$rand]);
  30.         unset($this->mazo[$rand]);$this->barajar();
  31.        
  32.         $rand = rand(0, (count($this->mazo) - 1));
  33.         $this->tallador['carta_1'] = $this->mazo[$rand];
  34.         $this->tallador['puntos_c'][] = $this->puntos($this->mazo[$rand]);
  35.         unset($this->mazo[$rand]);$this->barajar();
  36.        
  37.         $this->lanzarCarta('jugador');
  38.         $this->lanzarCarta('tallador');
  39.        
  40.         $this->imprimir();
  41.         $this->jugador();
  42.     }
  43.    
  44.     private function lanzarCarta($player) {
  45.         $rand = rand(0, (count($this->mazo) - 1));
  46.         if($player == 'jugador')
  47.         {
  48.             $this->jugador['cartas'][] = $this->mazo[$rand];
  49.             $this->jugador['puntos_c'][] = (int)($this->puntos($this->mazo[$rand]));
  50.         }
  51.         else if($player == 'tallador')
  52.         {
  53.             $this->tallador['cartas'][] = $this->mazo[$rand];
  54.             $this->tallador['puntos_c'][] = (int)($this->puntos($this->mazo[$rand]));
  55.         }
  56.        
  57.         $this->comprobar_ases($player);
  58.         $this->comprobar($player);
  59.         unset($this->mazo[$rand]);
  60.        
  61.         $this->barajar();
  62.     }
  63.    
  64.     private function comprobar_ases($player) {
  65.         $ases = array();
  66.         $puntos = 0;
  67.         if($player == 'tallador')
  68.         {
  69.             $datos = $this->tallador['puntos_c'];
  70.            
  71.             foreach($datos as $key => $valor)
  72.             {
  73.                 if($valor == 11)
  74.                 {
  75.                     $ases[] = $key;
  76.                 }
  77.                 else
  78.                 {
  79.                     $puntos += $valor;
  80.                 }
  81.             }
  82.            
  83.             foreach ($ases as $pos) {
  84.                 if(($puntos + 11) > 21)
  85.                 {
  86.                     $this->tallador['puntos_c'][$pos] = 1;
  87.                     $puntos += 1;
  88.                 }
  89.                 else
  90.                 {
  91.                     $this->tallador['puntos_c'][$pos] = 11;
  92.                     $puntos += 11;
  93.                 }
  94.             }
  95.             $this->tallador['puntos_t'] = $puntos;
  96.             $this->tallador['puntos_p'] = $puntos - $datos[0];
  97.         }
  98.         else if ($player == 'jugador')
  99.         {
  100.             $datos = $this->jugador['puntos_c'];
  101.            
  102.             foreach($datos as $key => $valor)
  103.             {
  104.                 if($valor == 11)
  105.                 {
  106.                     $ases[] = $key;
  107.                 }
  108.                 else
  109.                 {
  110.                     $puntos += $valor;
  111.                 }
  112.             }
  113.            
  114.             foreach ($ases as $pos) {
  115.                 if(($puntos + 11) > 21)
  116.                 {
  117.                     $this->jugador['puntos_c'][$pos] = 1;
  118.                     $puntos += 1;
  119.                 }
  120.                 else
  121.                 {
  122.                     $this->jugador['puntos_c'][$pos] = 11;
  123.                     $puntos += 11;
  124.                 }
  125.             }
  126.             $this->jugador['puntos_t'] = $puntos;
  127.             $this->jugador['puntos_p'] = $puntos - $datos[0];
  128.             //print_r($datos);die();
  129.         }
  130.     }
  131.    
  132.     private function comprobar($player) {
  133.         if($player == 'tallador')
  134.         {
  135.             if($this->tallador['puntos_t'] > 21)
  136.             {
  137.                 $this->fin('jugador');
  138.             }
  139.         }
  140.         else if ($player == 'jugador')
  141.         {
  142.             if($this->jugador['puntos_t'] > 21)
  143.             {
  144.                 $this->fin('tallador');
  145.             }
  146.         }
  147.         else
  148.         {
  149.            $this->fin();
  150.         }
  151.     }
  152.  
  153.     private function fin($jug = '') {
  154.         print "\n\n+============================FIN DEL JUEGO=====================================+\n";
  155.         print '';
  156.         print "[[ JUGADOR ]]\n";
  157.         print 'Cartas : ' . implode(', ', $this->jugador['cartas']) . "\n";
  158.         print 'Puntos totales : ' . $this->jugador['puntos_t'] . "\n";
  159.  
  160.         print "\n";
  161.         print "[[ TALLADOR ]]\n";
  162.         print 'Cartas : ' . $this->tallador['carta_1'] . ', ' . implode(', ', $this->tallador['cartas']) . "\n";
  163.         print 'Puntos del tallador : ' . $this->tallador['puntos_t'] . "\n";
  164.        
  165.         if($jug != '')
  166.         {
  167.             if($jug == 'tallador')
  168.             {
  169.                  print "\n[TALLADOR] GANA..!! Intenta de nuevo";
  170.             }
  171.             else
  172.             {
  173.                 print "\n[JUGADOR] GANA..!! Felicidades..!!";
  174.             }
  175.         }
  176.         else if($this->tallador['puntos_t'] > $this->jugador['puntos_t'] && $this->tallador['puntos_t'] <= 21)
  177.         {
  178.             print "\n[TALLADOR] GANA..!! Intenta de nuevo";
  179.         }
  180.         else if($this->tallador['puntos_t'] < $this->jugador['puntos_t'] && $this->jugador['puntos_t'] <= 21)
  181.         {
  182.             print "\n[JUGADOR] GANA..!! Felicidades..!!";
  183.         }
  184.         else if($this->tallador['puntos_t'] == $this->jugador['puntos_t'] && $this->tallador['puntos_t'] == 21)
  185.         {
  186.             if(count($this->tallador['cartas']) == 1)
  187.             {
  188.                 print "\n[TALLADOR] GANA..!! Intenta de nuevo";
  189.             }
  190.             else if($this->jugador['cartas'])
  191.             {
  192.                 print "\n[JUGADOR] GANA..!! Felicidades..!!";
  193.             }
  194.         }
  195.         else
  196.         {
  197.             print "\n[JUGADOR] GANA..!! Felicidades..!!";
  198.         }
  199.        
  200.         print "\n\nby bssanchez (kid_goth), cbriceno -> The Bolivar Team";
  201.         die();
  202.     }
  203.  
  204.     private function puntos($carta)
  205.     {
  206.         $puntos = 0;
  207.         $carta = explode("_", $carta);
  208.         $carta = $carta[0];
  209.        
  210.         switch ($carta) {
  211.             case "K":
  212.             case "Q":
  213.             case "J":
  214.                 $puntos = 10;
  215.                 break;
  216.             case "A":
  217.                 $puntos = 11;
  218.                 break;
  219.             default:
  220.                 $puntos = (int)$carta;
  221.                 break;
  222.         }
  223.        
  224.         return $puntos;
  225.     }
  226.  
  227.  
  228.     private function jugador() {
  229.         do
  230.         {
  231.             if (PHP_OS == 'WINNT') {
  232.                 print 'Otra [S/N]? [S]: ';
  233.                 $otra = stream_get_line(STDIN, 1);
  234.             }
  235.             else
  236.             {
  237.                 $otra = readline('Otra [S/N]? [S]: ');
  238.             }
  239.             $otra = strtolower($otra);
  240.            
  241.             if($otra == 's')
  242.             {
  243.                 $this->lanzarCarta('jugador');
  244.                 print "\n\n+==============================================================================+\n";
  245.                 print '';
  246.                 print "[[ JUGADOR ]]\n";
  247.                 print 'Cartas : ' . implode(', ', $this->jugador['cartas']) . "\n";
  248.                 print 'Puntos vistos por el tallador : ' . $this->jugador['puntos_p'] . "\n";
  249.                 print 'Puntos totales : ' . $this->jugador['puntos_t'] . "\n";
  250.  
  251.                 print "\n";
  252.                 print "[[ TALLADOR ]]\n";
  253.                 print 'Cartas : [?_/_?], ' . implode(', ', $this->tallador['cartas']) . "\n";
  254.                 print 'Puntos del tallador : ' . $this->tallador['puntos_p'] . "\n";
  255.             }
  256.         }while($otra != "n");
  257.        
  258.         $this->tallador();
  259.     }
  260.    
  261.     private function tallador()
  262.     {
  263.         while(true)
  264.         {
  265.             if($this->tallador['puntos_t'] == 21)
  266.             {
  267.                 break;
  268.             }
  269.             else if($this->tallador['puntos_t'] < $this->jugador['puntos_p'] || $this->tallador['puntos_t'] <= 13)
  270.             {
  271.                 $this->lanzarCarta('tallador');
  272.             }
  273.             else if($this->tallador['puntos_t'] >= 16 && $this->tallador['puntos_t'] <= 18)
  274.             {
  275.                 $rand = rand(0,1);
  276.                 if($rand)
  277.                 {
  278.                     $this->lanzarCarta('tallador');
  279.                     print "\n\n+==============================================================================+\n";
  280.                     print '';
  281.                     print "[[ JUGADOR ]]\n";
  282.                     print 'Cartas : ' . implode(', ', $this->jugador['cartas']) . "\n";
  283.                     print 'Puntos vistos por el tallador : ' . $this->jugador['puntos_p'] . "\n";
  284.                     print 'Puntos totales : ' . $this->jugador['puntos_t'] . "\n";
  285.  
  286.                     print "\n";
  287.                     print "[[ TALLADOR ]]\n";
  288.                     print 'Cartas : [?_/_?], ' . implode(', ', $this->tallador['cartas']) . "\n";
  289.                     print 'Puntos del tallador : ' . $this->tallador['puntos_p'] . "\n";
  290.                 }
  291.                 break;
  292.             }
  293.             else
  294.             {
  295.                 $rand = rand(0,1);
  296.                 if($rand)
  297.                 {
  298.                     $this->lanzarCarta('tallador');
  299.                     print "\n\n+==============================================================================+\n";
  300.                     print '';
  301.                     print "[[ JUGADOR ]]\n";
  302.                     print 'Cartas : ' . implode(', ', $this->jugador['cartas']) . "\n";
  303.                     print 'Puntos vistos por el tallador : ' . $this->jugador['puntos_p'] . "\n";
  304.                     print 'Puntos totales : ' . $this->jugador['puntos_t'] . "\n";
  305.  
  306.                     print "\n";
  307.                     print "[[ TALLADOR ]]\n";
  308.                     print 'Cartas : [?_/_?], ' . implode(', ', $this->tallador['cartas']) . "\n";
  309.                     print 'Puntos del tallador : ' . $this->tallador['puntos_p'] . "\n";
  310.                 }
  311.                 else
  312.                 {
  313.                     break;
  314.                 }
  315.             }
  316.            
  317.             $this->comprobar('fin');
  318.         }        
  319.     }
  320.  
  321.  
  322.     private function barajar()
  323.     {
  324.         $nuevas_cartas = array();
  325.        
  326.         foreach ($this->mazo as $value) {
  327.             $nuevas_cartas[] = $value;
  328.         }
  329.        
  330.         $this->mazo = $nuevas_cartas;
  331.     }
  332.  
  333.  
  334.     private function cargarCartas() {
  335.        
  336.         $mazo = array();
  337.        
  338.         for ($i = 1; $i <= 13; $i++)
  339.         {
  340.             switch ($i)
  341.             {
  342.                 case 1:
  343.                     $carta = "A";
  344.                     break;
  345.                 case 11:
  346.                     $carta = "J";
  347.                     break;
  348.                 case 12:
  349.                     $carta = "Q";
  350.                     break;
  351.                 case 13:
  352.                     $carta = "K";
  353.                     break;
  354.                 default :
  355.                     $carta = $i;
  356.                     break;
  357.             }
  358.             $cartas = array("C" => $carta, "P" => $carta, "D" => $carta, "T" => $carta);
  359.            
  360.             array_push($mazo, $cartas);
  361.         }
  362.        
  363.         foreach ($mazo as $num => $array) {
  364.             foreach ($array as $key => $value) {
  365.                 $this->mazo[] = $value . "_/_" . $key;
  366.             }
  367.         }
  368.     }
  369.    
  370.     private function imprimir()
  371.     {
  372.         print "+==============================================================================+";
  373.         print "|                                                        |                     |";
  374.         print "|       BBBBBB    LL          A        CCCCC  KK    KK   |  BOLIVAR TEAM       |";
  375.         print "|       B     B   LL         A A      C       KK   KK    |  2013               |";
  376.         print "|       B    B    LL        A   A    C        KK  KK     |                     |";
  377.         print "|       B  BBB    LL       AAAAAAA   C        KKKKK      |  ENJOY IT           |";
  378.         print "|       B     B   LL       A     A   C        KK  KK     |                     |";
  379.         print "|       B    B    LL       A     A    C       KK   KK    |                     |";
  380.         print "|       BBBBB     LLLLLL  AA     AA    CCCCC  KK    KKK  |                     |";
  381.         print "|                                                        |  JACK V 1.0         |";
  382.         print "+===============================Mesa de Juego==================================+\n";
  383.         print '';
  384.         print "[[ JUGADOR ]]\n";
  385.         print 'Cartas : ' . implode(', ', $this->jugador['cartas']) . "\n";
  386.         print 'Puntos vistos por el tallador : ' . $this->jugador['puntos_p'] . "\n";
  387.         print 'Puntos totales : ' . $this->jugador['puntos_t'] . "\n";
  388.        
  389.         print "\n";
  390.         print "[[ TALLADOR ]]\n";
  391.         print 'Cartas : [?_/_?], ' . implode(', ', $this->tallador['cartas']) . "\n";
  392.         print 'Puntos del tallador : ' . $this->tallador['puntos_p'] . "\n";
  393.     }
  394. }
  395.  
  396. /**
  397.  * Controlador
  398.  */
  399.  
  400. new blackjack();
  401. ?>
Advertisement
Add Comment
Please, Sign In to add comment