0

I was wondering if I could change this in any way so that I could have the output:

P1 Hand: 10 of diamonds, 9 of clubs, 5 of spades, Queen of spades, 4 of clubs.
P2 Hand: 7 of diamonds, 10 of spades, Jack of clubs, 6 of diamonds, 5 of clubs.
P3 Hand: 8 of spades, 8 of diamonds, 4 of spades, 3 of spades, 10 of hearts.
P4 Hand: Ace of spades, 8 of clubs, 6 of spades, Jack of spades, 3 of hearts.

NEVER have the same card in two different players. My code is here:

import java.util.Random;
import java.util.Arrays;
import java.util.ArrayList;

public class cardGame {
  public static void main(String[] args) {
    Random sad = new Random();
    ArrayList<String> all = new ArrayList<>(Arrays.asList("Ace of spades","2 of spades","3 of spades","4 of spades","5 of spades","6 of spades","7 of spades","8 of spades","9 of spades","10 of spades","Jack of spades","Queen of spades","King of spades","Ace of spades","2 of spades","3 of spades","4 of spades","5 of spades","6 of spades","7 of spades","8 of spades","9 of spades","10 of spades","Jack of spades","Queen of spades","King of spades","Ace of hearts","2 of hearts","3 of hearts","4 of hearts","5 of hearts","6 of hearts","7 of hearts","8 of hearts","9 of hearts","10 of hearts","Jack of hearts","Queen of hearts","King of hearts","Ace of diamonds","2 of diamonds","3 of diamonds","4 of diamonds","5 of diamonds","6 of diamonds","7 of diamonds","8 of diamonds","9 of diamonds","10 of diamonds","Jack of diamonds","Queen of diamonds","King of diamonds","Ace of clubs","2 of clubs","3 of clubs","4 of clubs","5 of clubs","6 of clubs","7 of clubs","8 of clubs","9 of clubs","10 of clubs","Jack of clubs","Queen of clubs","King of clubs"));
    ArrayList<String> trump = new ArrayList<>(Arrays.asList("spades","hearts","diamonds","clubs"));
    System.out.println("Boure hands and trump game.");
    System.out.println("");
    System.out.println("Trump: " + trump.remove(sad.nextInt(trump.size())) + ".");
    System.out.println("");
    System.out.println("P1 Hand: " + all.remove(sad.nextInt(all.size())) + ", " + all.remove(sad.nextInt(all.size())) + ", " + all.remove(sad.nextInt(all.size())) + ", " + all.remove(sad.nextInt(all.size())) + ", " + all.remove(sad.nextInt(all.size())) + ".");
    System.out.println("");
    System.out.println("P2 Hand: " + all.remove(sad.nextInt(all.size())) + ", " + all.remove(sad.nextInt(all.size())) + ", " + all.remove(sad.nextInt(all.size())) + ", " + all.remove(sad.nextInt(all.size())) + ", " + all.remove(sad.nextInt(all.size())) + ".");
    System.out.println("");
    System.out.println("P3 Hand: " + all.remove(sad.nextInt(all.size())) + ", " + all.remove(sad.nextInt(all.size())) + ", " + all.remove(sad.nextInt(all.size())) + ", " + all.remove(sad.nextInt(all.size())) + ", " + all.remove(sad.nextInt(all.size())) + ".");
    System.out.println("");
    System.out.println("P4 Hand: " + all.remove(sad.nextInt(all.size())) + ", " + all.remove(sad.nextInt(all.size())) + ", " + all.remove(sad.nextInt(all.size())) + ", " + all.remove(sad.nextInt(all.size())) + ", " + all.remove(sad.nextInt(all.size())) + ".");
  }
}

BTW the duplicate didn't help in one way: It didn't include arrays, which is what I'm dealing with. With that stated, I couldn't look at the question and figure out where I can change.

  • First read: [Polymorphism: Why use “List list = new ArrayList” instead of “ArrayList list = new ArrayList”?](https://stackoverflow.com/questions/9852831/polymorphism-why-use-list-list-new-arraylist-instead-of-arraylist-list-n) – Blasanka Aug 07 '17 at 04:07
  • @Blasanka what is that supposed to do, I'm looking for an error to fix. BTW: I checked out the question, no help there. –  Aug 07 '17 at 04:08
  • What error are you actually getting? – OneCricketeer Aug 07 '17 at 04:09
  • @cricket_007 it's not an error, I'm just getting two of the same cards often. –  Aug 07 '17 at 04:10
  • 2
    What about [`Collections.shuffle()`](http://docs.oracle.com/javase/6/docs/api/java/util/Collections.html#shuffle(java.util.List,%20java.util.Random)) to randomize cards for players – Blasanka Aug 07 '17 at 04:15
  • @BlazeDaBlur either 1. keep removing items from the "all" list as you draw. 2. Use a Set to track the indexes already used. – gvmani Aug 07 '17 at 04:16
  • @Blasanka pls explain, the shuffle website doesnt exactly help –  Aug 07 '17 at 04:25
  • Im not explaining because your intension is unclear to me. What is the output that you get(if you have provided an output that you expected) – Blasanka Aug 07 '17 at 04:26
  • How is my intention unclear, and my output is right there. Just some times I get something like, I dont know, every player gets a king of clubs –  Aug 07 '17 at 04:28
  • Can you please edit the question title to make it less vague? Please remember, that's the only thing people's see when they are on the front page or search engines – OneCricketeer Aug 07 '17 at 06:23
  • Possible duplicate of [Deck of cards JAVA](https://stackoverflow.com/questions/15942050/deck-of-cards-java) – Tezra Aug 07 '17 at 13:56

1 Answers1

2

Start off with a Deck of cards, and then assign the cards to each Player by removing them from the Deck and adding them to the Player's Collection which models the Player's Hand.

That way the player can only get the cards from the deck, and you can initialize the deck with one of each appropriate card.

Edwin Buck
  • 69,361
  • 7
  • 100
  • 138
  • Thank you, I'm trying it out now. –  Aug 07 '17 at 04:11
  • I just need a few pointers, 1. using arrays or what, 2. how to delete and add, and 3. Does each player get a `String[] p1 = {}` or what –  Aug 07 '17 at 04:37
  • @BlazeDaBlur I would use a Collection instead of an Array. Preferably a Set, because the order of the Cards in one's Hand is not important, it is only important if they are present. – Edwin Buck Aug 07 '17 at 04:39