Question

In: Computer Science

Program a simple game that shuffles a deck of 52 cards and hands them out to...

Program a simple game that shuffles a deck of 52 cards and hands them out to twoplayers randomly (26 each). Write a simulation of the card game, where, at each turn, bothplayers take the card from the top of their pile and the player with the higher card wins thatround. The winner of that round takes both cards and adds it to a stack of won cards.In essence, there are four stacks. Each player has one stack of cards they are playingwith and one stack of cards they have won.There are 26 rounds, since each player has a full stack of cards.At the end of the game, the player with the most cards in their “won” pile wins.Note:●The deck of cards can simply hold an integer between 0 and 51. The higher numberwins. Don’t worry about implementing suits like spades, hearts, etc.To do:●In ArrayStack.java ○Implement push(), pop(), peek(), length(), isFull(), and toString() methods●In Game.java○Design and implement the game play logic.

This is the given code:

public class ArrayStack<T> {
   private final int DEFAULT_SIZE = 52;
   private T[] stack;
   private int numItems;

   public ArrayStack() {
       // The more intuitive way to do this might seem like:
       // container = T[defaultSize];
       // But we will get a T cannot be resolved to a type error
       // This means that we cannot actually create an object/instance of type T, since T is just a placeholder
       // Instead, we can take advantage of inheritance and create an object of type Object
       // And cast it to type T
       stack = (T[]) new Object[DEFAULT_SIZE];
       numItems = 0;
   }
  
   public void push(T item) {
       //TODO
   }
  
   public boolean isFull() {
       //TODO
   }
  
   public T pop() {
       //TODO
   }
  
   public T peek() {
       //TODO
   }
  
   public String toString() {
       //TODO
   }
  
   public int length() {
       //TODO
   }
}

-----------------------------------

import java.util.*;

public class Game {
   static ArrayList<Integer> cardsDeck = new ArrayList<>();
   static ArrayStack<Integer> cardStack = new ArrayStack<>();
   static ArrayStack<Integer> p1cards = new ArrayStack<>();
   static ArrayStack<Integer> p2cards = new ArrayStack<>();
   static ArrayStack<Integer> p1cardsWon = new ArrayStack<>();
   static ArrayStack<Integer> p2cardsWon = new ArrayStack<>();

   public static void main(String[] args) {
       initializeCards();
       shuffleCards();
      
       // Displays shuffled full deck of cards
       System.out.println("Original shuffled deck of cards: ");
       displayDeck();
      
       initializeCardStack();
      
       System.out.println("\n\nDealing 26 cards to player 1 and player 2: ");
       dealCards();
      
      
       System.out.println("\nPlayer 1 now has " + p1cards.length() + " cards.");
       System.out.println("Player 1 cards are: ");
       System.out.println(p1cards);
      
      
       System.out.println("Player 2 now has " + p2cards.length() + " cards.");
       System.out.println("Player 2 cards are: ");
       System.out.println(p2cards);
      
       // TODO: Game play logic:
       // TODO: As game progresses, the p1cards and p2cards stacks should get smaller
       // TODO: As game progresses, the p1cardsWon and p2CardsWon should get bigger, depending on who wins each round
       // TODO: When the game is over, display who won the game; in other words, is p1cardsWon or p2cardsWon bigger?
      
   }
  
   static void initializeCards() {
       // Deck of cards is filled with integers 0 through 51
       for (int i= 0; i<52; i++) {
           cardsDeck.add(i);
       }
   }
  
   static void shuffleCards() {
       // Shuffle the ArrayList holding the initial deck of cards
       Collections.shuffle(cardsDeck);
   }
  
   static void displayDeck() {
       // Simple method to display all items in deck of cards
       for (int card: cardsDeck) {
           System.out.print(card + " ");
       }
   }
  
   static void initializeCardStack() {
       // The inital ArrayList was used to facilitate shuffling, now we use the Stack ADT to implement the rest of our game
       for (int i= 0; i<52; i++) {
           cardStack.push(cardsDeck.get(i));
       }
   }
  
   static void dealCards() {
       // Deal cards to each player
       for (int i= 0; i<52; i++) {
           if (i%2 == 0) {
               p1cards.push(cardStack.pop());
           } else {
               p2cards.push(cardStack.pop());  
           }
       }
   }
  
}

Solutions

Expert Solution

import java.util.*;

class ArrayStack<T> {
        private final int DEFAULT_SIZE = 52;
        private T[] stack;
        private int numItems;

        public ArrayStack() {
                // The more intuitive way to do this might seem like:
                // container = T[defaultSize];
                // But we will get a T cannot be resolved to a type error
                // This means that we cannot actually create an object/instance of type T, since
                // T is just a placeholder
                // Instead, we can take advantage of inheritance and create an object of type
                // Object
                // And cast it to type T
                stack = (T[]) new Object[DEFAULT_SIZE];
                numItems = 0;
        }

        public void push(T item) {
                stack[numItems++] = item;
        }

        public boolean isFull() {
                return numItems == stack.length;
        }

        public T pop() {
                return stack[--numItems];
        }

        public T peek() {
                return stack[numItems];
        }

        public String toString() {
                String s = "";
                
                for(int i=0; i<numItems; i++) {
                        if(i != 0) {
                                s += ", ";
                        }
                        s += stack[i];
                }
                return s;
        }

        public int length() {
                return numItems;
        }
}

public class Game {
        static ArrayList<Integer> cardsDeck = new ArrayList<>();
        static ArrayStack<Integer> cardStack = new ArrayStack<>();
        static ArrayStack<Integer> p1cards = new ArrayStack<>();
        static ArrayStack<Integer> p2cards = new ArrayStack<>();
        static ArrayStack<Integer> p1cardsWon = new ArrayStack<>();
        static ArrayStack<Integer> p2cardsWon = new ArrayStack<>();

        public static void main(String[] args) {
                initializeCards();
                shuffleCards();

                // Displays shuffled full deck of cards
                System.out.println("Original shuffled deck of cards: ");
                displayDeck();

                initializeCardStack();

                System.out.println("\n\nDealing 26 cards to player 1 and player 2: ");
                dealCards();

                System.out.println("\nPlayer 1 now has " + p1cards.length() + " cards.");
                System.out.println("Player 1 cards are: ");
                System.out.println(p1cards);

                System.out.println("Player 2 now has " + p2cards.length() + " cards.");
                System.out.println("Player 2 cards are: ");
                System.out.println(p2cards);

                // TODO: Game play logic:
                // TODO: As game progresses, the p1cards and p2cards stacks should get smaller
                // TODO: As game progresses, the p1cardsWon and p2CardsWon should get bigger,
                // depending on who wins each round
                // TODO: When the game is over, display who won the game; in other words, is
                // p1cardsWon or p2cardsWon bigger?
                
                while(p1cards.length() != 0 && p2cards.length() != 2) {
                        int c1 = p1cards.pop();
                        int c2 = p2cards.pop();
                        
                        if(c1 > c2) {
                                p1cardsWon.push(c1);
                                p1cardsWon.push(c2);
                        } else {
                                p2cardsWon.push(c1);
                                p2cardsWon.push(c2);
                        }
                }
                
                if(p1cardsWon.length() > p2cardsWon.length()) {
                        System.out.println("Player 1 won.");
                } else {
                        System.out.println("Player 2 won.");
                }
        }

        static void initializeCards() {
                // Deck of cards is filled with integers 0 through 51
                for (int i = 0; i < 52; i++) {
                        cardsDeck.add(i);
                }
        }

        static void shuffleCards() {
                // Shuffle the ArrayList holding the initial deck of cards
                Collections.shuffle(cardsDeck);
        }

        static void displayDeck() {
                // Simple method to display all items in deck of cards
                for (int card : cardsDeck) {
                        System.out.print(card + " ");
                }
        }

        static void initializeCardStack() {
                // The inital ArrayList was used to facilitate shuffling, now we use the Stack
                // ADT to implement the rest of our game
                for (int i = 0; i < 52; i++) {
                        cardStack.push(cardsDeck.get(i));
                }
        }

        static void dealCards() {
                // Deal cards to each player
                for (int i = 0; i < 52; i++) {
                        if (i % 2 == 0) {
                                p1cards.push(cardStack.pop());
                        } else {
                                p2cards.push(cardStack.pop());
                        }
                }
        }

}
**************************************************

Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.

Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.


Related Solutions

Consider five-card-hands out of a deck of 52 cards. a) (10%) What is the probability that...
Consider five-card-hands out of a deck of 52 cards. a) (10%) What is the probability that a hand selected at random contains five cards of the same suit? (The suits are hearts, spades, diamonds and clubs.) Show a formula and compute a final answer. Show your intermediate computations. b) (10%) In how many different ways can a hand contain no more than 2 cards of the same kind. (E.g. not more than 2 queens.) Show a formula but you do...
Poker hands with ranking. Consider a regular deck of 52 cards as usual with 5 cards...
Poker hands with ranking. Consider a regular deck of 52 cards as usual with 5 cards dealt. You have learned all those 8 patterns: one pair, two pairs, three of a kind, straight, flush, full house, four of a kind, and straight flush (with royal flush a special kind of straight flush). Note ace is counted as both 1 point and 14 points. So in counting straight, A2345 and 10JQKA are both valid. Compute / derive the number of ways...
Poker hands with ranking. Consider a regular deck of 52 cards as usual with 5 cards...
Poker hands with ranking. Consider a regular deck of 52 cards as usual with 5 cards dealt. You have learned all those 8 patterns: one pair, two pairs, three of a kind, straight, flush, full house, four of a kind, and straight flush (with royal flush a special kind of straight flush). Note ace is counted as both 1 point and 14 points. So in counting straight, A2345 and 10JQKA are both valid. Compute / derive the number of ways...
consider a standard deck of 52 cards . determine the number od distinct SEVEN cards hands...
consider a standard deck of 52 cards . determine the number od distinct SEVEN cards hands which include a. no restruction b. only clubs c. 3clubs and 4 diamonds d.1 jack, 3 queen
1. In the game of poker, five cards from a standard deck of 52 cards are...
1. In the game of poker, five cards from a standard deck of 52 cards are dealt to each player. Assume there are four players and the cards are dealt five at a time around the table until all four players have received five cards. a. What is the probability of the first player receiving a royal flush (the ace, king, queen, jack, and 10 of the same suit). b. What is the probability of the second player receiving a...
Design a c++ program to simulate the BlackJack game. Rules: 1) single deck, 52 cards -both...
Design a c++ program to simulate the BlackJack game. Rules: 1) single deck, 52 cards -both player and dealer are taking cards off the same deck (the same 52 values). 2) display BOTH cards of the players, and ONE card of the computer. 3) Check for blackjack (starting value of 21 for computer or player) -if either side has a blackjack end of the game, next hand of blackjack 4) Computer must hit if 16 or below. 5) Computer must...
Write a program in c++ that picks four cards from a deck of 52 cards and...
Write a program in c++ that picks four cards from a deck of 52 cards and computes the sum of the four cards. An Ace, King, Queen, and Jack represent 1, 13, 12, and 11, respectively. Your program should display the number of picks that yields the sum of 24. You are not allowed to use arrays. Declare four variables to hold the card values.
How many different 4 card hands can be dealt from a deck of 52 cards? The...
How many different 4 card hands can be dealt from a deck of 52 cards? The order of the cards does not matter in this case.
1, Suppose you want to divide a 52 card deck into four hands with 13 cards...
1, Suppose you want to divide a 52 card deck into four hands with 13 cards each. What is the probability that each hand has a king? 2.   Suppose that X ∼ Bin(n, 0.5). Find the probability mass function of Y =2X, E(Y) , and D(Y).
a.       Assume a good deck is 25% of each suit (out of 52 cards, there are...
a.       Assume a good deck is 25% of each suit (out of 52 cards, there are 13 each of hearts, spades, diamonds, and clubs). Deal 4 cards. Did you get 25% of each suit? (You can just focus on one suit if you want; you will learn the same thing.) Why or why not? Basing your decision only on the sample, do you have good deck or not? please explain decision Put the 4 cards back and reshuffle. Repeat the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT