0

As part of my assignment this week (computer programming I), I am supposed to work on the UNO game that we are working on throughout the class. The game will be played between ONLY computers, so no keyboard input except for the occasional enter to make the game continue (kinda boring right?). This week we are supposed to make some classes for each part of the game (such as the CARD class and the HAND class). I was able to do this, but the second part of my assignment--to have a driver program that: " creates a deck of UNO cards, deals the cards to two or more players, and displays the contents of each player’s hand."-- has me stuck. I've tried multiple printing methods that I know with my incredibly limited skills, but I've come up with nothing. Any Ideas?

Here's the Code:

public class CARD
{
    public String color;
    public int value;
    private Random random;
    private String face;

    public CARD(int v, String c)
    {
        value = v;
        color = c; 
    }

public CARD()
{
    random = new Random();
    value = random.nextInt(28); // 108 cards in a deck and it Can be reduced to 27 which ignores colors
    // Assigns value
    if (value >= 14) // Some cards show up more often (numbers)
        value -= 14;
    // Assigns color
    random = new Random();
    switch(random.nextInt(4) ) //learned about switches here: https://www.youtube.com/watch?v=RVRPmeccFT0
    {
        case 0: color = "Red"; 
            break;
        case 1: color = "Green"; 
            break;
        case 2: color = "Blue"; 
            break;
        case 3: color = "Yellow"; 
            break;
    }
    // If the card is wild 
    if (value >= 13)
        color = "none";
    }
}
  • Somehow, it seems a bit overkill for each `CARD` to create its own `new Random()`. – Solomon Slow Nov 11 '19 at 01:59
  • Also note, there is nothing random about an UNO deck. The deck always contains the same exact set of cards. The radomness in a game of UNO enters when you _shuffle_ the cards (i.e., when you take the fixed set of cards and you arrange them in some randomly chosen order.) – Solomon Slow Nov 11 '19 at 02:01

1 Answers1

0

Based on the code you've provided, I would recommend using a for loop to make cards:

public class DriverProgram {
    static final int players = 2, numberOfCards = 7;
    public static void main (String[] args){
         java.util.ArrayList<CARD> c = new java.util.ArrayList<CARD>();
         for(int i=0; i<numberOfCards; i++){
             c.add(new CARD()); // The previous example used a fixed size array
         } // Arrays.fill() would make all the cards identical
         System.out.println(c); 
         // Note: ArrayLists print themselves nicely with their toString method.
         // If you'd like, you could do String.replace("[", "").replace... to remove
         // certain characters.
    }
}

How do I print my Java object without getting "SomeType@2f92e0f4"?

FailingCoder
  • 757
  • 1
  • 8
  • 20