Advertisement
Guest User

Game

a guest
Feb 17th, 2020
857
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.76 KB | None | 0 0
  1.  
  2. import java.util.*;
  3.  
  4. public class Game {
  5.     static ArrayList<Integer> cardsDeck = new ArrayList<>();
  6.     static LinkedStack<Integer> cardStack = new LinkedStack<>();
  7.     static LinkedStack<Integer> p1cards = new LinkedStack<>();
  8.     static LinkedStack<Integer> p2cards = new LinkedStack<>();
  9.     static LinkedStack<Integer> p1cardsWon = new LinkedStack<>();
  10.     static LinkedStack<Integer> p2cardsWon = new LinkedStack<>();
  11.  
  12.     public static void main(String[] args) {
  13.         initializeCards();
  14.         shuffleCards();
  15.  
  16.         // Displays shuffled full deck of cards
  17.         System.out.println("Original shuffled deck of cards: ");
  18.         displayDeck();
  19.  
  20.         initializeCardStack();
  21.  
  22.         System.out.println("\n\nDealing 26 cards to player 1 and player 2: ");
  23.         dealCards();
  24.  
  25.  
  26.         System.out.println("\nPlayer 1 now has " + p1cards.length() + " cards.");
  27.         System.out.println("Player 1 cards are: ");
  28.         System.out.println(p1cards);
  29.  
  30.  
  31.         System.out.println("Player 2 now has " + p2cards.length() + " cards.");
  32.         System.out.println("Player 2 cards are: ");
  33.         System.out.println(p2cards + "\n");
  34.  
  35.         //begin a while loop that will run until either p1cards or p2cards reaches 0
  36.         while (p1cards.length() > 0|| p2cards.length() > 0) {
  37.             //display drawn cards for each round
  38.             System.out.println("Player 1 has drawn " + p1cards.peek());
  39.             System.out.println("Player 2 has drawn " + p2cards.peek() + "\n");
  40.  
  41.             //use an if statement to determine what stack the won cards are pushed into
  42.             //if player 1 wins
  43.             if (p1cards.peek() < p2cards.peek()) {
  44.                 p1cardsWon.push(p1cards.pop());
  45.                 p1cardsWon.push(p2cards.pop());
  46.                 //if player 2 wins
  47.             } else {
  48.                 p2cardsWon.push(p1cards.pop());
  49.                 p2cardsWon.push(p2cards.pop());
  50.             }
  51.  
  52.         }
  53.         //use an if statement to test the length of the stacks of won cards
  54.         //output the length of each stack and then determine the winner
  55.         if(p1cardsWon.length() > p2cardsWon.length()){
  56.             System.out.println("Player 1 has captured " + p1cardsWon.length() + " cards.");
  57.             System.out.println("Player 2 has captured " + p2cardsWon.length() + " cards.\n");
  58.             System.out.println("Player 1 has won the game!");
  59.  
  60.         }
  61.         else {
  62.             System.out.println("Player 1 has captured " + p1cardsWon.length() + " cards.");
  63.             System.out.println("Player 2 has captured " + p2cardsWon.length() + " cards.\n");
  64.             System.out.println("Player 2 has won the game!");
  65.         }
  66.     }
  67.  
  68.  
  69.     static void initializeCards() {
  70.         // Deck of cards is filled with integers 0 through 51
  71.         for (int i= 0; i<52; i++) {
  72.             cardsDeck.add(i);
  73.         }
  74.     }
  75.  
  76.     static void shuffleCards() {
  77.         // Shuffle the ArrayList holding the initial deck of cards
  78.         Collections.shuffle(cardsDeck);
  79.     }
  80.  
  81.     static void displayDeck() {
  82.         // Simple method to display all items in deck of cards
  83.         for (int card: cardsDeck) {
  84.             System.out.print(card + " ");
  85.         }
  86.     }
  87.  
  88.     static void initializeCardStack() {
  89.         // The initial ArrayList was used to facilitate shuffling, now we use the Stack ADT to implement the rest of our game
  90.         for (int i= 0; i<52; i++) {
  91.             cardStack.push(cardsDeck.get(i));
  92.         }
  93.     }
  94.  
  95.     static void dealCards() {
  96.         // Deal cards to each player
  97.         for (int i= 0; i<52; i++) {
  98.             if (i%2 == 0) {
  99.                 p1cards.push(cardStack.pop());
  100.             } else {
  101.                 p2cards.push(cardStack.pop());
  102.             }
  103.         }
  104.     }
  105.  
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement