Reputation:
I am trying my hand at using what I have learned to create a Blackjack game, but I seem to be having problems. It seems like everything I do in this project requires me to use static variables, but when I do, I'm not getting the correct answers. What makes java require variables to be static, even when you don't wish to use them, and is that the reason that my results are always wrong? i.e. You and the dealer having the same hand despite .remove(0) being called each time.
Card Class:
package cardGames;
import java.util.ArrayList;
public class Card {
private final Rank rank;
private final Suit suit;
private static final ArrayList<Card> deck = new ArrayList<Card>();
public enum Rank {
Two(2), Three(3), Four(4), Five(5), Six(6), Seven(7), Eight(8), Nine(9), Ten(10), Jack(10), Queen(10), King(10), Ace(11);
private int rankNum;
Rank(int value) {
this.rankNum = value;
}
public int getRankIDNum() {
return this.rankNum;
}
}
public enum Suit {
Clubs(0), Diamonds(1), Hearts(2), Spades(3);
private int suitNum;
Suit(int value) {
this.suitNum = value;
}
public int getSuitIDNum() {
return this.suitNum;
}
}
public Card(Rank rank, Suit suit) {
this.rank = rank;
this.suit = suit;
}
static {
for(Suit suit: Suit.values()) {
for(Rank rank: Rank.values()) {
deck.add(new Card(rank, suit));
}
}
}
public Rank getRank() {
return this.rank;
}
public Suit getSuit() {
return this.suit;
}
public static ArrayList<Card> createDeck() {
return new ArrayList<Card>(deck);
}
@Override
public String toString() {
return rank + " of " + suit;
}
}
And this is the class for my game.
Blackjack Class:
package cardGames;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class Blackjack {
static ArrayList<Card> deck;
static ArrayList<Card> hand = new ArrayList<Card>();
static ArrayList<Card> dealer = new ArrayList<Card>();
private static int value = 0;
private static int dealerValue = 0;
private static int choice;
private static Scanner play = new Scanner(System.in);
public static void main(String[] args) {
deck = Card.createDeck();
Collections.shuffle(deck);
Collections.shuffle(deck);
playBlackjack();
}
public static void playBlackjack() {
dealCards();
showHand();
showDealer();
System.out.println("\nThe value of your hand is: " + getValue(hand) + ". What would you like to do?"
+ "\n 1. Hit \t2. Stay \t3. Quit");
while(play.hasNextInt()) {
choice = play.nextInt();
switch(choice) {
case 1:
hit();
break;
case 2:
stay();
break;
case 3:
System.exit(0);
break;
default:
System.out.println("That was not a valid choice, please choose again.");
}
}
}
public static void dealCards() {
hand.add(deck.get(0));
deck.remove(0);
hand.add(deck.get(0));
deck.remove(0);
dealer.add(deck.get(0));
deck.remove(0);
dealer.add(deck.get(0));
deck.remove(0);
}
public static void showHand() {
for(Card card: hand) {
System.out.println(card.toString());
}
}
public static int getValue(ArrayList<Card> hand) {
for(Card card: hand) {
value += card.getRank().getRankIDNum();
}
return value;
}
public static void hit() {
hand.add(deck.get(0));
deck.remove(0);
if(getValue(hand) > 21) {
System.out.println("You have busted with a score of " + getValue(hand) + "! Game over.");
System.exit(0);
} else {
showHand();
System.out.println("The current value of your hand is: " + value + ".");
}
}
public static void stay() {
System.out.println("You are choosing to stay with a score of " + getValue(hand) + ". Let's see what the dealer gets.\n");
dealer();
}
public static void dealer() {
showDealer();
while(getDealer(dealer) > 17) {
System.out.println("The dealer is currently sitting at " + getDealer(dealer) + "."
+ "\nThe dealer draws a :" + deck.get(0).toString());
hitDealer();
showDealer();
}
if(getDealer(dealer) >= 17) {
System.out.println("The dealer is staying with his " + getDealer(dealer) + ".\n");
System.out.println("Your Hand: \n");
showHand();
System.out.println("\nDealer's Hand: \n");
showDealer();
if(getDealer(dealer) > getValue(hand)) {
System.out.println("They always say, 'Never bet against the house.");
} else if(getDealer(dealer) == getValue(hand)) {
System.out.println("It's a push, you have the same score as the dealer.");
} else {
System.out.println("Your " + getValue(hand) + " beat the dealer's " + getDealer(dealer) + ". Congratulations!");
}
System.out.println("Would you like to play again? (1. Yes \t2. No)");
choice = play.nextInt();
switch(choice) {
case 1:
new Blackjack();
break;
case 2:
System.out.println("\nThanks for playing.");
System.exit(0);
break;
default:
System.out.println("That was unintelligible, you must have had a little too much Chardonnay. We're calling you a cab.");
System.exit(0);
}
}
}
public static void showDealer() {
for(Card card: hand) {
System.out.println(card.toString());
}
System.out.println("\n");
}
public static int getDealer(ArrayList<Card> dealer) {
for(Card card: dealer) {
dealerValue += card.getRank().getRankIDNum();
}
return dealerValue;
}
public static void hitDealer() {
dealer.add(deck.get(0));
deck.remove(0);
}
}
Upvotes: 1
Views: 163
Reputation: 2053
Well, you brought it on yourself.
public static void main(String[] args) {
deck = Card.createDeck();
Collections.shuffle(deck);
Collections.shuffle(deck);
playBlackjack();
}
The createDeck()
method. Does it need to be static? the playBlackjack()
method. Does it need to be static? You need to work with objects by using the new
operator.
It is asking you to make everything static because you arent working with objects. the main method should be something like this.
public static void main(String[] args) {
Card cardDeck = new Card();
.... // your code here
card.playBlackjack();
}
Upvotes: 1
Reputation: 1805
You are doing all your logic in a static main method and thats why its asking for all the variables to be static.
Abstract out the entire logic of blackjack in a different class and use that class instance to do all your required tasks.
Upvotes: 0