Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Scanner;
- public class War {
- private Deck deck;
- private Hand playerOne;
- private Hand playerTwo;
- public War() {
- System.out.println("==================================================");
- System.out.println("Opening a fresh pack of cards.");
- // Create a new Deck instance and assign it to the instance variable field.
- Deck deck = new Deck();
- System.out.println(deck);
- System.out.println("");
- System.out.println("Shuffling the deck.");
- // Shuffle the deck
- deck.shuffle();
- System.out.println(deck);
- System.out.println("");
- System.out.println("Deal the cards to two players.");
- // instantiate playerOne and playerTwo as new Hand's
- Hand playerOne = new Hand();
- Hand playerTwo = new Hand();
- // deal Card's, one a time, to each players hand. Order matters!
- // Use the Deck's dealCard() method and Hand's addCard() method.
- // dealCard() returns null when there are no more cards left.
- for( int i = 0; i < 52; i++){
- if( i % 2 == 1 ){
- playerOne.addCard(deck.dealCard());
- }else{
- playerTwo.addCard(deck.dealCard());
- }
- }
- System.out.print("Player One:\n\t");
- System.out.println(playerOne);
- System.out.print("Player Two:\n\t");
- System.out.println(playerTwo);
- System.out.println("==================================================");
- }
- private void play() {
- /*
- In this method we will play the game of WAR!
- Logically, we need to do these things
- 1. Check if the game is over
- 2. Print a menu to the user (Enable auto play, play a round, quit)
- * Note, the menu will not be displayed once auto play is enabled
- 3. Play a card from each players hand and add it to a stack.
- 4.1 The player with the highest card wins the round and is awarded the stack
- 4.2 If there was a draw, each player adds three more cards to the stack and the round is over.
- * The stack can potentially be collected on the next Round
- 5. Repeat until the game is over or the user quits from the menu
- */
- int menuOption = '0';
- boolean autoPlay = false;
- Scanner scan = new Scanner(System.in);
- ArrayList<Card> stack = new ArrayList<>();
- int round = 1;
- do {
- // Check if the game is over (The game is over when one of the players Hands are empty)
- // If the game is over, print the winning player and return from the method (just a return statement)
- if( playerTwo.totalInHand() == 0 ){
- System.out.println( "Player one wins!");
- }else if( playerOne.totalInHand() == 0){
- System.out.println( "Player two wins!");
- }else{
- System.out.println( "Menu: \n1 - Enable auto play. \n2 - Play a round. \n3 - Quit the game.");
- menuOption = scan.nextInt();
- switch(menuOption) {
- case 1:
- autoPlay = true;
- break;
- case 2:
- System.out.println( "Playing a round.");
- break;
- case 3:
- menuOption = 3;
- break;
- default:
- System.out.println("Choice is not a option.");
- }
- }
- // If autoPlay is false, display a menu
- // Menu:
- // 1 - Enable auto play.
- // 2 - Play a round.
- // 3 - Quit the game.
- // Use nextInt() from a scanner to get the menu option
- // Use a switch statement to perform the users choice
- /*
- On option 1, set autoPlay to true
- On option 2, print "Playing a round."
- On option 3, return from the method
- */
- System.out.println("================== Round " + round + " ======================");
- // Display the number of cards player one has
- // Display the number of cards player two has
- System.out.println(playerOne.size());
- System.out.println(playerTwo.size());
- // Have each player draw a Card.
- // Make sure to save the Card returned from the players Hand into a Card reference
- // Card playerOnesCard = ... ?
- // Add both cards to the stack
- Card playerOnesCard = playerOne.playCard();
- Card playerTwosCard = playerTwo.playCard();
- stack.add(playerOnesCard);
- stack.add(playerTwosCard);
- // Display what Card each player played from their hand. (use the short name)
- System.out.println(playerOnesCard.getShortName());
- System.out.println(playerTwosCard.getShortName());
- // To compare cards, we need the rank index from each card
- // int playerOnesCardRankIndex = ... ?
- // Compare the cards
- /* If they are equal
- print WAR!
- Each player adds three cards to the stack
- */
- /* If player ones card is greater
- For each card in the stack
- remove it and add it to player ones hand.
- */
- /* If player twos card is greater
- For each card in the stack
- remove it and add it to player twos hand.
- */
- System.out.println("================== End " + round + " ========================");
- round++;
- } while (menuOption != 3);
- scan.close();
- }
- public static void main(String[] args) {
- System.out.println("WAR!");
- System.out.println("");
- War war = new War();
- war.play();
- System.out.println("");
- System.out.println("PEACE?");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement