简体   繁体   中英

How do I compare two card objects that hold strings?

I'm making a simulation of the card game War, and I am having problems with finding the greater value card between two card objects. For example, if player1 has a 10 of hearts and player2 has a 9 of clubs, player1 should win the hand. However, since the cards hold string variables, I have no idea how to compare them to determine which card has a greater value. I have three classes, a card class that holds two string arrays, a deck class which is a collections of cards, and a main class.

import java.util.ArrayList;
import java.util.Collections;

public class Deck {
private ArrayList<Card> cards;
public Deck() {
    cards = new ArrayList<Card>();
    for(int i = 0; i <=3; i++) {
        for(int j = 0 ; j <= 12; j++) {
            cards.add(new Card(i,j));
        }
    }  
}

public void shuffle() {
    Collections.shuffle(cards);
} 

public Card deal() {
    if (isEmpty() == true) {
        return null;
    }
    else {
        return cards.remove(0);
    }
}
public Card playCard() {
    return cards.remove(0);
}
public void getCard (Card newCard) {
    cards.add(newCard);
}
public void clearDeck() {
    cards.removeAll(cards);
}
public boolean isEmpty() {
    return cards.isEmpty();
}

public int cardCount() {
    return cards.size();
}

public String toString() {
    String result = "Cards remaining in deck: " + cards;
    return result;
}    
}

 public class Card 
 {
private int type, value;
protected String[] cardType = {"Clubs", "Spades", "Diamonds", "Hearts"};
protected String[] cardValue = {"Ace", "King", "Queen", "Jack", "10",
                               "9", "8", "7", "6", "5", "4", "3", "2"};

public Card() {

}
public Card(int types, int values)
{
    type = types; 
    value = values;
}


public String toString()
{
    String resultCard = cardValue[value] + " of " + cardType[type];

    return resultCard;
}

}

public class Main
{
public static void main(String[] args)
{
    Deck deck = new Deck();
    Deck player1 = new Deck();
    Deck player2 = new Deck();
    Deck pile = new Deck();
    pile.clearDeck();
    deck.shuffle();

    dealCards(deck, player1, player2);
    addToPile(player1, player2, pile);
}
 public static void dealCards(Deck deck, Deck p1, Deck p2) {
   for (int i=0; i <26; i++) {
        p1.getCard(deck.deal());
        p2.getCard(deck.deal());
   }
   p1.shuffle();
   p2.shuffle();
 }
 public static void addToPile(Deck p1, Deck p2, Deck deckPile) {
    Card p1Card;
    Card p2Card;
    p1Card = p1.playCard();
    p2Card = p2.playCard();
    deckPile.getCard(p1Card);
    deckPile.getCard(p2Card);
  } 
}

What you can do is create a method in your Card class which compares 2 Cards .

Since i don't know the rules of the game here is an example with the following rules
1. Clubs > Spades > Diamonds > Hearts
2. Highest card wins

It might be something like this:

// 1 -> This card is greater
// 0 -> both cards are equal
// -1 -> This card is lesser
public int isGreater(Card cardToCompare) {
    // Check the cardType
    if (this.type < cardToCompare.type) {
        return 1;
    }
    if (this.type == cardToCompare.type) {
        if (this.value < cardToCompare.value) {
            return 1;
        }
        if (this.value == cardToCompare.value) {
            return 0;
        }
        if (this.value > cardToCompare.value) {
            return -1;
        }
    }
    if (this.type > cardToCompare.type) {
        return -1;
    }
}

Note that lower numbers for value or type equals a greater card in the arrays.

EDIT: Since card type doesn't matter in this game (so i've been told), you can also do this:

// positive result -> this card is greater
// 0 -> cards are equal
// negative result -> cardToCompare is greater
public int isGreater(Card cardToCompare) {
    return cardToCompare.value - this.value;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM