package blackjack;
/**
* Rappresenta un banco di black jack.
* @author paolo
*/
public class Shuffler {
/**
* Il mazzo di carte utilizzato dal Shuffler.
*/
private Deck deck;
/**
* Le carte sul tavolo.
*/
private Card[][] table;
/**
* Il numero di carte prelevate dal Shuffler e dal giocatore.
*/
private int[] numCard;
/**
* Variabile booleana per controllare se il banco deve pagare l'insurance.
*/
private boolean insurancePay;
/**
* Variabile booleana per controllare se è stata effettiuata l'insurance.
*/
private boolean insurance;
/**
* La puntata effettuata.
*/
private int bet;
/**
* Il costruttore di default, inizializza il mazzo e lo mischia.
*/
public Shuffler() {
this.deck = new Deck();
this.deck.shuffle();
this.table = new Card[2][5];
this.numCard = new int[2];
this.insurancePay = false;
this.insurance = false;
}
/**
* Serve le prime 4 carte.
*/
public void start() {
this.table[0][0] = this.deck.getCard(0);
this.table[1][0] = this.deck.getCard(1);
this.table[1][0].setCoperta(true);
this.table[0][1] = this.deck.getCard(2);
this.table[1][1] = this.deck.getCard(3);
this.numCard[0] = 2;
this.numCard[1] = 2;
}
/**
* Richiede una card.
* @param pWho chi ha richiesto la card, 0 per il giocatore, 1 per il banco.
*/
public void card(int pWho) {
this.table[pWho][this.numCard[pWho]] = this.deck.getCard(this.numCard[0] + this.numCard[1]);
this.numCard[pWho]++;
}
/**
* Ritorna il punteggio.
* @param pWho 0 per sapere il punteggio del giocatore, 1 per quello del banco.
* @return Un <code>int</code> contenente il punteggio di <code>pWho</code>
*/
public int getPoint(int pWho) {
int player = 0;
boolean isAnAce = false;
int numAce = 0;
for (int i = 0; i < this.numCard[pWho]; i++) {
if (!this.table[pWho][i].isCover()) {
player = player + this.table[pWho][i].getValue();
if (this.table[pWho][i].getNumber().equals("1")) {
isAnAce = true;
numAce++;
}
}
}
for (int i = 0; i < numAce; i++) {
if (player > 21 && isAnAce) {
player = player - 10;
}
}
return player;
}
/**
* Il valore della puntata.
* @return Un <code>int</code> contenente il valore della puntata.
*/
public int getBet() {
return bet;
}
/**
* Setta la puntata.
* @param pBet La puntata.
*/
public void setBet(int pBet) {
if(pBet>0){
this.bet = pBet;
}
}
/**
* Controlla se il giocatore è vincente.
* @return <code>true</code> se è vincente, <code>false</code> se non lo è.
*/
public boolean isWinner() {
int giocatore = this.getPoint(0);
int banco = this.getPoint(1);
if (giocatore > banco && giocatore <= 21 || (banco > 21)) {
return true;
}
return false;
}
/**
* Paga il giocatore.
* @return Quanto bisogna pagare al giocatore.
*/
public int pay() {
int retval = 0;
if (this.isWinner()) {
retval = this.bet * 2;
}
if (this.isBlackJack(0) && !this.isBlackJack(1)) {
retval = (this.bet * 2) + (this.bet / 2);
}
if (this.insurancePay) {
retval = retval + this.bet;
}
return retval;
}
/**
* Controlla se <code>pWho</code> ha fatto black jack.
* @param pWho 0 per sapere del giocatore, 1 per il Shuffler.
* @return <code>true</code> se pWho ha fatto Black Jack, <code>false</code><br>
* in caso contrario.
*/
public boolean isBlackJack(int pWho) {
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)) {
return true;
}
return false;
}
/**
* Fa giocare il Shuffler.
*/
public void play() {
this.table[1][0].setCoperta(false);
if (!this.isBusted()) {
while (this.getPoint(1) < 17 && this.numCard[1] <= 5) {
this.card(1);
}
}
}
/**
* Ritorna le carte di pWho.
* @param pWho 0 per sapere del giocatore, 1 per sapere del Shuffler.
* @return L'insieme delle carte di pWho.
*/
public Card[] getCards(int pWho) {
Card[] cards = new Card[this.numCard[pWho]];
for (int i = 0; i < this.numCard[pWho]; i++) {
cards[i] = this.table[pWho][i];
}
return cards;
}
/**
* Controlla se è possibile l'insurance, e quindi se la prima card del Shuffler<br>
* è un asso.
* @return <code>true</code> se è possibile, <code>false</code> in caso contrario.
*/
public boolean isInsurancePossible() {
if (this.table[1][1].getNumber().equals("1")) {
return true;
}
return false;
}
/**
* Il numero delle carte di pWho.
* @param pWho 0 per sapere del giocatore, 1 per sapere del banco.
* @return Un <code>int</code> contenente il numero delle carte di pWho.
*/
public int getNumCarte(int pWho) {
return this.numCard[pWho];
}
/**
* Controlla se il giocatore ha sballato.
* @return <code>true</code> se ha sballato, <code>false</code> in caso contrario.
*/
public boolean isBusted() {
if (this.getPoint(0) > 21) {
return true;
}
return false;
}
/**
* Setta il valore di <code>insurancepay</code>
* @param pInsurance <code>true</code> nel caso in cui bisogna pagare <br>
* l'insurance, <code>false</code> in caso contrario.
*/
public void setInsurance(boolean pInsurance) {
this.insurancePay = pInsurance;
}
/**
* Controlla se è possibile l'insurance.
* @return <code>true</code> nel caso sia possibile, <code>false</code> <br>
* in caso contrario.
*/
public boolean isInsurance() {
return insurance;
}
/**
* Setta il valore della variabile <code>insurance</code>
* @param insurance <code>true</code> o <code>false</code>
*/
public void setIns(boolean insurance) {
this.insurance = insurance;
}
}