Black Jack - Shuffler.java
Cerca
 











Shuffler.java

Caricato da: Paoloricciuti
Scarica il programma completo

  1. package blackjack;
  2.  
  3. /**
  4.  * Rappresenta un banco di black jack.
  5.  * @author paolo
  6.  */
  7. public class Shuffler {
  8.  
  9.     /**
  10.      * Il mazzo di carte utilizzato dal Shuffler.
  11.      */
  12.     private Deck deck;
  13.     /**
  14.      * Le carte sul tavolo.
  15.      */
  16.     private Card[][] table;
  17.     /**
  18.      * Il numero di carte prelevate dal Shuffler e dal giocatore.
  19.      */
  20.     private int[] numCard;
  21.     /**
  22.      * Variabile booleana per controllare se il banco deve pagare l'insurance.
  23.      */
  24.     private boolean insurancePay;
  25.     /**
  26.      * Variabile booleana per controllare se è stata effettiuata l'insurance.
  27.      */
  28.     private boolean insurance;
  29.     /**
  30.      * La puntata effettuata.
  31.      */
  32.     private int bet;
  33.  
  34.     /**
  35.      * Il costruttore di default, inizializza il mazzo e lo mischia.
  36.      */
  37.     public Shuffler() {
  38.         this.deck = new Deck();
  39.         this.deck.shuffle();
  40.         this.table = new Card[2][5];
  41.         this.numCard = new int[2];
  42.         this.insurancePay = false;
  43.         this.insurance = false;
  44.     }
  45.  
  46.     /**
  47.      * Serve le prime 4 carte.
  48.      */
  49.     public void start() {
  50.         this.table[0][0] = this.deck.getCard(0);
  51.         this.table[1][0] = this.deck.getCard(1);
  52.         this.table[1][0].setCoperta(true);
  53.         this.table[0][1] = this.deck.getCard(2);
  54.         this.table[1][1] = this.deck.getCard(3);
  55.         this.numCard[0] = 2;
  56.         this.numCard[1] = 2;
  57.     }
  58.  
  59.     /**
  60.      * Richiede una card.
  61.      * @param pWho chi ha richiesto la card, 0 per il giocatore, 1 per il banco.
  62.      */
  63.     public void card(int pWho) {
  64.         this.table[pWho][this.numCard[pWho]] = this.deck.getCard(this.numCard[0] + this.numCard[1]);
  65.         this.numCard[pWho]++;
  66.     }
  67.  
  68.     /**
  69.      * Ritorna il punteggio.
  70.      * @param pWho 0 per sapere il punteggio del giocatore, 1 per quello del banco.
  71.      * @return Un <code>int</code> contenente il punteggio di <code>pWho</code>
  72.      */
  73.     public int getPoint(int pWho) {
  74.         int player = 0;
  75.         boolean isAnAce = false;
  76.         int numAce = 0;
  77.         for (int i = 0; i < this.numCard[pWho]; i++) {
  78.             if (!this.table[pWho][i].isCover()) {
  79.                 player = player + this.table[pWho][i].getValue();
  80.                 if (this.table[pWho][i].getNumber().equals("1")) {
  81.                     isAnAce = true;
  82.                     numAce++;
  83.                 }
  84.             }
  85.         }
  86.         for (int i = 0; i < numAce; i++) {
  87.             if (player > 21 && isAnAce) {
  88.                 player = player - 10;
  89.             }
  90.         }
  91.         return player;
  92.     }
  93.  
  94.     /**
  95.      * Il valore della puntata.
  96.      * @return Un <code>int</code> contenente il valore della puntata.
  97.      */
  98.     public int getBet() {
  99.         return bet;
  100.     }
  101.  
  102.     /**
  103.      * Setta la puntata.
  104.      * @param pBet La puntata.
  105.      */
  106.     public void setBet(int pBet) {
  107.         if(pBet>0){
  108.             this.bet = pBet;
  109.         }
  110.     }
  111.  
  112.     /**
  113.      * Controlla se il giocatore è vincente.
  114.      * @return <code>true</code> se è vincente, <code>false</code> se non lo è.
  115.      */
  116.     public boolean isWinner() {
  117.         int giocatore = this.getPoint(0);
  118.         int banco = this.getPoint(1);
  119.         if (giocatore > banco && giocatore <= 21 || (banco > 21)) {
  120.             return true;
  121.         }
  122.         return false;
  123.     }
  124.  
  125.     /**
  126.      * Paga il giocatore.
  127.      * @return Quanto bisogna pagare al giocatore.
  128.      */
  129.     public int pay() {
  130.         int retval = 0;
  131.         if (this.isWinner()) {
  132.             retval = this.bet * 2;
  133.         }
  134.         if (this.isBlackJack(0) && !this.isBlackJack(1)) {
  135.             retval = (this.bet * 2) + (this.bet / 2);
  136.         }
  137.  
  138.         if (this.insurancePay) {
  139.             retval = retval + this.bet;
  140.         }
  141.         return retval;
  142.     }
  143.  
  144.     /**
  145.      * Controlla se <code>pWho</code> ha fatto black jack.
  146.      * @param pWho 0 per sapere del giocatore, 1 per il Shuffler.
  147.      * @return <code>true</code> se pWho ha fatto Black Jack, <code>false</code><br>
  148.      * in caso contrario.
  149.      */
  150.     public boolean isBlackJack(int pWho) {
  151.         if ((this.table[pWho][0].getNumber().equals("1") || this.table[pWho][1].getNumber().equals("1")) && (this.table[pWho][0].getValue() == 10 || this.table[pWho][1].getValue() == 10)) {
  152.             return true;
  153.         }
  154.         return false;
  155.     }
  156.  
  157.     /**
  158.      * Fa giocare il Shuffler.
  159.      */
  160.     public void play() {
  161.         this.table[1][0].setCoperta(false);
  162.         if (!this.isBusted()) {
  163.             while (this.getPoint(1) < 17 && this.numCard[1] <= 5) {
  164.                 this.card(1);
  165.             }
  166.         }
  167.     }
  168.  
  169.     /**
  170.      * Ritorna le carte di pWho.
  171.      * @param pWho 0 per sapere del giocatore, 1 per sapere del Shuffler.
  172.      * @return L'insieme delle carte di pWho.
  173.      */
  174.     public Card[] getCards(int pWho) {
  175.         Card[] cards = new Card[this.numCard[pWho]];
  176.         for (int i = 0; i < this.numCard[pWho]; i++) {
  177.             cards[i] = this.table[pWho][i];
  178.         }
  179.         return cards;
  180.     }
  181.  
  182.     /**
  183.      * Controlla se è possibile l'insurance, e quindi se la prima card del Shuffler<br>
  184.      * è un asso.
  185.      * @return <code>true</code> se è possibile, <code>false</code> in caso contrario.
  186.      */
  187.     public boolean isInsurancePossible() {
  188.         if (this.table[1][1].getNumber().equals("1")) {
  189.             return true;
  190.         }
  191.         return false;
  192.     }
  193.  
  194.     /**
  195.      * Il numero delle carte di pWho.
  196.      * @param pWho  0 per sapere del giocatore, 1 per sapere del banco.
  197.      * @return Un <code>int</code> contenente il numero delle carte di pWho.
  198.      */
  199.     public int getNumCarte(int pWho) {
  200.         return this.numCard[pWho];
  201.     }
  202.  
  203.     /**
  204.      * Controlla se il giocatore ha sballato.
  205.      * @return <code>true</code> se ha sballato, <code>false</code> in caso contrario.
  206.      */
  207.     public boolean isBusted() {
  208.         if (this.getPoint(0) > 21) {
  209.             return true;
  210.         }
  211.         return false;
  212.     }
  213.  
  214.     /**
  215.      * Setta il valore di <code>insurancepay</code>
  216.      * @param pInsurance <code>true</code> nel caso in cui bisogna pagare <br>
  217.      * l'insurance, <code>false</code> in caso contrario.
  218.      */
  219.     public void setInsurance(boolean pInsurance) {
  220.         this.insurancePay = pInsurance;
  221.     }
  222.  
  223.     /**
  224.      * Controlla se è possibile l'insurance.
  225.      * @return <code>true</code> nel caso sia possibile, <code>false</code> <br>
  226.      * in caso contrario.
  227.      */
  228.     public boolean isInsurance() {
  229.         return insurance;
  230.     }
  231.  
  232.     /**
  233.      * Setta il valore della variabile <code>insurance</code>
  234.      * @param insurance <code>true</code> o <code>false</code>
  235.      */
  236.     public void setIns(boolean insurance) {
  237.         this.insurance = insurance;
  238.     }
  239. }
 

Creative Commons License
Il layout di questo sito è concesso sotto licenza Creative Commons.
Per maggiori informazioni sulle licenze dei contenuti del sito, clicca.