'How do I separate items in an arraylist by character in Java?
I'm working on a game of Go Fish for a school assignment and I need to separate a group of 4 cards in order to keep score. Any help would be greatly appreciated
import java.util.*;
import java.io.*;
public class GoFish1 {
public static void main(final String[] args) {
String[] arr = {
"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> deck = new ArrayList<>(Arrays.asList(arr));
ArrayList<String> CPU = new ArrayList<String> ();
ArrayList<String> Player = new ArrayList<String> ();
int PlayerBook = 0;
int CPUbook = 0;
System.out.println("\nPlayer's cards:");
Random rand = new Random();
for (int i = 0; i<7 && deck.size() > 0; i++) {
int randomIndex = rand.nextInt(deck.size());
String card = deck.get(randomIndex);
Player.add(card);
System.out.println(card);
deck.remove(randomIndex);
}
System.out.println("\nCPU's cards:");
for (int i = 0; i<7 && deck.size() > 0; i++) {
int randomIndex = rand.nextInt(deck.size());
String card = deck.get(randomIndex);
CPU.add(card);
System.out.println(card);
deck.remove(randomIndex);
}
}
}
Solution 1:[1]
public static void main(String[] args) {
Stack<String> deck = new Stack();
deck.addAll(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 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"));
List <String> CPU = new ArrayList<>();
List <String> Player = new ArrayList<>();
int PlayerBook = 0;
int CPUbook = 0;
Collections.shuffle(deck);
for (int i = 0; i < 7; i++) {
Player.add(deck.pop());
CPU.add(deck.pop());
}
System.out.println("Players cards:");
Player.stream().forEach(System.out::println);
System.out.println("\nCPU cards:");
CPU.stream().forEach(System.out::println);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Ryan |