Question: Helllo, I have a bug in my Java code and can't seem to figure out where it all went wrong. I want the player to
Helllo, I have a bug in my Java code and can't seem to figure out where it all went wrong. I want the player to draw only 5 cards but they keep drawing 6 cards instead. I have pasted my code below. It would be great if you could run it and see what I can change to fix this. Please and thank you!
MY CARD CLASS
public class Card {
public static final int ACE = 1; public static final int TWO = 2; public static final int THREE = 3; public static final int FOUR = 4; public static final int FIVE = 5; public static final int SIX = 6; public static final int SEVEN = 7; public static final int EIGHT = 8; public static final int NINE = 9; public static final int TEN = 10; public static final int JACK= 11; public static final int QUEEN = 12; public static final int KING = 13; public static final int CLUBS = 1; public static final int DIAMONDS = 2; public static final int HEARTS = 3; public static final int SPADES = 4; public int face, suit; public String faceName, suitName; Random rand = new Random(); public Card() { int randomFace = rand.nextInt(13) + 1; int randomSuit = rand.nextInt(4) + 1; face = randomFace; suit= randomSuit; } public Card(int face, int suit) { this.face = face; this.suit = suit; } @Override public String toString() { switch(face) { case ACE: faceName = "Ace"; break; case TWO: faceName = "Two"; break; case THREE: faceName = "Three"; break; case FOUR: faceName = "Four"; break; case FIVE: faceName = "Five"; break; case SIX: faceName = "Six"; break; case SEVEN: faceName = "Seven"; break; case EIGHT: faceName = "Eight"; break; case NINE: faceName = "Nine"; break; case TEN: faceName = "Ten"; break; case JACK: faceName = "Eleven"; break; case QUEEN: faceName = "Twelve"; break; case KING: faceName = "Thirteen"; break; } switch(suit) { case CLUBS: suitName = "Clubs"; break; case DIAMONDS: suitName = "Diamonds"; case HEARTS: suitName = "Hearts"; case SPADES: suitName = "Spades"; } return faceName + " of " + suitName; } }
MY PLAYER CLASS
public class Player {
private Card[]cards; private int numCards; Random rand = new Random(); public Player() { //cards = new Card[5]; deck(); shuffle(); } public void deck() { Card[] deckOfCards = new Card[52];
int cardIndex = 0; for (int face = Card.ACE; face <= Card.KING; face++) for (int suit = Card.CLUBS; suit <= Card.SPADES; suit++) deckOfCards[cardIndex++] = new Card(face, suit); this.numCards = 52; this.cards = deckOfCards; } public void shuffle() { int N = this.numCards; for (int i = 0; i < N; i++) { int r = rand.nextInt(i + 1); Card temp = this.cards[r]; this.cards[r] =this.cards[i]; this.cards[i] = temp; } }
public void drawCards() { for (int i=0; i<5; i++) { cards[i] = this.cards[i]; } } @Override public String toString() { String cardsToString = ""; for (int i = 0; i < cards.length; i++) { cardsToString=cardsToString+cards[i].toString()+" "; } return cardsToString; } }
MY DRIVER CLASS
public class Driver {
public static void main(String[]args) { Player b = new Player(); b.drawCards(); System.out.println(b); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
