1

I'm new to Java and I am having trouble wrapping my mind around one of the concepts.

The assignment I am currently working on is the card game War. The current instructions is for me to remove a random card from a a deck of cards.

I have created an array, but it is an array of class Card. The class creates the card by basically adding an int and a String together. I then created the array from that class. In my mind, I neither have an int or a String in my array, is that correct?

Now I need to remove one of the random cards from the deck and give it to a player. This is where I am getting lost. I would think I can just use Random to remove a random card, but I always seem to get an error.

I'm not asking for you to do the assignment for me, but if you would please point me in the right direction and possibly correct me if I am confused.

Current Class I am working on:

import java.util.Random;
import java.util.*;

public class War3
{

    Random ran = new Random();

    public FullDeck randomCard()
    {
        ArrayList <FullDeck> randCard = new ArrayList <FullDeck> (52);

        int index = ran.nextInt(randCard.size());
        FullDeck x = randCard.remove(index);
        return x;
    }

    public void display()
    {
         System.out.println("Your card is" + randomCard());

    }
}

Entire project for clarification Java - War Game - Gist

Many thanks in advance.

MBH
  • 131
  • 9
  • What's the error you're getting? – Mureinik Mar 29 '14 at 14:57
  • You have not actually added any cards to the deck yet. The constructor `new ArrayList (52);` just makes space, it does not fill it with anything. – Salix alba Mar 29 '14 at 15:03
  • I have browsed your code on github.It seems you didn't put "Card" in the list?`ArrayList randCard = new ArrayList (52);` Should the be ? – yellowB Mar 29 '14 at 15:07
  • The error I am getting is "Exception in thread "main" java.lang.IllegalArgumentException: n must be positive". I think part of the confusion is the professors wants us to use FullDeck instead of using another way to select a random card. yellowB, I think you're right. I think I may not be using the ArrayList properly. Let me switch things around. – MBH Mar 29 '14 at 15:32

2 Answers2

0

Two issues that I see:

  1. You're creating an ArrayList that can hold a reference to 52 instances of the FullDeck class, but you're not adding anything to it. You need to do something like randCard.add(new FullDeck()) 52 times/in a loop**. Then likely you'll want to "shuffle" the deck, see this question for how to do that.
  2. You're naming is a little weird...the FullDeck class in actuality seems like it should be renamed to just Card, and the randCard variable should be renamed to something like fullDeck...after you've added 52 Cards.

**EDIT: actually, the generation of a deck of cards will be more complicated to make sure you don't have any duplicate cards.

Community
  • 1
  • 1
Sam Dozor
  • 40,335
  • 6
  • 42
  • 42
  • 1. If I understand correctly, FullDeck should already have 52 of Card in it.Does the ArrayList not "pull" that information from FullDeck.java? 2. I think my own confusion may be causing problems with the naming I'm using. – MBH Mar 29 '14 at 15:41
  • No, you're creating an array of a certain size and type, but not actually putting anything in it. – Sam Dozor Mar 29 '14 at 15:46
  • Well that definitely clarifies the problem. My assumption that it would "pull" was entirely false. Thank you. – MBH Mar 29 '14 at 15:59
  • Sam_D is right - I think there is some confusion here also about what the generic parameter is doing. By specifying `` when you declare your `ArrayList`, you're telling the compiler what *kind* of objects that list can contain, but not where to go get them from. – Craig Otis Mar 29 '14 at 16:01
  • @Sam_D Regarding the generation of a shuffled deck, there are two initial approaches. *The bad one:* Generate a random card, and check to see if you've added it already. If you have, generate again. Otherwise, add it. Repeat until you have 52 cards. *The good one:* A nested for loop - one for suit (0-3), one for value (0-12). Then use `Collections.shuffle()`. – Craig Otis Mar 29 '14 at 16:11
  • @Craig Otis That first approach you suggest isn't acceptable, it's best case comp. complexity is o(n^2) and worst case it will actually never finish executing. – Sam Dozor Mar 29 '14 at 16:21
  • @Sam_D That's why it's the bad one. But I've seen it commonly chosen by early programmers as the first approach for this kind of problem. I merely meant to offer an alternative. – Craig Otis Mar 29 '14 at 16:32
0

ArrayList <FullDeck> randCard = new ArrayList <FullDeck> (52);

This creates an ArrayList. You do not need to specify the number 52, since ArrayLists grow dynamically, as opposed to Arrays. The call is similar to ArrayList <FullDeck> randCard = new ArrayList <FullDeck> ();, the difference being that the constructor you used sets the initial capacity of the ArrayList to 52. That in no way restricts the size of the ArrayList though.

Anyway, you are creating a new, empty ArrayList. Then you want the size, but since you didn't put anything into the list, it is still empty, to the size is zero. You then try to call ran.nextInt(0)... nextInt(int n) expects a number greater than zero. From the javadoc:

public int nextInt(int n) {
 if (n <= 0)
  throw new IllegalArgumentException("n must be positive");
blueygh2
  • 1,538
  • 10
  • 15
  • But, if OP knows how many cards he has (52), it *is* good practice to use this as the initial capacity. So saying he "doesn't need to specify the number 52" is technically correct, but doing so does have its advantages. – Craig Otis Mar 29 '14 at 15:59
  • Yes, I am starting to see that now. I thought would refer back to the array in FullDeck.java. Let me rework this and try again. Thank you. – MBH Mar 29 '14 at 16:00
  • @MBH I can see another problem having looked at the full code. You use `new Card();` to deal cards to player and computer. However, what that does it create a new, blank card. Your Card constructor sets `number` and `suit`, but since its a new Card, these values will have the default values of 0. – blueygh2 Mar 29 '14 at 16:06
  • @blueygh2 - Agreed. We needed to create the cards, assign them a random number and suit, deal them to two players, and compare. I left them blank initially so cards could be given a number, compared, then add the suit. In the next assignment we were told to create a deck, which is the array. Now we need to pick a random card from the deck. Logic tells me I should be able to grab 1 of Card from the array FullDeck. This is where I am getting hung up because it is no longer an int, it is a class, correct? If it were still an int it would be easy. – MBH Mar 29 '14 at 16:15
  • @MBH If you change the constructor of FullDeck, move the array up one level and add a method to retrieve cards, you could call something like FullDeck.giveMeACard(); (if the method was static) – blueygh2 Mar 29 '14 at 16:19
  • @blueygh2 - Understood. Unfortunately the assignment says: Use the FullDeck class. Select a random number for the deck position of the player’s first card an assign the card at that array position to the player...Save in War3.java – MBH Mar 29 '14 at 16:28
  • @blueygh2 - So my logic goes back to - how the heck do I get a Card from the FullDeck array? – MBH Mar 29 '14 at 16:30
  • @MBH As I said, you have to provide access to the array. It is encapsulated inside the FullDeck class and not visible. (The scope should probably different as well). So you can either provide access to it by returning the deck array from inside FullDeck, or by having a method in FullDeck that returns a (random) card from the deck array. – blueygh2 Mar 29 '14 at 16:33
  • @blueygh2 - OK, I understand. Let me see if I can make that happen. – MBH Mar 29 '14 at 16:53
  • @blueygh2 - I think I'm losing my mind. Now I can easily create a random card, but my for loop will not generate the deck. https://gist.github.com/anonymous/9861596. Do the Java gods hate me? – MBH Mar 29 '14 at 19:51
  • @MBH can be continue this discussion somewhere else? I don't think it's a good idea to keep this as comments – blueygh2 Mar 29 '14 at 20:51