Question: So, I'm trying to create a Java UNO game and have manage to make the card classes and shuffler work and can print out the

So, I'm trying to create a Java UNO game and have manage to make the card classes and shuffler work and can print out the results, but next I need to make an array for the players hands, and for that I need to make the array list of my already shuffled cards and I'm stuck on how to fill it. Also if you want to see how it typically runs, just delete the part in the main class of the DeckofCardsTest with theDeckInPlay.

//card class represents playing card in the deck public class card { private String rank;//face of card private String color;//suit of card //two argument constructor initializes the cards' face and suit public card(String cardRank, String cardColor) { rank = cardRank; color = cardColor; }//end constructor //return String representation for Card public String toString() { return color + " " + rank; }//end method toString }//end class

import java.util.Random;

public class DeckofCards { private card deck[]; //array of card objects private int currentCard; // index of next card dealt private Random randomNumbers; // random number generator //constructor fills deck of cards public DeckofCards() { String rank[]= {"ZERO", "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE", "DRAW 2", "SKIP", "REVERSE", "ZERO", "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE", "DRAW 2", "SKIP", "REVERSE", "WILD", "WILD DRAW 4"}; String color[]= {"Red", "Blue", "Green", "Yellow"}; //doubling the array items mathmatically wasn't working so I ended up //just giving multiple versions of the same ranks to get the extra cards needed. deck = new card[112]; // create an array of card objects currentCard = 0; // set current card to first in deck randomNumbers = new Random();//creating generator //populate deck with card objects for(int count = 0; count < deck.length; count++) { deck[count]= new card(rank[count % 28], color[count/28]); }//end for loop to populate deck }//end DeckofCards //shuffle deck of cards with one pass algorithm public void shuffle() { //after shuffling, deal should start at deck[0] again currentCard = 0; //reinitalize currentCard //for each card, pick another random card and swap them for(int first = 0; first < deck.length; first++) { //select a random number between 0-51 int second = randomNumbers.nextInt(112); //swap current card with rand selected card card temp = deck[first]; deck[first] = deck[second]; deck[second] = temp; }//end for }//end shuffle //deal one card public card dealCard() { //detering whether cards are still to be dealt if (currentCard < deck.length) return deck[currentCard++];//return current card in array else return null;//return to indicate that all cards were dealt }//end deal card }//end DeckofCards class

import java.util.Arrays;

public class DeckofCardsTest { public static void main(String[] args) { DeckofCards myDeckofCards = new DeckofCards(); myDeckofCards.shuffle(); //placing cards in random order card[] theDeckinPlay; for (int x = 0; x<112; x++) theDeckinPlay[x] = myDeckofCards.dealCard(); //print cards in order dealt for(int i = 0; i<28; i++) { //deal & print 4 cards System.out.printf("%-20s%-20s%-20s%-20s ", myDeckofCards.dealCard(), myDeckofCards.dealCard(), myDeckofCards.dealCard(), myDeckofCards.dealCard()); }//end for }//end main }//end class

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!