import java.awt.Color; import breadboard.toys.Card; import breadboard.toys.CollectionOfCards; import breadboard.toys.Deck; import breadboards.Breadboard; import breadboards.GCompound; import breadboards.GImage; public class War extends Breadboard { // Use the following constants and variables as appropriate // in the designated places below: final int BETTER = 0; // the 3 possible outputs for the compare() method below final int WORSE = 1; final int SAME = 2; CollectionOfCards hand1; // hand for player 1 CollectionOfCards hand2; // hand for player 2 int x1 = 50; // x-coordinate for left side of player1's cards int x2 = 150; // x-coordinate for left side of player2's cards int yFirst = 50; // y-coordinate for top edge of first cards shown int y; // y-coordinate for top edge of next card to be shown int yStep = 17; // pairs of cards drawn in a "war" should be shown successively lower, // this tells you by how much lower each pair should sink each time public CollectionOfCards addToBottom(Card card, CollectionOfCards collection) { // adds a card to the bottom (position 0) of the given collection of cards // note: the provided collection of cards methods only allow for the // addition of a card to the top of the collection -- so you have to be sneaky // think recursively! ////////////////////// // INSERT CODE HERE // ////////////////////// } public void give(CollectionOfCards spoils, CollectionOfCards hand) { // adds all the cards in spoils to the botton of hand ////////////////////// // INSERT CODE HERE // ////////////////////// } public void declareWinner(String player) { if (this.getTimer().isRunning()) this.getTimer().stop(); this.getTextField().setText(player + " wins!"); this.getButton1().setEnabled(false); } public void makeAllFaceUp(CollectionOfCards collection) { // you might find having this utility method useful // it is meant to make all cards in the collection face up ////////////////////// // INSERT CODE HERE // ////////////////////// } public int compare(Card c1, Card c2) { // determines if card c1 is BETTER, SAME, or WORSE than card c2 by returning // one of the similarly-named constants defined earlier at the top of this file // note, for a given card, the card.rank() method returns values consistent with // Ace = 1, 2, 3, ..., 10, Jack = 10, Queen = 11, King = 12 // In the game war however, Aces should beat Kings, so you will want to // treat Aces as if they had rank 14 instead of 1, so that the higher "war rank" wins ////////////////////// // INSERT CODE HERE // ////////////////////// } public void onButton1Click() { // draw! // clear the table -- i.e., remove all cards (actually, their images) from the canvas this.removeAll(); // the cards that will be "won" should be put into the spoils collection CollectionOfCards spoils = new CollectionOfCards(); /////////////////////////////////////////////////////////// // INSERT ANY OTHER NEEDED INITIALIZATION OF THINGS HERE // /////////////////////////////////////////////////////////// // Each player draws the top card from their hand and places it face-up // on the felt (of course, if your hand is empty, you immediately lose) // both cards are added to the "spoils" (collectionOfCards) and then // the cards showing are compared. The player with the better card wins the spoils // If however, the cards have the same rank, the players "war": // // (*) For each player, 3 more cards (from the top of their hands) should // be placed (face-down) on the green felt, so that consecutive cards // placed for each player are slightly lower (by yStep, each time) // // a 4th card for each player should be placed on the felt, // also lower, but this time face up. // // if at any time, a player runs out of cards and can't put the requisite // cards on the felt, they immediately lose // // all cards on the felt are now the "spoils" to be won // the last cards placed on the felt are compared, the player with the // better card wins the spoils. If they have the same rank, war continues // That is to say, play goes back to (*) /////////////////////////////////////////////////// // INSERT CODE HERE TO ACCOMPLISH THE ABOVE HERE // /////////////////////////////////////////////////// // update the text area so you can keep track of who is winning or losing this.getTextArea().setText("Cards Left: " + "Player 1 has " + hand1.size() + " " + "Player 2 has " + hand2.size() + System.lineSeparator() + "(First player to run out of cards loses!)"); } public void onButton2Click() { // update text on this button to be "Manual" if previously "Auto", and vice-versa // start the timer in the case one is switching from manual to auto // stop the timer otherwise if (this.getButton2().getText().equals("Auto")) { this.getButton2().setText("Manual"); this.getTimer().start(); } else { this.getButton2().setText("Auto"); this.getTimer().stop(); } } public void onTimerTick() { // every time the timer ticks, button1 clicks itself! // the resulting behavior will be identical to that seen if // the user clicked the button -- the onButton1Click() method gets called this.getButton1().doClick(); } public War() { // setup breadboard this.setTitle("Let's Play War!"); this.getButton1().setText("Draw!"); this.getButton2().setText("Auto"); this.setSize(500,500); final Color POKER_FELT_GREEN = new Color(71,113,72); this.setBackground(POKER_FELT_GREEN); this.getTimer().setDelay(100); // create deck Deck deck = new Deck(); deck.shuffle(); // deal entire deck out to 2 hands hand1 = new CollectionOfCards(); hand2 = new CollectionOfCards(); int numberOfCardsDrawn = 0; while (deck.size() > 0) { Card card = deck.draw(); (++numberOfCardsDrawn % 2 == 0 ? hand1 : hand2).add(card); } // reset vertical position for next draw y = yFirst; } public static void main(String[] args) { War war = new War(); } }