SHARE
TWEET

Untitled

a guest Nov 19th, 2019 84 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.ArrayList;
  2. import java.util.Scanner;
  3.  
  4. public class War {
  5.     private Deck deck;
  6.     private Hand playerOne;
  7.     private Hand playerTwo;
  8.  
  9.     public War() {
  10.         System.out.println("==================================================");
  11.         System.out.println("Opening a fresh pack of cards.");
  12.        
  13.         // Create a new Deck instance and assign it to the instance variable field.
  14.         Deck deck = new Deck();
  15.  
  16.         System.out.println(deck);
  17.         System.out.println("");
  18.  
  19.         System.out.println("Shuffling the deck.");
  20.        
  21.         // Shuffle the deck
  22.         deck.shuffle();
  23.         System.out.println(deck);
  24.         System.out.println("");
  25.  
  26.         System.out.println("Deal the cards to two players.");
  27.         // instantiate playerOne and playerTwo as new Hand's
  28.         Hand playerOne = new Hand();
  29.         Hand playerTwo = new Hand();
  30.        
  31.         // deal Card's, one a time, to each players hand. Order matters!
  32.         // Use the Deck's dealCard() method and Hand's addCard() method.
  33.         // dealCard() returns null when there are no more cards left.
  34.         for( int i = 0; i < 52; i++){
  35.             if( i % 2 == 1 ){
  36.                 playerOne.addCard(deck.dealCard());
  37.             }else{
  38.                 playerTwo.addCard(deck.dealCard());
  39.             }
  40.         }
  41.  
  42.         System.out.print("Player One:\n\t");
  43.         System.out.println(playerOne);
  44.         System.out.print("Player Two:\n\t");
  45.         System.out.println(playerTwo);
  46.         System.out.println("==================================================");
  47.     }
  48.  
  49.     private void play() {
  50.         /*
  51.             In this method we will play the game of WAR!
  52.             Logically, we need to do these things
  53.             1. Check if the game is over
  54.             2. Print a menu to the user (Enable auto play, play a round, quit)
  55.             * Note, the menu will not be displayed once auto play is enabled
  56.             3. Play a card from each players hand and add it to a stack.
  57.             4.1 The player with the highest card wins the round and is awarded the stack
  58.             4.2 If there was a draw, each player adds three more cards to the stack and the round is over.
  59.             * The stack can potentially be collected on the next Round
  60.             5. Repeat until the game is over or the user quits from the menu
  61.         */
  62.  
  63.         int menuOption = '0';
  64.         boolean autoPlay = false;
  65.         Scanner scan = new Scanner(System.in);
  66.         ArrayList<Card> stack = new ArrayList<>();
  67.         int round = 1;
  68.  
  69.         do {
  70.             // Check if the game is over (The game is over when one of the players Hands are empty)
  71.             // If the game is over, print the winning player and return from the method (just a return statement)
  72.             if( playerTwo.totalInHand() == 0 ){
  73.                 System.out.println( "Player one wins!");
  74.             }else if( playerOne.totalInHand() == 0){
  75.                 System.out.println( "Player two wins!");
  76.             }else{
  77.                 System.out.println( "Menu: \n1 - Enable auto play. \n2 - Play a round. \n3 - Quit the game.");
  78.                 menuOption = scan.nextInt();
  79.                 switch(menuOption) {
  80.                     case 1:
  81.                         autoPlay = true;
  82.                         break;
  83.                     case 2:
  84.                         System.out.println( "Playing a round.");
  85.                         break;
  86.                     case 3:
  87.                         menuOption = 3;
  88.                         break;
  89.                     default:
  90.                         System.out.println("Choice is not a option.");
  91.                 }
  92.             }
  93.  
  94.            
  95.  
  96.             // If autoPlay is false, display a menu
  97.             // Menu:
  98.             //  1 - Enable auto play.
  99.             //  2 - Play a round.
  100.             //  3 - Quit the game.
  101.             // Use nextInt() from a scanner to get the menu option
  102.             // Use a switch statement to perform the users choice
  103.             /*
  104.                 On option 1, set autoPlay to true
  105.                 On option 2, print "Playing a round."
  106.                 On option 3, return from the method
  107.             */
  108.  
  109.  
  110.             System.out.println("================== Round " + round + " ======================");
  111.             // Display the number of cards player one has
  112.             // Display the number of cards player two has
  113.             System.out.println(playerOne.size());
  114.             System.out.println(playerTwo.size());
  115.  
  116.  
  117.             // Have each player draw a Card.
  118.             // Make sure to save the Card returned from the players Hand into a Card reference
  119.             // Card playerOnesCard = ... ?
  120.             // Add both cards to the stack
  121.             Card playerOnesCard = playerOne.playCard();
  122.             Card playerTwosCard = playerTwo.playCard();
  123.  
  124.             stack.add(playerOnesCard);
  125.             stack.add(playerTwosCard);
  126.  
  127.             // Display what Card each player played from their hand. (use the short name)
  128.             System.out.println(playerOnesCard.getShortName());
  129.             System.out.println(playerTwosCard.getShortName());
  130.  
  131.             // To compare cards, we need the rank index from each card
  132.             // int playerOnesCardRankIndex = ... ?
  133.  
  134.            
  135.             // Compare the cards
  136.             /* If they are equal
  137.                 print WAR!
  138.                 Each player adds three cards to the stack
  139.             */
  140.             /* If player ones card is greater
  141.                 For each card in the stack
  142.                 remove it and add it to player ones hand.
  143.             */
  144.             /* If player twos card is greater
  145.                 For each card in the stack
  146.                 remove it and add it to player twos hand.
  147.             */
  148.        
  149.  
  150.             System.out.println("================== End " + round + " ========================");
  151.  
  152.             round++;
  153.         } while (menuOption != 3);
  154.  
  155.         scan.close();
  156.     }
  157.  
  158.     public static void main(String[] args) {
  159.         System.out.println("WAR!");
  160.         System.out.println("");
  161.  
  162.         War war = new War();
  163.         war.play();
  164.  
  165.         System.out.println("");
  166.         System.out.println("PEACE?");
  167.     }
  168. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top