In: Computer Science
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());
}
}
}
}
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.