import java.util.Scanner; import java.util.*; public class Game { ArrayList humanHand; ArrayList computerHand; Scanner in; public Game() { humanHand = new ArrayList(); computerHand = new ArrayList(); in = new Scanner(System.in); Deck deck = new Deck(); // Creates a full deck of cards excluding the Queen of Hearts deck.shuffle(); // Shuffle the deck dealCards(deck.getCards()); // Deal the human and the computer their initial hands // Uncomment the following two statements to see the initial hands dealt // System.out.println(" The computer's initial hand is: " + computerHand); // System.out.println(" The humans's initial hand is: " + humanHand); } public void waitForPlayer() { System.out.println("Press enter to continue!"); in.nextLine(); // We don't care about the output of nextLine; this is used merely to wait for a // return key press. } public void dealCards(ArrayList cards) { int i = 0; while (i < cards.size() - 1) { humanHand.add(cards.get(i++)); computerHand.add(cards.get(i++)); } if (i == cards.size() - 1) humanHand.add(cards.get(i)); // If there is an odd number of cards, //deal the last card to the human player } public int getValidInput(int n) { int position = 0; // Uncomment the following statement to see the computer's hand in each round, that is, cheat ;) // System.out.println(" The computer's hand is: " + computerHand); System.out.println("I have " + n + " cards. If 1 stands for my first card and " + n + " for my last card, which of my cards would you like?"); System.out.print("Give me an integer between 1 and " + n + ": "); position = Integer.parseInt(in.nextLine()); while (!(position >= 1 && position <= n)) { System.out.print("Invalid number. Please enter integer between 1 and " + n + ": "); position = Integer.parseInt(in.nextLine()); } return position; } public static void sortCards(ArrayList sequence) { if (sequence == null) return; for (int i = 0; i < sequence.size() - 1; i++) { int indexMin = i; for (int j = i + 1; j < sequence.size(); j++) { if (sequence.get(j).compareTo(sequence.get(indexMin)) < 0) indexMin = j; } Collections.swap(sequence, i, indexMin); } } /* * IMPORTANT: When asked to perform a sort, shuffle (or the like) in a lab, * assignment, mid-term or the final exam, you are supposed to provide your own * implementation, similar to what Q4 in part1 and this class (Game) in part2 do * for sort. Unless you have been specifically instructed otherwise, you are NOT * allowed to use methods such as Collections.shuffle(), like the method below * does for simplicity. */ public static ArrayList removePairs(ArrayList currentHand) { if (currentHand == null) return null; ArrayList newHand = new ArrayList<>(); int i = 0; sortCards(currentHand); // Uncomment the following statement to see the sort results in each round //System.out.println(" Sort result: " + currentHand); while (i < currentHand.size() - 1) { if (currentHand.get(i).getRank() == currentHand.get(i + 1).getRank()) i = i + 2; else newHand.add(currentHand.get(i++)); } if (i == currentHand.size() - 1) // If this condition is met, it means that the last card has not been paired up // within anything else and has to be added to the new hand. newHand.add(currentHand.get(i)); Collections.shuffle(newHand); // Shuffle the new hand before returning it. return newHand; } static final String[] englishOrdinalsEnd = { "st", "nd", "rd", "th" }; public static String getOrdinalsEnd(int position) { if (position > 3) return englishOrdinalsEnd[3]; else return englishOrdinalsEnd[position - 1]; } public void play() { System.out.println("Hello. My name is Robot and I am the dealer."); System.out.println("Welcome to my card game!"); System.out.println("Your current hand is:"); System.out.println(humanHand); System.out.println("Do not worry. I cannot see the order of your cards."); System.out.println("Now discard all the pairs from your deck. I will do the same."); waitForPlayer(); humanHand = removePairs(humanHand); computerHand = removePairs(computerHand); boolean isHumansTurn = true; while (humanHand.size() > 0 && computerHand.size() > 0) { if (isHumansTurn) { // The computer offers her cards. System.out.println("***********************************************************"); System.out.println("Your turn"); System.out.println("Your current hand is: "); System.out.println(humanHand); int position = getValidInput(computerHand.size()); Card card = computerHand.remove(position - 1); System.out.println("You asked for my " + position + getOrdinalsEnd(position) + " card."); System.out.println("Here it is: " + card); humanHand.add(card); System.out.println("With " + card + " added, your current hand is:"); System.out.println(humanHand); System.out.println("And after discarding pairs and shuffling, your hand is:"); humanHand = removePairs(humanHand); System.out.println(humanHand); waitForPlayer(); isHumansTurn = false; } else { // It is the computer's turn. System.out.println("***********************************************************"); System.out.println("My turn"); Random randomGenerator = new Random(); int position = randomGenerator.nextInt(humanHand.size()) + 1; Card card = humanHand.remove(position - 1); System.out.println("I took your " + position + getOrdinalsEnd(position) + " card."); System.out.println("Here it is: " + card); computerHand.add(card); computerHand = removePairs(computerHand); waitForPlayer(); isHumansTurn = true; } } if (computerHand.size() == 0) { System.out.println("I have no more cards!"); System.out.println("Sorry, you lost! I, the Robot, win!"); } else { System.out.println("***********************************************************"); System.out.println("You have no more cards!"); System.out.println("Congratulations! You, the Human, win!"); } } public static void main(String[] args) { Game game = new Game(); game.play(); } }