...................................
Printable View
...................................
Hello and welcome,
Most of this code is just initialising the cards list but here we go.
Code :final List<String> cards = new ArrayList<String>(); cards.add("Ace of Diamonds"); cards.add("One of Diamonds"); cards.add("Two of Diamonds"); cards.add("Three of Diamonds"); cards.add("Four of Diamonds"); cards.add("Five of Diamonds"); cards.add("Six of Diamonds"); cards.add("Seven of Diamonds"); cards.add("Eight of Diamonds"); cards.add("Nine of Diamonds"); cards.add("Ten of Diamonds"); cards.add("Knight of Diamonds"); cards.add("Queen of Diamonds"); cards.add("King of Diamonds"); cards.add("Ace of Spades"); cards.add("One of Spades"); cards.add("Two of Spades"); cards.add("Three of Spades"); cards.add("Four of Spades"); cards.add("Five of Spades"); cards.add("Six of Spades"); cards.add("Seven of Spades"); cards.add("Eight of Spades"); cards.add("Nine of Spades"); cards.add("Ten of Spades"); cards.add("Knight of Spades"); cards.add("Queen of Spades"); cards.add("King of Spades"); cards.add("Ace of Hearts"); cards.add("One of Hearts"); cards.add("Two of Hearts"); cards.add("Three of Hearts"); cards.add("Four of Hearts"); cards.add("Five of Hearts"); cards.add("Six of Hearts"); cards.add("Seven of Hearts"); cards.add("Eight of Hearts"); cards.add("Nine of Hearts"); cards.add("Ten of Hearts"); cards.add("Knight of Hearts"); cards.add("Queen of Hearts"); cards.add("King of Hearts"); cards.add("Ace of Clubs"); cards.add("One of Clubs"); cards.add("Two of Clubs"); cards.add("Three of Clubs"); cards.add("Four of Clubs"); cards.add("Five of Clubs"); cards.add("Six of Clubs"); cards.add("Seven of Clubs"); cards.add("Eight of Clubs"); cards.add("Nine of Clubs"); cards.add("Ten of Clubs"); cards.add("Knight of Clubs"); cards.add("Queen of Clubs"); cards.add("King of Clubs"); Collections.shuffle(cards); for (final String card : cards) { System.out.println(card); }
// Json
Rasla, there's a flaw in your shuffling algorithm. Firstly, you never update taken, so it is possible (and very likely) to have duplicate cards end up in the shuffled deck. Secondly, you're using an awful amount of space. A better sorting algorithm would be to keep swapping cards.
Code :public void shuffle(Card[] fulldeck) { Random gen = new Random(); for (int i = 0; i < fulldeck.length; i++) { int index = gen.nextInt(fulldeck.length); Card temp = fulldeck[index]; fulldeck[index] = fulldeck[i]; fulldeck[i] = temp; } }
ah i understand , thanks
thats much better