Note that there are some explanatory texts on larger screens.

plurals
  1. POI need help drawing a Hand of Cards
    primarykey
    data
    text
    <p>I am trying to draw a Hand of Cards (in the Hand class) but have it shuffled. My problem is if I do shuffleDeck() in the initialDeal() method (where I need to draw a Hand of Cards but shuffled) it gives me an <code>ArrayIndexOutOfBounds</code> exception.</p> <p>And I draw two narfs OF Clubs... Note, the narf is basically 0 and just a placeholder. It will not be used.</p> <pre><code>class Card { int suit, rank; public Card () { this.suit = 0; this.rank = 0; } public Card (int suit, int rank) { this.suit = suit; this.rank = rank; } public int getSuit() { return suit; } public int getRank() { return rank; } public void printCard () { String[] suits = { "Clubs", "Diamonds", "Hearts", "Spades" }; String[] ranks = { "narf", "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King" }; System.out.println (ranks[rank] + " of " + suits[suit]); } //end printCard } //end class </code></pre> <p>Card class(this is basically doing basic stuff) ^</p> <pre><code>import java.util.Random; class Hand { static Card[] deck = new Card[52]; Card[] hand = new Card[10]; static int index; public static void createDeck () { int m = 0; for (int j = 0; j &lt; 4; j++) { for (int k = 1; k &lt; 14; k++) { deck[m] = new Card(j,k); m++; } } index = 0; } // end createDeck public static Card deal () { Hand.index++; return deck[index-1]; } // end deal public static int compareCard (Card c1, Card c2) { if (c1.getSuit() &gt; c2.getSuit()) return 1; if (c1.getSuit() &lt; c2.getSuit()) return -1; if (c1.getRank() &gt; c2.getRank()) return 1; if (c1.getRank() &lt; c2.getRank()) return -1; return 0; } // end compareCard public void printDeck (int size) { int j = 0; while (j &lt; size) { deck[j].printCard(); j++; } }// end printDeck public static void shuffleDeck () { Card tempCard; Random rd = new Random(); for (index = 0; index &lt; deck.length; index++) { int r = rd.nextInt(deck.length); tempCard = deck[index]; deck[index] = deck[r]; deck[r] = tempCard; } } // end shuffleDeck public void initialDeal() { hand[0] = null; hand[1] = null; hand[0] = deal(); hand[1] = deal(); } public void printHand() { initialDeal(); index = 0; for(Card outputCard = new Card(); hand[index] != null; index++) { outputCard.printCard(); } } } </code></pre> <p>Hand class^ (this is where I need to draw two cards from a shuffled deck) </p> <pre><code>class Dealer { Hand dealer = new Hand(); public Dealer () { dealer.createDeck(); dealer.shuffleDeck(); System.out.println("Dealer's Hand"); System.out.println(""); initDeal(); } public void initDeal() { dealer.initialDeal(); dealer.printHand(); } } //end class </code></pre> <p>Dealer class.^ Calling the methods of Hand</p> <pre><code>class Driver { public static void main (String[] args) { Dealer dealer = new Dealer(); } //end main method } //end class </code></pre> <p>^ Running everything basically</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload