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);
}
}