Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Would anyone be able to provide a solution in Java for this scenario? Thank you

ID: 3863963 • Letter: W

Question

Would anyone be able to provide a solution in Java for this scenario? Thank you in advance!

implement a simple card game, making use of class structure, object usage and data structures. The first round starts with all players having the same number of cards in their deck, which should not yet be visible to the players; each card has several attributes, with each attribute ranging in value from 0 up to 9. All players pick up a card from the top of their deck and view it. The nominated player then chooses an attribute which they wish to compare, say Attribute ‘B’ to “play with”; all the players compare the values of this Attribute ‘B’ on their cards and the player with the highest value wins the round and then collects the cards that the other players lost that round with and puts them (along with their own card) at the back of their deck.

The next round once again starts with each player picking up the card on top of their deck and looking at it, then winner of the previous round chooses the attribute everyone will play with for that specific round. The game continues in the same fashion until one player has all the cards, so say everyone had 5 cards and there were two players, the user and the computer then it would be played until one player accumulated all 10 cards

Explanation / Answer

hi, I have done this for 2 players using linkedlist implementations of Queue in Java. you can expand on it. Please find the code below with 3 different classes:

public class Card {

private String atrribute_1;
private String atrribute_2;
private String atrribute_3;

public Card(String atrribute_1, String atrribute_2, String atrribute_3) {
  super();
  this.atrribute_1 = atrribute_1;
  this.atrribute_2 = atrribute_2;
  this.atrribute_3 = atrribute_3;
}

public String getAtrribute_1() {
  return atrribute_1;
}

public void setAtrribute_1(String atrribute_1) {
  this.atrribute_1 = atrribute_1;
}

public String getAtrribute_2() {
  return atrribute_2;
}

public void setAtrribute_2(String atrribute_2) {
  this.atrribute_2 = atrribute_2;
}

public String getAtrribute_3() {
  return atrribute_3;
}

public void setAtrribute_3(String atrribute_3) {
  this.atrribute_3 = atrribute_3;
}

}

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

import java.util.LinkedList;

public class CardQueue<Card> {

private LinkedList<Card> list = new LinkedList<Card>();

public void enqueue(Card item) {
  list.addLast(item);
}

public void addToFront(Card item) {
  list.addFirst(item);
}

public Card dequeue() {
  return list.poll();
}

public boolean hasItems() {
  return !list.isEmpty();
}

public int size() {
  return list.size();
}

public void addItems(CardQueue<? extends Card> q) {
  while (q.hasItems())
   list.addLast(q.dequeue());
}
}

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

import java.util.Scanner;

import javax.smartcardio.ATR;

public class Game {

public static void main(String[] args) {
  Scanner s = new Scanner(System.in);
  String winnerFlag = "1"; // value will be 1 or 2 to indicate the winner
         // of turns
  // Assuming 2 players only
  CardQueue<Card> playerOneCardQueue = new CardQueue<>();
  CardQueue<Card> playerTwoCardQueue = new CardQueue<>();

  // Assuming each player has 5 cards each
  // We need to construct 10 different card object
  Card player1Card1 = new Card("9", "2", "9");
  Card player1Card2 = new Card("9", "4", "6");
  Card player1Card3 = new Card("9", "0", "9");
  Card player1Card4 = new Card("9", "2", "3");
  Card player1Card5 = new Card("9", "9", "9");
  Card player2Card1 = new Card("1", "2", "1");
  Card player2Card2 = new Card("8", "9", "9");
  Card player2Card3 = new Card("8", "8", "7");
  Card player2Card4 = new Card("7", "5", "6");
  Card player2Card5 = new Card("3", "6", "8");

  // Now assign cards to two players card Queue
  playerOneCardQueue.enqueue(player1Card1);
  playerOneCardQueue.enqueue(player1Card2);
  playerOneCardQueue.enqueue(player1Card3);
  playerOneCardQueue.enqueue(player1Card4);
  playerOneCardQueue.enqueue(player1Card5);

  playerTwoCardQueue.enqueue(player2Card1);
  playerTwoCardQueue.enqueue(player2Card2);
  playerTwoCardQueue.enqueue(player2Card3);
  playerTwoCardQueue.enqueue(player2Card4);
  playerTwoCardQueue.enqueue(player2Card5);

  // Now continue the game untill one player loses all its cards
  while (playerOneCardQueue.size() > 0 && playerTwoCardQueue.size() > 0) {
   // Suppose playerOne always starts the game
   // dequeue returns the card from top
   if ("1".equals(winnerFlag)) {
    Card playerOnePlayedCard = playerOneCardQueue.dequeue();
    System.out
      .println("Player, Which attribute you want to compare! ,1/2/3");
    String attributeNo = s.next();
    // Now player 2 plays his card
    Card playerTwoplayedCard = playerTwoCardQueue.dequeue();
    // Now decide the winner
    if ("1".equals(attributeNo)) {
     if (Integer.parseInt(playerOnePlayedCard.getAtrribute_1()) > Integer
       .parseInt(playerTwoplayedCard.getAtrribute_1())) {

      winnerFlag = "1";
      playerOneCardQueue.addToFront(playerTwoplayedCard);
      playerOneCardQueue.addToFront(playerOnePlayedCard);
      System.out.println("Player One wins this turn");
     } else {
      winnerFlag = "2";
      playerTwoCardQueue.addToFront(playerTwoplayedCard);
      playerTwoCardQueue.addToFront(playerOnePlayedCard);
      System.out.println("Player Tw0 wins this turn");
     }
    } else if ("2".equals(attributeNo)) {
     if (Integer.parseInt(playerOnePlayedCard.getAtrribute_2()) > Integer
       .parseInt(playerTwoplayedCard.getAtrribute_2())) {

      winnerFlag = "1";
      playerOneCardQueue.addToFront(playerTwoplayedCard);
      playerOneCardQueue.addToFront(playerOnePlayedCard);
      System.out.println("Player One wins this turn");
     } else {
      winnerFlag = "2";
      playerTwoCardQueue.addToFront(playerTwoplayedCard);
      playerTwoCardQueue.addToFront(playerOnePlayedCard);
      System.out.println("Player Tw0 wins this turn");
     }

    } else if ("3".equals(attributeNo)) {
     if (Integer.parseInt(playerOnePlayedCard.getAtrribute_3()) > Integer
       .parseInt(playerTwoplayedCard.getAtrribute_3())) {

      winnerFlag = "1";
      playerOneCardQueue.addToFront(playerTwoplayedCard);
      playerOneCardQueue.addToFront(playerOnePlayedCard);
      System.out.println("Player One wins this turn");
     } else {
      winnerFlag = "2";
      playerTwoCardQueue.addToFront(playerTwoplayedCard);
      playerTwoCardQueue.addToFront(playerOnePlayedCard);
      System.out.println("Player Tw0 wins this turn");
     }

    }

   } else {
    Card playerTwoplayedCard = playerTwoCardQueue.dequeue();

    System.out
      .println("Player, Which attribute you want to compare! ,1/2/3");
    String attributeNo = s.next();
    // Now player 1 plays his card
    Card playerOnePlayedCard = playerOneCardQueue.dequeue();
    // Now decide the winner
    if ("1".equals(attributeNo)) {
     if (Integer.parseInt(playerTwoplayedCard.getAtrribute_1()) > Integer
       .parseInt(playerOnePlayedCard.getAtrribute_1())) {

      winnerFlag = "2";
      playerTwoCardQueue.addToFront(playerTwoplayedCard);
      playerTwoCardQueue.addToFront(playerOnePlayedCard);
      System.out.println("Player Tw0 wins this turn");
     } else {
      winnerFlag = "1";
      playerOneCardQueue.addToFront(playerTwoplayedCard);
      playerOneCardQueue.addToFront(playerOnePlayedCard);
      System.out.println("Player One wins this turn");
     }
    } else if ("2".equals(attributeNo)) {
     if (Integer.parseInt(playerTwoplayedCard.getAtrribute_2()) > Integer
       .parseInt(playerOnePlayedCard.getAtrribute_2())) {

      winnerFlag = "2";
      playerTwoCardQueue.addToFront(playerTwoplayedCard);
      playerTwoCardQueue.addToFront(playerOnePlayedCard);
      System.out.println("Player Tw0 wins this turn");
     } else {
      winnerFlag = "1";
      playerOneCardQueue.addToFront(playerTwoplayedCard);
      playerOneCardQueue.addToFront(playerOnePlayedCard);
      System.out.println("Player One wins this turn");
     }
    } else if ("3".equals(attributeNo)) {
     if (Integer.parseInt(playerTwoplayedCard.getAtrribute_3()) > Integer
       .parseInt(playerOnePlayedCard.getAtrribute_3())) {

      winnerFlag = "2";
      playerTwoCardQueue.addToFront(playerTwoplayedCard);
      playerTwoCardQueue.addToFront(playerOnePlayedCard);
      System.out.println("Player Tw0 wins this turn");
     } else {
      winnerFlag = "1";
      playerOneCardQueue.addToFront(playerTwoplayedCard);
      playerOneCardQueue.addToFront(playerOnePlayedCard);
      System.out.println("Player One wins this turn");
     }

    }

   }

  }
  System.out.println("Game completed !! and the winner is :"
    + (playerOneCardQueue.size() == 0 ? "Player 2" : "Player1")
    + " Hurray !!");

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote