Question

In: Computer Science

USING JAVA... We want to play a simple card game with 2 players option. The deck...

USING JAVA...
We want to play a simple card game with 2 players option. The deck of cards contains 52 cards with 13 cards each in the 4 suits: clubs, diamonds, hearts and spades. Each player begins with 26 cards and one of the players starts the game by putting their first card on the table. Players take turns by putting the top card from their hand, until the current card placed on the table matches the suit of the previous card on the table. If a match happens, the player whose card matched, gets all the cards currently on the table and adds them at the end of the cards currently in his or her hand. Game continues until one player gets all the 52 cards, or for 10 rounds.
Construct the game using the following guidelines:

!!JAVA COLLECTIONS IS NOT ALLOWED!!
1. Create a method to deal the deck of cards so that each player gets 26 random cards
2. Start the game by choosing either of the player randomly
3. Show the cards on the table and in the hand of each player at each step of the game
4. Continue the game for 10 rounds or until one player has all the cards, whichever happens first
5. Declare the winner (the player with all the cards, or with more cards after 10 rounds), or say its a tie! (when both players have equal number of cards after 10 rounds)

Solutions

Expert Solution

import java.util.*; // Import Random

class Node {

Object data;

Node next;

public Node(Object data) {

this.data = data;

this.next = null;

}

}

class LL{

private Node head = new Node(-1);

public int size = 0;

public void add(Object item) {

addL(item, getNode(size));

}

public void add(Object item, int index) {

if (index < 0 || index > size) {

return;

}

if (index == 0) {

addF(item);

}

else {

addL(item, getNode(index - 1));

}

}

public void addF(Object item) {

Node first = new Node(item); // Create new first Node

first.next = head.next;

head.next = first;

size++;

}

public void addL(Object item, Node target) {

Node after = new Node(item);

after.next = target.next;

target.next = after;

size++;

}

public Object deleteFirst() {

if (size > 0) {

Node first = head.next;

head.next = first.next;

size--;

return first.data;

}

return null;

}

public Object deleteAfter(Node target) {

if (target.next != null) {

Node after = target.next;

target.next = after.next;

size--;

return after.data;

}

return null;

}

public Node getNode(int index) {

Node node = head;

for (int i = 0; i < index && node != null; i++) {

node = node.next;

}

return node;

}

public void printList(Node head) {

Node dumpp = head.next;

while (dumpp != null) {

System.out.print(dumpp.data.toString() + " ");

dumpp = dumpp.next;

}

}

}

class Hand {

private LL cards; // Cards in hand

public Hand() {

cards = new LL(); // A singly linked list of cards

}

public void addCard(Card c) {

cards.add(c);

}

public Card playCard() {

Card cardToPlay = (Card) cards.deleteFirst();

return cardToPlay;

}

public int getSize() {

return cards.size;

}

public void display() {

String[][] dumppArray = new String[13][4];

int k = 1;

for (int i = 0; i < 13; i++) {

for (int j = 0; j < 4; j++) {

if (cards.getNode(k) != null) {

dumppArray[i][j] = cards.getNode(k).data.toString();

k++;

}

}

}

for (int i = 0; i < 13; i++) {

if (dumppArray[i][0] != null) {

for (int j = 0; j < 4; j++) {

if (dumppArray[i][j] != null) {

System.out.print(String.format("%3s", dumppArray[i][j]));

System.out.print(" ");

}

}

System.out.println();

}

}

}

}

class Deck {

private Card deck[];

private int next;

public Deck(boolean startShuffled) {

deck = new Card[53];

for (int pos = 1; pos <= 13; pos++) {

deck[pos] = new Card(1, pos); // First suit, ex: 3 of clubs

deck[pos+13] = new Card(2, pos); // Second suit, diamonds

deck[pos+26] = new Card(3, pos); // Third suit, hearts

deck[pos+39] = new Card(4, pos); // Fourth suit, spades

}

next = 1; // Set next to 1 since first card is in index 1

if (startShuffled == true) {

shuffle();

}

}

public void shuffle() {

Random randomNumber = new Random();

for (int card = 1; card <= 52; card++) {

int rand = randomNumber.nextInt(52) + 1;

Card dumpp = deck[card]; // Card from r

deck[card] = deck[rand];

deck[rand] = dumpp;

}

next = 1; // Reset next

}

public Card deal() { // Deals one card at a time

if (next > 53) // If deck is depleted

shuffle();

Card c = deck[next];

next++;

return c;

}

}

class Card {

private int suit; // 1 Clubs, 2 Diamonds, 3 Hearts, 4 Spades

private int value; // 1 Ace... 11 J, 12 Q, 13 K

// Constructors

public Card() { // Default constructor, default card is Ace of Spades

this.suit = 1;

this.value = 1;

}

public Card(int suit, int value) { // Construct a speciifc card

this.suit = suit;

this.value = value;

}

public int getSuit() {

return this.suit;

}

public int getValue() {

return this.value;

}

public void setSuit(int suit) {

this.suit = suit;

}

public void setValue(int value) {

this.value = value;

}

public String getName() {

String name = "";

if (this.value == 1)

name = "A";

else if (value == 11)

name = "J";

else if (value == 12)

name = "Q";

else if (value == 13)

name = "K";

else // For cards 2 through 10

name = Integer.toString(value);

if (suit == 1)

name += (char)'\u2663';

else if (suit == 2)

name += (char)'\u2666';

else if (suit == 3)

name += (char)'\u2764';

else if (suit == 4)

name += (char)'\u2660';

return name;

}

public String toString() {

return getName();

}

}

class Player {

private Hand hand;

private String name;

public Player(String name) {

hand = new Hand(); // Instantiate new hand object

this.name = name;

}

public Card playCard() {

Card c = hand.playCard();

System.out.println(String.format("%5s", name) + " plays a " + c.getName() + "!");

return c;

}

public void takeCard(Card card) {

hand.addCard(card);

}

public String getName() {

return name;

}

public void displayHand() {

System.out.println(name + "\'s hand (" + hand.getSize() + "):");

hand.display();

System.out.println();

}

public int sizeHand() {

return hand.getSize();

}

}

class Main {

private static Player P1 = new Player("Dick");

private static Player P2 = new Player("Mike");

private static Player curr = P1;

private static Deck deck = new Deck(true);

private static ArrayList<Card> table = new ArrayList<>();

private static Card topCard;

private static int roundsPlayed = 1;

private static boolean gameEnd = false;

public static void main(String[] args) {

startG();

}

public static void startG() {

System.out.println("Starting simple card game simulation...");

System.out.println();

dealCards(); // Deal 26 cards to each player

chooseFirstPlayer(); // Choose who goes first

playRounds(); // Start the rounds

declareWinner(); // Declare a winner

}

public static void dealCards() {

for (int i = 0; i < 26; i++) {

P1.takeCard(deck.deal());

P2.takeCard(deck.deal());

}

}

public static void chooseFirstPlayer() {

Random random = new Random();

int n = random.nextInt(2);

if (n == 1) { // Make P2 the new P1

Player dumpp = P1;

P1 = P2;

P2 = dumpp;

}

}

public static void playRounds() {

while (roundsPlayed <= 10 && (gameEnd == false)) {

System.out.println("ROUND " + roundsPlayed);

System.out.println();

displayHands();

playRound();

roundsPlayed++;

}

}

public static void playRound() {

boolean suitMatch = false;

Card cardToPlay;

if ((P1.sizeHand() == 52) || (P2.sizeHand() == 52)) {

gameEnd = true;

}

while (suitMatch == false) {

cardToPlay = curr.playCard();

table.add(cardToPlay);

suitMatch = checkSuitMatch();

if (suitMatch == false)

switchP();

}

collectCards();

System.out.println();

try {

Thread.sleep(500);

}

catch (InterruptedException e) {

}

}

public static void switchP() {

if (curr == P1)

curr = P2;

else if (curr == P2)

curr = P1;

}

public static boolean checkSuitMatch() {

int tableSize = table.size();

int lastSuit;

int topSuit;

if (tableSize < 2) {

return false;

}

else {

lastSuit = table.get(tableSize - 1).getSuit();

topSuit = table.get(tableSize - 2).getSuit();

}

if (lastSuit == topSuit) {

System.out.println();

System.out.println(curr.getName() + " wins the round!");

System.out.println();

return true;

}

return false;

}

public static void collectCards() {

System.out.print(curr.getName() + " takes the table (" +

table.size() + "): ");

displayTable();

for (int i = 0; i < table.size(); i++) {

Card cTT= table.get(i);

curr.takeCard(cTT);

}

table.clear();

}

public static void displayTable() {

for (int i = 0; i < table.size(); i++) {

if (table.get(i) != null) {

System.out.print(table.get(i).getName() + " ");

}

}

System.out.println();

System.out.println();

}

public static void displayHands() {

P1.displayHand();

P2.displayHand();

}

public static void declareWinner() {

if (P1.sizeHand() > P2.sizeHand()) {

System.out.println(P1.getName().toUpperCase() + " WINS " +

"WITH A TOTAL OF " + P1.sizeHand() + " CARDS!");

}

else if (P2.sizeHand() > P1.sizeHand()) {

System.out.println(P2.getName().toUpperCase() + " WINS " +

"WITH A TOTAL OF " + P2.sizeHand() + " CARDS!");

}

else {

System.out.println("TIE! WOW IT'S SUPER RARE!");

}

System.out.println();

}

}

OUTPUT:

IF YOU HAVE ANY QUERY PLEASE COMMENT DOWN BELOW

PLEASE GIVE A THUMBS UP


Related Solutions

Create a java program. War is a card game for two players. A standard deck of...
Create a java program. War is a card game for two players. A standard deck of 52 cards is dealt so that both players have 26 cards. During each round of play (or "battle"), both players play a card from the top of their hand face up. The player who plays the card of the higher rank wins both cards and places them at the bottom of his stack of cards. If both cards played are of the same rank,...
We are creating a new card game with a new deck. Unlike the normal deck that...
We are creating a new card game with a new deck. Unlike the normal deck that has 13 ranks (Ace through King) and 4 Suits (hearts, diamonds, spades, and clubs), our deck will be made up of the following. Each card will have: i) One rank from 1 to 15. ii) One of 5 different suits. Hence, there are 75 cards in the deck with 15 ranks for each of the 5 different suits, and none of the cards will...
We are creating a new card game with a new deck. Unlike the normal deck that...
We are creating a new card game with a new deck. Unlike the normal deck that has 13 ranks (Ace through King) and 4 Suits (hearts, diamonds, spades, and clubs), our deck will be made up of the following. Each card will have: i) One rank from 1 to 15. ii) One of 5 different suits. Hence, there are 75 cards in the deck with 15 ranks for each of the 5 different suits, and none of the cards will...
We are creating a new card game with a new deck. Unlike the normal deck that...
We are creating a new card game with a new deck. Unlike the normal deck that has 13 ranks (Ace through King) and 4 Suits (hearts, diamonds, spades, and clubs), our deck will be made up of the following. Each card will have: i) One rank from 1 to 16. ii) One of 5 different suits. Hence, there are 80 cards in the deck with 16 ranks for each of the 5 different suits, and none of the cards will...
1) We are creating a new card game with a new deck. Unlike the normal deck...
1) We are creating a new card game with a new deck. Unlike the normal deck that has 13 ranks (Ace through King) and 4 Suits (hearts, diamonds, spades, and clubs), our deck will be made up of the following. Each card will have: i) One rank from 1 to 10. ii) One of 9 different suits. Hence, there are 90 cards in the deck with 10 ranks for each of the 9 different suits, and none of the cards...
1) We are creating a new card game with a new deck. Unlike the normal deck...
1) We are creating a new card game with a new deck. Unlike the normal deck that has 13 ranks (Ace through King) and 4 Suits (hearts, diamonds, spades, and clubs), our deck will be made up of the following. Each card will have: i) One rank from 1 to 16. ii) One of 5 different suits. Hence, there are 80 cards in the deck with 16 ranks for each of the 5 different suits, and none of the cards...
6. You are playing a card game with a friend. You are using a new deck...
6. You are playing a card game with a friend. You are using a new deck of 52 playing cards and you’d like to calculate some probabilities to improve your game. (Remember, the total number of cards decreases by 1 every time you draw a card!) a. What is the probability of drawing three queen cards in a row? b. What is the probability of drawing all four aces in a row? c. What is the probability of drawing the...
6. You are playing a card game with a friend. You are using a new deck...
6. You are playing a card game with a friend. You are using a new deck of 52 playing cards and you’d like to calculate some probabilities to improve your game. (Remember, the total number of cards decreases by 1 every time you draw a card!) a. What is the probability of drawing three queen cards in a row? b. What is the probability of drawing all four aces in a row? c. What is the probability of drawing the...
C++ Make a Tic Tac Toe game for 2 players to play using 2D arrays and...
C++ Make a Tic Tac Toe game for 2 players to play using 2D arrays and classes. Do not add more #include functions other than the ones listed. I never said what type of code I needed in a previous question. I apologize and I can't go back and change it so here is the same question with more information Using the tictactoeGame class, write a main program that uses a tictactoeGame to implement a game in which two players...
Make a Tic Tac Toe game for 2 players to play using 2D arrays and classes....
Make a Tic Tac Toe game for 2 players to play using 2D arrays and classes. Do not add more #include functions other than the ones listed (such as #include <stdio.h> etc). Using the tictactoeGame class, write a main program that uses a tictactoeGame to implement a game in which two players (you and a friend) take turns placing X’s and O’s onto the board. After each turn, the current board configuration should be displayed, and once a player connects...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT