Read carefully. You are to simulate a game called TrumpWar , which is a variatio
ID: 3836015 • Letter: R
Question
Read carefully.
You are to simulate a game called TrumpWar, which is a variation of the child’s card game called War. Like War, there is no strategy to playing the game, so you are writing the mechanics of 2 people playing against each other according to the rules. Unlike War, there is no risking 3 cards in case of a tie and trump cards have been added (that beat non-trump cards).
The setup of a TrumpWar is as follows:
Each player has an ordered pile of Cards. There is also a pile of Cards that are designated as “Trump Cards”; this means that they automatically will beat any non-trump card, no matter the rank of each card is. (As you picture this, remember that the Card instances will be references, so a player’s pile and the trump pile can both refer to the same Card). Each of these piles of Cards should be stored in an ArrayList so your logic can use ArrayList’s methods.
They then play “hands” against each other until one player wins. The game is over when one player has MORE THAN twice as many Cards as the other player (checked after the hand is played). They will always play at least one hand in the game.
During each hand, each player takes a card out of their ArrayList (from position 0) and then plays the Cards against each other according to the rules below. Ace is considered the high card. So a key thing is to be able to determine (in an OOP way) which Card is higher…
During each hand, each player removes their top Card (from position 0 of the ArrayList) to play. Then the specific rules for the hand are:
If one player has a trump card and the other does not, then the player with the trump card wins and gets both cards and also the contents of the Treasury (see the last rule below) and adds them to the bottom of their pile (ArrayList). Be sure that Player1’s Card, THEN Player2’s Card, then the contents of the Treasury are added in that order. The player that won the hand adds 1 to their current winning streak. Also keep track of that player’s longest winning streak for the whole game.
But if both players have trump cards, then the player whose trump card has the higher rank wins and gets both cards and also the contents of the Treasury (see the last rule below) and adds them to the bottom of their pile (ArrayList). Be sure that Player1’s Card, THEN Player2’s Card, then the contents of the Treasury are added in that order. The player that won the hand adds 1 to their current winning streak. Also keep track of that player’s longest winning streak for the whole game.
But if neither player has a trump card, then the player whose card has the higher rank wins and gets both cards and also the contents of the Treasury (see the last rule below) and adds them to the bottom of their pile (ArrayList). Be sure that Player1’s Card, THEN Player2’s Card, then the contents of the Treasury are added in that order. The player that won the hand adds 1 to their current winning streak. Also keep track of that player’s longest winning streak for the whole game.
But if both players have trump cards with the same rank or non-trump cards with the same rank, then it is a tie. The player with the longer winning streak for the whole game gets both cards and also the contents of the Treasury (see the last rule below) and puts them on the bottom of their pile (ArrayList). Be sure that Player1’s Card, THEN Player2’s Card, then the contents of the Treasury are added in that order. Since this hand was a tie, both players current winning streaks must start over at 0.
But if both players have trump cards with the same rank or non-trump cards with the same rank and also have the same longest winning streak for the whole game, then it is a tie and the cards go into another pile (ArrayList) called the Treasury. Since this hand was a tie, both player’s current winning streaks must start over at 0.
Using ArrayList’s methods: Once your ArrayLists are created and filled with Cards (for details, see the constructor requirements below), most of the functionality can be done with the ArrayList methods. Specifically, it would help if you were familiar with:
ArrayList’s add method
ArrayList’s remove method
ArrayList’s size() method
ArrayList’s contains method
ArrayList’s toString() method
ArrayList’s addAll method (if you want, when you move Cards from the Treasury).
ArrayList’s clear() method (if you use addAll, since the Treasury will not be emptied)
Card’s compareTo method.
Your TrumpWar class should have:
Data that will have 4 ArrayList’s; one for player1’s Cards, one for player2’s Cards, one for the trump Cards, and one for the treasury Cards. Note that they can be declared here
( private ArrayList p1Cards; for example)
and then created in the constructors. You can also have additional data to hold the current and longest winning streak of each player.
A default constructor: Normally, the code in the default constructor would create a deck of 52 Cards, shuffle them, deal 26 to each player, and choose some at random to put in the trump ArrayList. But…this is too non-specific for Hypergrade to handle and too many Cards to work with. Therefore, your default constructor could just create the 4 ArrayLists.
( p1Cards = new ArrayList(); for example)
The tester will not use the default constructor, but will create specific situations using the parameterized constructor.
A parameterized constructor that receives 3 arrays of Cards, which represent player1’s Cards, player2’s Cards, and the trump cards respectively. It should:
Create the 4 new ArrayList instances
( p1Cards = new ArrayList(); for example)
Fill player1’s ArrayList of Cards by traversing the first array of Cards (player1’s), starting at position 0, and tell player1’s ArrayList to add each Card to itself.
Fill player1’s ArrayList of Cards by traversing the second array of Cards (player2’s), starting at position 0, and tell player2’s ArrayList to add each Card to itself.
Fill the ArrayList of trump Cards by traversing the third array of Cards (the trump cards), starting at position 0, and tell the trump ArrayList to add each Card to itself.
(Be sure this tests correctly, as this constructor will be used to generate different situations to test your TrumpWar)
A method called play(), which actually contains the loop and logic to play a game of TrumpWar. The same logic should happen over and over again using the rules above until one player has more than twice as many cards as the other player. It should be printing the results (see below) as it loops through the logic.
Card code:
Card.java:
public class Card implements Comparable {
private int rank;
private char suit;
public int getRank() {
return rank;
}
public void setRank(int rank) {
this.rank = rank;
}
public char getSuit() {
return suit;
}
public void setSuit(char suit) {
this.suit = suit;
}
public Card(int r, char thisSuit) {
if(r < 2 || r > 14){
throw new IllegalArgumentException("Please enter between 2 to 14");
}
if(thisSuit != 'C' && thisSuit != 'D' && thisSuit != 'H' && thisSuit != 'S'){
throw new IllegalArgumentException("Please enter valid suit (C,D,H,S)");
}
this.rank = r;
this.suit = thisSuit;
}
public Card() {
this.rank = 14;
this.suit = 'S';
}
public Card(Card argCard) {
this.rank = argCard.getRank();
this.suit = argCard.getSuit();
}
@Override
public int compareTo(Object o) {
Card card = (Card) o;
if(this.rank > card.getRank()){
return 1;
}
else if(this.rank < card.getRank()){
return -1;
}
else{
return 0;
}
}
@Override
public String toString() {
if(this.rank <= 10){
return this.rank+""+this.suit+"";
}
else{
char finalSuit = 0;
if(this.rank == 11){
finalSuit = 'J';
}
if(this.rank == 12){
finalSuit = 'Q';
}
if(this.rank == 13){
finalSuit = 'K';
}
if(this.rank == 14){
finalSuit = 'A';
}
return this.rank+""+finalSuit+"";
}
}
@Override
public boolean equals(Object obj) {
if(obj instanceof String){
String val = (String) obj;
String rankVal = val.charAt(0)+"";
String suitVal = val.charAt(1)+"";
String thisRank = this.getRank()+"";
String thisSuit = this.getSuit()+"";
if(thisRank.equals(rankVal) && thisSuit.equals(suitVal)){
return true;
}
else{
return false;
}
}
Card card = (Card) obj;
if(card == null){
return false;
}
else if(this.rank == card.rank && this.suit == card.suit){
return true;
}
else{
return false;
}
}
}
Tester:
public class TrumpWarTester
{
public static void main(String[ ] args)
{
//get the arguments for the test
java.util.Scanner kb = new java.util.Scanner(System.in);
String test = kb.nextLine();
//***************************************************
//***************************************************
if (test.equalsIgnoreCase("1. In first hand, everything ties, so cards go into Treasury. Player1 wins second hand (and Treasury) because they have a trump."))
{
boolean printDescription = true;
boolean checkChanges = true;
try
{
Card[ ] p1CardArray = { new Card(2, 'S'), new Card(3, 'C') };
Card[ ] p2CardArray = { new Card(2, 'D'), new Card(12, 'H') };
Card[ ] trumpArray = { new Card(3, 'C'), new Card(8, 'S'), new Card(8, 'H'), new Card(11, 'D') };
TrumpWar myTW = new TrumpWar(p1CardArray, p2CardArray, trumpArray);
myTW.play();
}
catch (Throwable ex)
{
//System.out.println(" " + ex);
System.out.println(" " + ex.getClass().getName());
}
}
else if (test.equalsIgnoreCase("2. In first hand, everything ties, so cards go into Treasury. Player2 wins second hand (and Treasury) because they have a trump."))
{
boolean printDescription = true;
boolean checkChanges = true;
try
{
Card[ ] p2CardArray = { new Card(2, 'S'), new Card(3, 'C') };
Card[ ] p1CardArray = { new Card(2, 'D'), new Card(12, 'H') };
Card[ ] trumpArray = { new Card(3, 'C'), new Card(8, 'S'), new Card(8, 'H'), new Card(11, 'D') };
TrumpWar myTW = new TrumpWar(p1CardArray, p2CardArray, trumpArray);
myTW.play();
}
catch (Throwable ex)
{
//System.out.println(" " + ex);
System.out.println(" " + ex.getClass().getName());
}
}
else if (test.equalsIgnoreCase("3. In first hand, everything ties, so cards go into Treasury. Player1 wins second hand (and Treasury) because they have a higher trump."))
{
boolean printDescription = true;
boolean checkChanges = true;
try
{
Card[ ] p1CardArray = { new Card(2, 'S'), new Card(8, 'H') };
Card[ ] p2CardArray = { new Card(2, 'D'), new Card(3, 'C') };
Card[ ] trumpArray = { new Card(3, 'C'), new Card(8, 'S'), new Card(8, 'H'), new Card(11, 'D') };
TrumpWar myTW = new TrumpWar(p1CardArray, p2CardArray, trumpArray);
myTW.play();
}
catch (Throwable ex)
{
//System.out.println(" " + ex);
System.out.println(" " + ex.getClass().getName());
}
}
else if (test.equalsIgnoreCase("4. In first hand, everything ties, so cards go into Treasury. Player2 wins second hand (and Treasury) because they have a higher trump."))
{
boolean printDescription = true;
boolean checkChanges = true;
try
{
Card[ ] p2CardArray = { new Card(2, 'S'), new Card(8, 'H') };
Card[ ] p1CardArray = { new Card(2, 'D'), new Card(3, 'C') };
Card[ ] trumpArray = { new Card(3, 'C'), new Card(8, 'S'), new Card(8, 'H'), new Card(11, 'D') };
TrumpWar myTW = new TrumpWar(p1CardArray, p2CardArray, trumpArray);
myTW.play();
}
catch (Throwable ex)
{
//System.out.println(" " + ex);
System.out.println(" " + ex.getClass().getName());
}
}
else if (test.equalsIgnoreCase("5. In first hand, everything ties, so cards go into Treasury. Player1 wins second hand (and Treasury) because they have a higher non-trump."))
{
boolean printDescription = true;
boolean checkChanges = true;
try
{
Card[ ] p1CardArray = { new Card(2, 'S'), new Card(12, 'D') };
Card[ ] p2CardArray = { new Card(2, 'D'), new Card(11, 'C') };
Card[ ] trumpArray = { new Card(3, 'C'), new Card(8, 'S'), new Card(8, 'H'), new Card(11, 'D') };
TrumpWar myTW = new TrumpWar(p1CardArray, p2CardArray, trumpArray);
myTW.play();
}
catch (Throwable ex)
{
//System.out.println(" " + ex);
System.out.println(" " + ex.getClass().getName());
}
}
else if (test.equalsIgnoreCase("6. In first hand, everything ties, so cards go into Treasury. Player2 wins second hand (and Treasury) because they have a higher non-trump."))
{
boolean printDescription = true;
boolean checkChanges = true;
try
{
Card[ ] p2CardArray = { new Card(2, 'S'), new Card(12, 'D') };
Card[ ] p1CardArray = { new Card(2, 'D'), new Card(11, 'C') };
Card[ ] trumpArray = { new Card(3, 'C'), new Card(8, 'S'), new Card(8, 'H'), new Card(11, 'D') };
TrumpWar myTW = new TrumpWar(p1CardArray, p2CardArray, trumpArray);
myTW.play();
}
catch (Throwable ex)
{
//System.out.println(" " + ex);
System.out.println(" " + ex.getClass().getName());
}
}
else if (test.equalsIgnoreCase("7. Hands #6 and #8 are ties but Player1 wins them (and Treasury) because they have longer overall streaks."))
{
boolean printDescription = true;
boolean checkChanges = true;
try
{
Card[ ] p1CardArray = { new Card(5, 'C'), new Card(14, 'H'), new Card(9, 'H'), new Card(3, 'H'), new Card(5, 'D'),
new Card(4, 'H'), new Card(8, 'C'), new Card(5, 'D'), new Card(6, 'C'), new Card(14, 'C'), };
Card[ ] p2CardArray = { new Card(2, 'S'), new Card(10, 'D'), new Card(6, 'C'), new Card(9, 'D'), new Card(7, 'S'),
new Card(4, 'D'), new Card(9, 'S'), new Card(5, 'S'), new Card(4, 'S'), new Card(11, 'S'), };
Card[ ] trumpArray = { new Card(3, 'C'), new Card(8, 'S'), new Card(8, 'H'), new Card(11, 'D') };
TrumpWar myTW = new TrumpWar(p1CardArray, p2CardArray, trumpArray);
myTW.play();
}
catch (Throwable ex)
{
//System.out.println(" " + ex);
System.out.println(" " + ex.getClass().getName());
}
}
else if (test.equalsIgnoreCase("8. Hands #6 and #8 are ties but Player2 wins them (and Treasury) because they have longer overall streaks."))
{
boolean printDescription = true;
boolean checkChanges = true;
try
{
Card[ ] p2CardArray = { new Card(5, 'C'), new Card(14, 'H'), new Card(9, 'H'), new Card(3, 'H'), new Card(5, 'D'),
new Card(4, 'H'), new Card(8, 'C'), new Card(5, 'D'), new Card(6, 'C'), new Card(14, 'C'), };
Card[ ] p1CardArray = { new Card(2, 'S'), new Card(10, 'D'), new Card(6, 'C'), new Card(9, 'D'), new Card(7, 'S'),
new Card(4, 'D'), new Card(9, 'S'), new Card(5, 'S'), new Card(4, 'S'), new Card(11, 'S'), };
Card[ ] trumpArray = { new Card(3, 'C'), new Card(8, 'S'), new Card(8, 'H'), new Card(11, 'D') };
TrumpWar myTW = new TrumpWar(p1CardArray, p2CardArray, trumpArray);
myTW.play();
}
catch (Throwable ex)
{
//System.out.println(" " + ex);
System.out.println(" " + ex.getClass().getName());
}
}
else if (test.equalsIgnoreCase("9. Hand #5 and #6 are ties and both players have same overall streaks. Cards go into Treasury until Player1 wins Hand #7. No winner until > twice as many cards."))
{
boolean printDescription = true;
boolean checkChanges = true;
try
{
Card[ ] p2CardArray = { new Card(14, 'H'), new Card(9, 'H'), new Card(3, 'H'), new Card(5, 'D'),
new Card(4, 'H'), new Card(8, 'S'), new Card(13, 'S'), new Card(4, 'C'), new Card(5, 'S') };
Card[ ] p1CardArray = { new Card(10, 'D'), new Card(6, 'C'), new Card(9, 'D'), new Card(7, 'S'),
new Card(4, 'D'), new Card(8, 'H'), new Card(14, 'D'), new Card(6, 'S'), new Card(10, 'C') };
Card[ ] trumpArray = { new Card(3, 'C'), new Card(8, 'S'), new Card(8, 'H'), new Card(11, 'D') };
TrumpWar myTW = new TrumpWar(p1CardArray, p2CardArray, trumpArray);
myTW.play();
}
catch (Throwable ex)
{
//System.out.println(" " + ex);
System.out.println(" " + ex.getClass().getName());
}
}
else if (test.equalsIgnoreCase("10. Hand #5 and #6 are ties and both players have same overall streaks. Cards go into Treasury until Player2 wins Hand #7. No winner until > twice as many cards."))
{
boolean printDescription = true;
boolean checkChanges = true;
try
{
Card[ ] p1CardArray = { new Card(14, 'H'), new Card(9, 'H'), new Card(3, 'H'), new Card(5, 'D'),
new Card(4, 'H'), new Card(8, 'S'), new Card(13, 'S'), new Card(4, 'C'), new Card(5, 'S') };
Card[ ] p2CardArray = { new Card(10, 'D'), new Card(6, 'C'), new Card(9, 'D'), new Card(7, 'S'),
new Card(4, 'D'), new Card(8, 'H'), new Card(14, 'D'), new Card(6, 'S'), new Card(10, 'C') };
Card[ ] trumpArray = { new Card(3, 'C'), new Card(8, 'S'), new Card(8, 'H'), new Card(11, 'D') };
TrumpWar myTW = new TrumpWar(p1CardArray, p2CardArray, trumpArray);
myTW.play();
}
catch (Throwable ex)
{
//System.out.println(" " + ex);
System.out.println(" " + ex.getClass().getName());
}
}
else if (test.equalsIgnoreCase("11. A game with every possibility."))
{
boolean printDescription = true;
boolean checkChanges = true;
try
{
Card[ ] p1CardArray = { new Card(8, 'H'),
new Card(3, 'H'),
new Card(13, 'C'),
new Card(10, 'D'),
new Card(11, 'C'),
new Card(8, 'D'),
new Card(4, 'D'),
new Card(7, 'H'),
new Card(11, 'H'),
new Card(6, 'D'),
new Card(2, 'C'),
new Card(3, 'D'),
new Card(7, 'H'),
new Card(10, 'S'),
new Card(2, 'H'),
new Card(5, 'D') };
Card[ ] p2CardArray = { new Card(8, 'C'),
new Card(14, 'D'),
new Card(13, 'H'),
new Card(6, 'C'),
new Card(2, 'S'),
new Card(5, 'S'),
new Card(10, 'D'),
new Card(7, 'S'),
new Card(14, 'C'),
new Card(12, 'H'),
new Card(14, 'S'),
new Card(8, 'S'),
new Card(7, 'S'),
new Card(12, 'C'),
new Card(7, 'D'),
new Card(10, 'H') };
Card[ ] trumpArray = { new Card(3, 'H'),
new Card(4, 'D'),
new Card(5, 'S'),
new Card(6, 'C'),
new Card(7, 'H'),
new Card(7, 'S'),
new Card(10, 'D'),
new Card(12, 'S'),
new Card(12, 'H') };
TrumpWar myTW = new TrumpWar(p1CardArray, p2CardArray, trumpArray);
myTW.play();
}
catch (Throwable ex)
{
//System.out.println(" " + ex);
System.out.println(" " + ex.getClass().getName());
}
}
}
}
Explanation / Answer
package card;
import card.*;
import java.util.*;
public class TrumpWar
{
//data stored in 4 ArrayLists of Cards
private ArrayList<Card> p1Cards;
private ArrayList<Card> p2Cards;
private ArrayList<Card> trmpCards;
private ArrayList<Card> trsryCards;
//default constructor - creates the ArrayLists that will be used
public TrumpWar()
{
p1Cards = new ArrayList<Card>();
p2Cards = new ArrayList<Card>();
trmpCards = new ArrayList<Card>();
trsryCards = new ArrayList<Card>();
}
//parametrized constructor
public TrumpWar(ArrayList<Card> player1, ArrayList<Card> player2, ArrayList<Card> trump)
{
Iterator<Card> itr1 = player1.iterator();
while(itr1.hasNext()){
Card card1 = itr1.next();
p1Cards = new ArrayList<Card>();
p1Cards.add(card1);
}
Iterator<Card> itr2 = player2.iterator();
while(itr2.hasNext()){
Card card2 = itr2.next();
p2Cards = new ArrayList<Card>();
p2Cards.add(card2);
}
Iterator<Card> itr3 = trump.iterator();
while(itr3.hasNext()){
Card card3 = itr3.next();
trmpCards = new ArrayList<Card>();
trmpCards.add(card3);
}
}
public void play(){
int p1_streak = 0;
int p2_streak = 0;
Card p1[]=new Card[p1Cards.size()];
p1Cards.toArray(p1);
Card p2[]=new Card[p2Cards.size()];
p2Cards.toArray(p2);
for (int i = p1Cards.size()-1; i >=0; i--) {
if((trmpCards.contains(p1Cards.remove(i))) && (!trmpCards.contains(p2Cards.remove(i)))){
p1Cards.add(p1[i]);
p1Cards.add(p2[i]);
p1Cards.addAll(trsryCards);
p1_streak++;
}
else if((!trmpCards.contains(p1Cards.remove(i))) && (trmpCards.contains(p2Cards.remove(i)))){
p2Cards.add(p1[i]);
p2Cards.add(p2[i]);
p2Cards.addAll(trsryCards);
p2_streak++;
}
else {
if(p1[i].getRank() > p2[i].getRank()){
p1Cards.add(p1[i]);
p1Cards.add(p2[i]);
p1Cards.addAll(trsryCards);
p1_streak++;
}
else if(p2[i].getRank() > p1[i].getRank()){
p2Cards.add(p1[i]);
p2Cards.add(p2[i]);
p2Cards.addAll(trsryCards);
p2_streak++;
}
else{
if(p1_streak > p2_streak){
p1Cards.add(p1[i]);
p1Cards.add(p2[i]);
p1Cards.addAll(trsryCards);
}
else if(p2_streak > p1_streak){
p2Cards.add(p1[i]);
p2Cards.add(p2[i]);
p2Cards.addAll(trsryCards);
}
else{
trsryCards.add(p1[i]);
trsryCards.add(p2[i]);
}
p1_streak = 0;
p2_streak = 0;
}
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.