blackjack;
package blackjack;
import java.util.*;
public class Blackjack implements BlackjackEngine {
private int playerAccount;
private int bet;
private Random randomGenerator;
private int numberOfDecks;
private ArrayList<Card> originalGameDeck;
private ArrayList<Card> playerHand;
private ArrayList<Card> dealerHand;
private int gameStatus;
/**
* Constructor you must provide. Initializes the player's account
* to 200 and the initial bet to 5. Feel free to initialize any other
* fields. Keep in mind that the constructor does not define the
* deck(s) of cards.
* @param randomGenerator
* @param numberOfDecks
*/
public Blackjack(Random randomGenerator, int numberOfDecks) {
this.playerAccount = 200;
this.bet = 5;
this.randomGenerator = randomGenerator;
this.numberOfDecks = numberOfDecks;
//throw new UnsupportedOperationException("You must implement this method.");
}
public int getNumberOfDecks() {
return this.numberOfDecks;
//throw new UnsupportedOperationException("You must implement this method.");
}
public void createAndShuffleGameDeck() {
originalGameDeck = new ArrayList<Card>();
for(int index = 0; index < this.numberOfDecks; index++){
for(CardSuit suit : CardSuit.values()) {
for(CardValue value : CardValue.values()) {
originalGameDeck.add(new Card(value, suit));
}
}
}
Collections.shuffle(this.originalGameDeck, this.randomGenerator);
//throw new UnsupportedOperationException("You must implement this method.");
}
public Card[] getGameDeck() {
Card[] gameDeckArray = new Card[this.originalGameDeck.size()];
return this.originalGameDeck.toArray(gameDeckArray);
//throw new UnsupportedOperationException("You must implement this method.");
}
public void deal() {
createAndShuffleGameDeck();
this.playerHand = new ArrayList<Card>();
this.dealerHand = new ArrayList<Card>();
this.playerHand.add(this.originalGameDeck.get(0));
//System.out.println(this.originalGameDeck.get(0).toString() + "******************************");
this.originalGameDeck.remove(0);
Card c = this.originalGameDeck.get(0);
this.originalGameDeck.remove(0);
c.setFaceDown();
this.dealerHand.add(c);
this.playerHand.add(this.originalGameDeck.get(0));
this.originalGameDeck.remove(0);
this.dealerHand.add(this.originalGameDeck.get(0));
this.originalGameDeck.remove(0);
setAccountAmount(this.playerAccount -= this.bet);
this.gameStatus = BlackjackEngine.GAME_IN_PROGRESS;
//throw new UnsupportedOperationException("You must implement this method.");
}
public Card[] getDealerCards() {
return getCards(this.dealerHand);
//throw new UnsupportedOperationException("You must implement this method.");
}
public int[] getDealerCardsTotal() {
return getCardsTotal(this.dealerHand);
//throw new UnsupportedOperationException("You must implement this method.");
}
public int getDealerCardsEvaluation() {
return getCardsEvaluation(getDealerCardsTotal(), this.dealerHand);
//throw new UnsupportedOperationException("You must implement this method.");
}
public Card[] getPlayerCards() {
return getCards(this.playerHand);
//throw new UnsupportedOperationException("You must implement this method.");
}
public int[] getPlayerCardsTotal() {
return getCardsTotal(this.playerHand);
//throw new UnsupportedOperationException("You must implement this method.");
}
public int getPlayerCardsEvaluation() {
return getCardsEvaluation(getPlayerCardsTotal(), this.playerHand);
//throw new UnsupportedOperationException("You must implement this method.");
}
public void playerHit() {
this.playerHand.add(this.originalGameDeck.remove(0));
//this.playerHand.get(this.playerHand.size() - 1).setFaceUp();
if(getPlayerCardsEvaluation() == BlackjackEngine.BUST) {
this.gameStatus = BlackjackEngine.DEALER_WON;
} else {
this.gameStatus = BlackjackEngine.GAME_IN_PROGRESS;
}
//throw new UnsupportedOperationException("You must implement this method.");
}
public void playerStand() {
this.dealerHand.get(0).setFaceUp();
int[] temp;
while(true) {
temp = getDealerCardsTotal();
if(temp == null || temp[temp.length - 1] >= 16) {
break;
} else {
this.dealerHand.add(this.originalGameDeck.remove(0));
}
}
int[] temp1 = getPlayerCardsTotal();
if(temp == null) {
this.gameStatus = BlackjackEngine.PLAYER_WON;
} else if(temp1 == null) {
this.gameStatus = BlackjackEngine.DEALER_WON;
} else if(temp.length == 2 && temp1.length == 2) {
if(temp[1] > temp1[1]) {
this.gameStatus = BlackjackEngine.DEALER_WON;
} else if(temp[1] < temp1[1]) {
this.gameStatus = BlackjackEngine.PLAYER_WON;
this.playerAccount += (this.bet * 2);
} else {
this.gameStatus = BlackjackEngine.DRAW;
this.playerAccount += this.bet;
}
} else if(temp.length == 2 && temp1.length == 1) {
if(temp[1] > temp1[0]) {
this.gameStatus = BlackjackEngine.DEALER_WON;
} else if(temp[1] < temp1[0]) {
this.gameStatus = BlackjackEngine.PLAYER_WON;
this.playerAccount += (this.bet * 2);
} else {
this.gameStatus = BlackjackEngine.DRAW;
this.playerAccount += this.bet;
}
} else if(temp.length == 1 && temp1.length == 2) {
if(temp[0] > temp1[1]) {
this.gameStatus = BlackjackEngine.DEALER_WON;
} else if(temp[0] < temp1[1]) {
this.gameStatus = BlackjackEngine.PLAYER_WON;
this.playerAccount += (this.bet * 2);
} else {
this.gameStatus = BlackjackEngine.DRAW;
this.playerAccount += this.bet;
}
} else if(temp.length == 1 && temp1.length == 1) {
if(temp[0] > temp1[0]) {
this.gameStatus = BlackjackEngine.DEALER_WON;
} else if(temp[0] < temp1[0]) {
this.gameStatus = BlackjackEngine.PLAYER_WON;
this.playerAccount += (this.bet * 2);
} else {
this.gameStatus = BlackjackEngine.DRAW;
this.playerAccount += this.bet;
}
}
/*
if(temp.length == 2) {
if(temp[1] < 16) {
temp = getDealerCardsTotal();
int[] temp1 = getPlayerCardsTotal();
if(temp == null) {
this.gameStatus = BlackjackEngine.PLAYER_WON;
} else if(temp[1] > temp1[1]) {
this.gameStatus = BlackjackEngine.DEALER_WON;
} else if(temp[1] == temp1[1]) {
this.gameStatus = BlackjackEngine.DRAW;
}
} else {
do {
this.dealerHand.add(this.originalGameDeck.get(0));
this.originalGameDeck.remove(0);
this.dealerHand.get(this.dealerHand.size() - 1).setFaceUp();
} while(temp[0] >= 16 && temp[0] <= 21);
temp = getDealerCardsTotal();
int[] temp1 = getPlayerCardsTotal();
if(temp == null) {
this.gameStatus = BlackjackEngine.PLAYER_WON;
} else if(temp[0] > temp1[0]) {
this.gameStatus = BlackjackEngine.DEALER_WON;
} else if(temp[0] == temp1[0]) {
this.gameStatus = BlackjackEngine.DRAW;
}
}
}
*/
//throw new UnsupportedOperationException("You must implement this method.");
}
public int getGameStatus() {
return this.gameStatus;
//throw new UnsupportedOperationException("You must implement this method.");
}
public void setBetAmount(int amount) {
this.bet = amount;
//throw new UnsupportedOperationException("You must implement this method.");
}
public int getBetAmount() {
return this.bet;
//throw new UnsupportedOperationException("You must implement this method.");
}
public void setAccountAmount(int amount) {
this.playerAccount = amount;
//throw new UnsupportedOperationException("You must implement this method.");
}
public int getAccountAmount() {
return this.playerAccount;
//throw new UnsupportedOperationException("You must implement this method.");
}
/* Feel Free to add any private methods you might need */
private Card[] getCards(ArrayList<Card> hand) {
Card[] handArray = new Card[hand.size()];
return hand.toArray(handArray);
}
private int[] getCardsTotal(ArrayList<Card> hand) {
boolean isAce = false;
for(int index = 0; index < hand.size(); index++) {
if(hand.get(index).getValue() == CardValue.Ace){
isAce = true;
}
}
int totalWithAceIsOne = 0, totalWithAceIsEleven = 0, totalWithNoAces = 0;
if(isAce) {
for(int index = 0; index < hand.size(); index++) {
totalWithAceIsOne += hand.get(index).getValue().getIntValue();
}
totalWithAceIsEleven = totalWithAceIsOne + 10;
if(totalWithAceIsOne > 21){
return null;
} else if(totalWithAceIsOne <= 21 && totalWithAceIsEleven > 21) {
int[] temp = {totalWithAceIsOne};
return temp;
} else {
int[] temp = {totalWithAceIsOne, totalWithAceIsEleven};
return temp;
}
} else {
for(int index = 0; index < hand.size(); index++) {
totalWithNoAces += hand.get(index).getValue().getIntValue();
}
if(totalWithNoAces > 21) {
return null;
}
int[] temp = {totalWithNoAces};
return temp;
}
}
private int getCardsEvaluation(int[] handTotal, ArrayList<Card> hand) {
if(handTotal == null) {
return BlackjackEngine.BUST;
} else if(handTotal.length == 2) {
if(handTotal[1] == 21) {
if(hand.size() > 2) {
return BlackjackEngine.HAS_21;
} else {
return BlackjackEngine.BLACKJACK;
}
}
} else {
if(handTotal[0] == 21) {
if(hand.size() > 2) {
return BlackjackEngine.HAS_21;
} else {
return BlackjackEngine.BLACKJACK;
}
}
}
return BlackjackEngine.LESS_THAN_21;
}
}
package blackjack;
import java.util.*;
public class Blackjack implements BlackjackEngine {
private int playerAccount;
private int bet;
private Random randomGenerator;
private int numberOfDecks;
private ArrayList<Card> originalGameDeck;
private ArrayList<Card> playerHand;
private ArrayList<Card> dealerHand;
private int gameStatus;
/**
* Constructor you must provide. Initializes the player's account
* to 200 and the initial bet to 5. Feel free to initialize any other
* fields. Keep in mind that the constructor does not define the
* deck(s) of cards.
* @param randomGenerator
* @param numberOfDecks
*/
public Blackjack(Random randomGenerator, int numberOfDecks) {
this.playerAccount = 200;
this.bet = 5;
this.randomGenerator = randomGenerator;
this.numberOfDecks = numberOfDecks;
//throw new UnsupportedOperationException("You must implement this method.");
}
public int getNumberOfDecks() {
return this.numberOfDecks;
//throw new UnsupportedOperationException("You must implement this method.");
}
public void createAndShuffleGameDeck() {
originalGameDeck = new ArrayList<Card>();
for(int index = 0; index < this.numberOfDecks; index++){
for(CardSuit suit : CardSuit.values()) {
for(CardValue value : CardValue.values()) {
originalGameDeck.add(new Card(value, suit));
}
}
}
Collections.shuffle(this.originalGameDeck, this.randomGenerator);
//throw new UnsupportedOperationException("You must implement this method.");
}
public Card[] getGameDeck() {
Card[] gameDeckArray = new Card[this.originalGameDeck.size()];
return this.originalGameDeck.toArray(gameDeckArray);
//throw new UnsupportedOperationException("You must implement this method.");
}
public void deal() {
createAndShuffleGameDeck();
this.playerHand = new ArrayList<Card>();
this.dealerHand = new ArrayList<Card>();
this.playerHand.add(this.originalGameDeck.get(0));
//System.out.println(this.originalGameDeck.get(0).toString() + "******************************");
this.originalGameDeck.remove(0);
Card c = this.originalGameDeck.get(0);
this.originalGameDeck.remove(0);
c.setFaceDown();
this.dealerHand.add(c);
this.playerHand.add(this.originalGameDeck.get(0));
this.originalGameDeck.remove(0);
this.dealerHand.add(this.originalGameDeck.get(0));
this.originalGameDeck.remove(0);
setAccountAmount(this.playerAccount -= this.bet);
this.gameStatus = BlackjackEngine.GAME_IN_PROGRESS;
//throw new UnsupportedOperationException("You must implement this method.");
}
public Card[] getDealerCards() {
return getCards(this.dealerHand);
//throw new UnsupportedOperationException("You must implement this method.");
}
public int[] getDealerCardsTotal() {
return getCardsTotal(this.dealerHand);
//throw new UnsupportedOperationException("You must implement this method.");
}
public int getDealerCardsEvaluation() {
return getCardsEvaluation(getDealerCardsTotal(), this.dealerHand);
//throw new UnsupportedOperationException("You must implement this method.");
}
public Card[] getPlayerCards() {
return getCards(this.playerHand);
//throw new UnsupportedOperationException("You must implement this method.");
}
public int[] getPlayerCardsTotal() {
return getCardsTotal(this.playerHand);
//throw new UnsupportedOperationException("You must implement this method.");
}
public int getPlayerCardsEvaluation() {
return getCardsEvaluation(getPlayerCardsTotal(), this.playerHand);
//throw new UnsupportedOperationException("You must implement this method.");
}
public void playerHit() {
this.playerHand.add(this.originalGameDeck.remove(0));
//this.playerHand.get(this.playerHand.size() - 1).setFaceUp();
if(getPlayerCardsEvaluation() == BlackjackEngine.BUST) {
this.gameStatus = BlackjackEngine.DEALER_WON;
} else {
this.gameStatus = BlackjackEngine.GAME_IN_PROGRESS;
}
//throw new UnsupportedOperationException("You must implement this method.");
}
public void playerStand() {
this.dealerHand.get(0).setFaceUp();
int[] temp;
while(true) {
temp = getDealerCardsTotal();
if(temp == null || temp[temp.length - 1] >= 16) {
break;
} else {
this.dealerHand.add(this.originalGameDeck.remove(0));
}
}
int[] temp1 = getPlayerCardsTotal();
if(temp == null) {
this.gameStatus = BlackjackEngine.PLAYER_WON;
} else if(temp1 == null) {
this.gameStatus = BlackjackEngine.DEALER_WON;
} else if(temp.length == 2 && temp1.length == 2) {
if(temp[1] > temp1[1]) {
this.gameStatus = BlackjackEngine.DEALER_WON;
} else if(temp[1] < temp1[1]) {
this.gameStatus = BlackjackEngine.PLAYER_WON;
this.playerAccount += (this.bet * 2);
} else {
this.gameStatus = BlackjackEngine.DRAW;
this.playerAccount += this.bet;
}
} else if(temp.length == 2 && temp1.length == 1) {
if(temp[1] > temp1[0]) {
this.gameStatus = BlackjackEngine.DEALER_WON;
} else if(temp[1] < temp1[0]) {
this.gameStatus = BlackjackEngine.PLAYER_WON;
this.playerAccount += (this.bet * 2);
} else {
this.gameStatus = BlackjackEngine.DRAW;
this.playerAccount += this.bet;
}
} else if(temp.length == 1 && temp1.length == 2) {
if(temp[0] > temp1[1]) {
this.gameStatus = BlackjackEngine.DEALER_WON;
} else if(temp[0] < temp1[1]) {
this.gameStatus = BlackjackEngine.PLAYER_WON;
this.playerAccount += (this.bet * 2);
} else {
this.gameStatus = BlackjackEngine.DRAW;
this.playerAccount += this.bet;
}
} else if(temp.length == 1 && temp1.length == 1) {
if(temp[0] > temp1[0]) {
this.gameStatus = BlackjackEngine.DEALER_WON;
} else if(temp[0] < temp1[0]) {
this.gameStatus = BlackjackEngine.PLAYER_WON;
this.playerAccount += (this.bet * 2);
} else {
this.gameStatus = BlackjackEngine.DRAW;
this.playerAccount += this.bet;
}
}
/*
if(temp.length == 2) {
if(temp[1] < 16) {
temp = getDealerCardsTotal();
int[] temp1 = getPlayerCardsTotal();
if(temp == null) {
this.gameStatus = BlackjackEngine.PLAYER_WON;
} else if(temp[1] > temp1[1]) {
this.gameStatus = BlackjackEngine.DEALER_WON;
} else if(temp[1] == temp1[1]) {
this.gameStatus = BlackjackEngine.DRAW;
}
} else {
do {
this.dealerHand.add(this.originalGameDeck.get(0));
this.originalGameDeck.remove(0);
this.dealerHand.get(this.dealerHand.size() - 1).setFaceUp();
} while(temp[0] >= 16 && temp[0] <= 21);
temp = getDealerCardsTotal();
int[] temp1 = getPlayerCardsTotal();
if(temp == null) {
this.gameStatus = BlackjackEngine.PLAYER_WON;
} else if(temp[0] > temp1[0]) {
this.gameStatus = BlackjackEngine.DEALER_WON;
} else if(temp[0] == temp1[0]) {
this.gameStatus = BlackjackEngine.DRAW;
}
}
}
*/
//throw new UnsupportedOperationException("You must implement this method.");
}
public int getGameStatus() {
return this.gameStatus;
//throw new UnsupportedOperationException("You must implement this method.");
}
public void setBetAmount(int amount) {
this.bet = amount;
//throw new UnsupportedOperationException("You must implement this method.");
}
public int getBetAmount() {
return this.bet;
//throw new UnsupportedOperationException("You must implement this method.");
}
public void setAccountAmount(int amount) {
this.playerAccount = amount;
//throw new UnsupportedOperationException("You must implement this method.");
}
public int getAccountAmount() {
return this.playerAccount;
//throw new UnsupportedOperationException("You must implement this method.");
}
/* Feel Free to add any private methods you might need */
private Card[] getCards(ArrayList<Card> hand) {
Card[] handArray = new Card[hand.size()];
return hand.toArray(handArray);
}
private int[] getCardsTotal(ArrayList<Card> hand) {
boolean isAce = false;
for(int index = 0; index < hand.size(); index++) {
if(hand.get(index).getValue() == CardValue.Ace){
isAce = true;
}
}
int totalWithAceIsOne = 0, totalWithAceIsEleven = 0, totalWithNoAces = 0;
if(isAce) {
for(int index = 0; index < hand.size(); index++) {
totalWithAceIsOne += hand.get(index).getValue().getIntValue();
}
totalWithAceIsEleven = totalWithAceIsOne + 10;
if(totalWithAceIsOne > 21){
return null;
} else if(totalWithAceIsOne <= 21 && totalWithAceIsEleven > 21) {
int[] temp = {totalWithAceIsOne};
return temp;
} else {
int[] temp = {totalWithAceIsOne, totalWithAceIsEleven};
return temp;
}
} else {
for(int index = 0; index < hand.size(); index++) {
totalWithNoAces += hand.get(index).getValue().getIntValue();
}
if(totalWithNoAces > 21) {
return null;
}
int[] temp = {totalWithNoAces};
return temp;
}
}
private int getCardsEvaluation(int[] handTotal, ArrayList<Card> hand) {
if(handTotal == null) {
return BlackjackEngine.BUST;
} else if(handTotal.length == 2) {
if(handTotal[1] == 21) {
if(hand.size() > 2) {
return BlackjackEngine.HAS_21;
} else {
return BlackjackEngine.BLACKJACK;
}
}
} else {
if(handTotal[0] == 21) {
if(hand.size() > 2) {
return BlackjackEngine.HAS_21;
} else {
return BlackjackEngine.BLACKJACK;
}
}
}
return BlackjackEngine.LESS_THAN_21;
}
}