Question: I'm getting an error with this code. error: cannot access Deck.bad source file: ./Deck.java file does not contain class Deck Please remove or make sure
I'm getting an error with this code. error: cannot access Deck.bad source file: ./Deck.java file does not contain class Deck Please remove or make sure it appears in the correct subdirectory of the sourcepath. Also, that file is in the same project folder, any ideas of what I should do? Here is the code, where the error is occurring. I'm also getting errors when i try to compile the deck class too. error: cannot find symbol private ArrayList
error: cannot find symbol int randomIndex = Randomizer.nextInt(52);. error: cannot find symbol Card one = deck.get(i);. error: cannot find symbol Card two = deck.get(randomIndex);. symbol: class Card location: class Deck.
(black jack demo class)
import java.util.Scanner; public class BlackJackDemo { public static final int HEARTS = 0; public static final int DIAMONDS = 1; public static final int SPADES = 2; public static final int CLUBS = 3; public static final int JACK = 11; public static final int QUEEN = 12; public static final int KING = 13; public static final int ACE = 14;
public static void main(String[] args) { BlackJackDemo dj = new BlackJackDemo(); //BlackJack mechanism starts here Scanner keyboard = new Scanner(System.in); String s = "yes"; int i = 0; int j = 0;
while(s.equalsIgnoreCase("yes")) { i++; Hand dealer = new Hand(); Hand player = new Hand(); Deck deck = new Deck(); dj.playerTurn(player,deck); System.out.println("Player's hand has value" + player.getValue()); if(player.busted()) { System.out.println("Dealer wins"); } else { dj.dealerTurn(dealer,deck); if(dealer.busted()) { System.out.println("player wins"); j++; } else if(dj.playerWins(player,dealer)) { System.out.println("player wins"); j++; } else
System.out.println("dealer wins"); } //Continue to the process if yes was interested //and stop the process if no was pressed. System.out.println("Do you want to play again?(yes/no)"); s = keyboard.next(); }
System.out.println("Statistics:"); //Printing number of games played. System.out.println("Number of games played:" +i); //Calculating win percentage. System.out.println("Wins" + j/(double)i*100 + "%"); }
String getPlayerMove() { Scanner kb = new Scanner(System.in); String input; while(true) { System.out.print("Do you want to stand or hit ?"); input = keyboard.nextLine(); if(input.equals("hit") || input.equals("stand")) { //input = keyboard.nextLine(); return input; } System.out.println("Please try again."); } }
private void dealerTurn(Hand dealer, Deck deck) { while(true) { System.out.println("Dealer's hand"); System.out.println(dealer); int value = dealer.getValue(); System.out.println("Dealer's hand has value" + value); if(value < 17) { System.out.println("Dealer hits"); Card c = deack.deal(); dealer.addCard(c); System.out.println("Dealer card was " + c); if(dealer.busted()) { System.out.println("Dealer busted!"); break; } } else { System.out.println("Dealer stands."); break; } } }
private boolean playerTurn(Hand player,Deck deck) { while(true) { System.out.println("Players's hand has value" + player.getValue()); String move = getPlayerMove(); if(move.equals("hit")) { Card c = deck.deal(); System.out.println("Your card was: " + c); player.addCard(c); System.out.println("Player's hand"); System.out.println(player); if(player.busted()) { return true; } } else { return false; } } }
private boolean playerWins(Hand player,Hand dealer) { if(player.busted()) { return false; } if(dealer.busted()) { return player.getValue() >= dealer.getValue(); } } }
(the deck class)
package dec; import java.util.*; public class Deck {
public static final int HEARTS = 0; public static final int DIAMONDS = 1; public static final int SPADES = 2; public static final int CLUBS = 3; public static final int JACK = 11; public static final int QUEEN = 12; public static final int KING = 13; public static final int ACE = 14; private ArrayList
//This creates a deck. A deck starts as a list of 52 cards. //We loop through each suit and rank and construct a card and //add it to the deck. public Deck() { deck = new ArrayList
for(int rank = 2; rank <= ACE; rank++) { for(int suit = HEARTS; suit <= CLUBS; suit++) { Card card = new Card(rank, suit); deck.add(card); } } shuffle();//Shuffling the cards for the first time. }
//This getter method returns the ArrayList of cards. public ArrayList getCards() { return deck; }
//This deals the first card from the deck by removing it. public Card deal() { return deck.remove(0); }
//This shuffles the deck by making 52 swaps of cards positions. public void shuffle() { for(int i = 0; i < deck.size(); i++) { int randomIndex = Randomizer.nextInt(52); Card one = deck.get(i); Card two = deck.get(randomIndex); deck.set(i,two); deck.set(randomIndex,one); } } }
(the card class)
public class Card { public static final int HEARTS = 0; public static final int DIAMONDS = 1; public static final int SPADES = 2; public static final int CLUBS = 3; public static final int JACK = 11; public static final int QUEEN = 12; public static final int KING = 13; public static final int ACE = 14; //This represents the rank of the card, the value from 2 to Ace. private int rank; //This represents the suit of the card, one of hearts, diamonds, spades, or clubs. private int suit; //This represents the value of the card, which is 10 for face cards or 11 for an Ace. private int value; //The reason for the two X's in the front provide padding so numbers //have their String representation for their corresponding index. private String[]ranks = {"X","X","2","3","4","5","6","7","8","8","9","10","Jack","Queen", "King","Ace"}; //The String array makes it easier to get the String value of the Card for its suit. private String[]suits = {"HEARTS","DIAMONDS","SPADES","CLUBS"};
//This is the constructor to create a new card. //To create a new card we pass in its rank and its suit. public Card(int r, int s) { rank = r; suit = s; }
public void setRank(int rank) { this.rank = rank; }
public void setValue(int value) { this.value = value; } //This getter method returns the rank of the cards an an integer. public int getRank() { return rank; }
//This getter method returns the suit of the cards an integer. public int getSuit() { return suit; }
//This getter method returns the value of the card as an integer. //For facecards the value is 10, which is different than their rank //underlying value. //Four aces the default value is 11. public int getValue() { int value = rank; if(rank > 10) { value = 10; } if(rank == ACE) { value = 11; } return value; }
//This utility method converts from a rank integer to a String. public String rankToString(int r) { return ranks[r]; }
//This utility method converts from a suit integer to a String. public String suitToString(int s) { return suits[s]; }
//Returns the String version of the suit. public String getSuitAsString() { return suitToString(suit); }
//Returns the String version of the rank. public String getRankAsString() { return rankToString(rank); }
//This toString method returns the String representation of a card //which will be two characters. //For instance, the two of hearts would return 2HEARTS. //Face cards have a short string so the ace of spades would return AceSpades. public String toString() { String rankString = ranks[rank]; String suitString = suits[suit]; return rankString + suitString; } }
(the hand class)
import java.util.*; public class Hand { public static final int HEARTS = 0; public static final int DIAMONDS = 1; public static final int SPADES = 2; public static final int CLUBS = 3; public static final int JACK = 11; public static final int QUEEN = 12; public static final int KING = 13; public static final int ACE = 14; private ArrayList
//This constructor sets up our hand by initializing our ArrayList. public Hand() { cards = new ArrayList
//This adds a card to our hand. public void addCard(Card c) { cards.add(c); }
//This returns the value of the hand as an integer. //The value of the hand is the sum of the values of the individual cards. //There is also an adjustment made for the value of an ace //which can be 11 or 1 depending on the situation. public int getValue() { int sum = 0; int aceCount = 0;
for(Card c: cards) { sum += c.getValue(); if(c.getRank() == ACE) { aceCount++; } } while(sum > 21 && aceCount > 0) { sum -= 10; aceCount--; } return sum; }
//Return if this hand has a blackjack. public boolean hasBlackJack() { return getValue() == 21 && cards.size() == 2; }
//Return if the hand is busted, which means it has value //greater than 21. public boolean busted() { return getValue() > 21; }
}
(the randomizer class)
import java.util.*; public class Randomizer { public static Random theInstance = null;
public Randomizer() {
}
public static Random getInstance() { if(theInstance == null) { theInstance = new Random(); } return theInstance; }
//Returns a random boolean value. public static boolean nextBoolean() { return Randomizer.getInstance().nextBoolean(); }
//This method simulated a wieghted coin flip which will return //true with the probability passed as a parameter. public static boolean nextBoolean(double probability) { return Randomizer.nextDouble() < probability; }
//This method returns a random integer. public static int nextInt() { return Randomizer.getInstance().nextInt(); }
//This method returns a random integer between 0 and n, exclusive. public static int nextInt(int n) { return Randomizer.getInstance().nextInt(n); }
//Returns a number between min and mix, inclusive. public static int nextInt(int min, int max) { return min + Randomizer.nextInt(max - min + 1); }
//Returns a random double between 0 and 1. public static double nextDouble() { return Randomizer.getInstance().nextDouble(); }
//Returns a random double between min and max. public static double nextDouble(double min, double max) { return min + (max - min) * Randomizer.nextDouble(); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
