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

Could someone please code this Java UML. it is War Card game with 2 playes and 5

ID: 3695995 • Letter: C

Question


Could someone please code this Java UML. it is War Card game with 2 playes and 52 cards and deck and higher card wins and they should play untill nothing left in their hands also it should be the same as the UML as possible.

**** NO GUI please, just a normal input "scanner"


GroupOfCards Card cards: Cardl -num int -suit: int -currentSize: int +addCard(card : Card): void +display0 : void +displayO: void Deck Hand +shuffle0: void +dealCardO: Card +sortO void +playACard0: Card Game -deck: Deck -playerl : Hand -player2 : Hand +playAGame): void

Explanation / Answer

main program is WarGame class

2 other files are Card, Deck

____

public class WarGame {

public static void main(String[] args) {

Card[][] hands = new Card[2][1];
Deck myDeck = new Deck();

//reduced this to 26 iterations because two cards are dealt each iteration
for (int i = 0; i < 26; i++) {
System.out.printf(" Round %s of The War ", i);

//You really don't need to loop just once here...
//Simply assign the card to hands[player][0] since that's the only option
//for (int c = 0; c < 1; c++)
for (int player = 0; player < hands.length; player++)
hands[player][0] = myDeck.dealCard();

for (int player = 0; player < hands.length; player++) {
System.out.printf("Player %d: ", player);
printHand(hands[player]);
}

int player1 = hands[0][0].getValue(); //get the value from the Card object
int player2 = hands[1][0].getValue();

if (player1 > player2)
System.out.println("Player One Wins The War");
else if (player2 > player1)
System.out.println("Player Two Wins The War");
else
System.out.println("The War Is A Tie");

}
}

public static void printHand(Card[] hand) {

for (int card = 0; card < hand.length; card++)
System.out.printf("%s", hand[card].toString());

System.out.println();

}
}

_____________


public class Card {

private int cardNum;
final static String[] suits = {"Spades", "Hearts", "Diamonds", "Clubs"};
final static String[] ranks = {"2", "3","4","5","6","7","8", "9","10", "Jack", "Queen", "King", "Ace"};

Card (int theCard) {
setCardNum (theCard);
}

public void setCardNum (int theCard) {
cardNum = (theCard >= 0 && theCard <= 51)? theCard: 0;
}

public int getCardNum() {
return cardNum;
}

public String toString() {
return ranks[cardNum%13] + " of " + suits[cardNum/13];
}

public String getSuit() {
return suits[cardNum/13];
}

public String getRank() {
return ranks[cardNum%13];
}

public int getValue() {
return cardNum%13;
}
}

______________

public class Deck {

private Card[] deck = new Card[52];
private int topCard;

Deck() {

topCard = 0;

for (int i = 0; i < deck.length; i++)
deck[i] = new Card(i);

}

public void shuffle() {

topCard = 0;

for (int i = 0; i < 1000; i++) {
int j = (int)(Math.random()*52);
int k = (int)(Math.random()*52);
Card tmpCard = deck[j];
deck[j] = deck[k];
deck[k] = tmpCard;
}
}

public Card dealCard() {
Card theCard;
if (topCard < deck.length) {
theCard = deck[topCard];
topCard++;
}
else
theCard = null;

return theCard;
}
}