I'm writing a program creating a card game of war. I (think) that I have the majority of it done, but I cannot seem to figure out how to split a deck into two separate hands in a Hand class. My code is in components, I have a Deck, Card, Game, War (main) classes. I have the Game set up to use a Hand class with a player A hand and a player B hand, but I'm having a hard time wrapping my head around creating this class with the two hands. Here is my Card and Deck classes (so far). Could anyone point me in the right direction? I can post my other two classes if needed, but I don't want to scare anyone away with how much code I post.

public class Card
{
	private int rank;
	private int suit;
	
	public Card(int rank, int suit) 
	{
	    this.rank = rank;
		 this.suit = suit;
   }
		
	public int getRank()
	{
		 return rank;
	}
	
	public int getSuit()
	{
	    return suit;
	}
	
	
	public String toString()
	{		
		 String printed;
		
		 switch (rank){
		
		 case 11:
			  printed = "Jack ";
		  	  break;
		 case 12:
			  printed = "Queen ";
			  break;
		 case 13:
			  printed = "King ";
			  break;
		 case 14: 
			  printed = "Ace ";
			  break;
		 default: 
			  printed = rank + " ";
			  break;		
	}   
		
	printed = printed + "of";
		
	switch (suit)
	{
		
		 case 1: 
			  printed = printed + " Diamonds";
			  break;
		 case 2:
			  printed = printed + " Hearts";
			  break;
		 case 3:
			  printed = printed + " Spades";
		 	  break;
		 case 4:
			  printed = printed + " Clubs";
			  break;	
	}   
		
		 return printed;
	}
	
	public static int compareRank(int rank1, int rank2)
	{
		 int num = 0;
		
		 if (rank1 > rank2)
		    num = 1;
		 else
		      if (rank1 < rank2)
			       num = -1;
	   	   else
		          num = 2;
		
		 return num;
	}	
}
import java.util.*;
 
public class Deck{
	
	 List<Card> deck = new ArrayList<Card>();
		
	 public Deck()
	 {
		  int x, y;
		
		  for (x = 2; x <= 14; x++)
		  {
			   for (y = 1; y <= 4; y++)
			   {
				    deck.add( new Card(x, y) );
			   }
		  }
		
		  Collections.shuffle(deck);
		  Collections.shuffle(deck);
		  Collections.shuffle(deck);
	          Collections.shuffle(deck);
		  Collections.shuffle(deck);
		  Collections.shuffle(deck);
		  Collections.shuffle(deck);
		
		  System.out.println(deck);
 	 }
 
	 public List<Card> splitDeck(int handNum)
	 {
		  List<Card> tempHand = new ArrayList<Card>();
		
		  if (handNum == 1)
			   tempHand = deck.subList(0, 26);
		  if (handNum == 2)
			   tempHand = deck.subList(26, 52);
		
		  return tempHand;
	 } 
}

I would think your game class would have a variable for number of cards in a hand. The deck class could contain a method deal(numberOfCards, numberOfHands) that returns a Set of Hand instances. This would keep your deck independent of the game and not tied to the specifics of your game. Since you have shuffled already, dealing to the Hand instances can be just like a real game, one card per hand until all have the required number of cards.

A Hand class would really just be a List of Cards with a few methods like add() and discard(). The game class would be responsible for evaluating a Hand, since that is specific to the Game and not the Hand.

Actually, thinking on it a bit, it seems the deck should just contain a method deal() that returns a single card. The Game class should be responsible for seeing that each Player gets a card dealt until they have the required number.
In pseudocode:

for (numberPerHand) {
  for (numberOfPlayers) {
    player.take( deck.deal() )
  }
}

OK, I'm still pretty confused, but I've got a better card class and a deal class that utilizes it. It works, but I've got a main method in the deal class. Do you think that I can make it work how it is? I wrote my game class to utilize a player A hand and a player B hand in a Hand class.

import java.util.*;

public class Card
{
    public enum Rank 
	 { 
	     DEUCE, THREE, FOUR, FIVE, SIX,
        SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE 
	 }

    public enum Suit 
	 { 
	     CLUBS, DIAMONDS, HEARTS, SPADES 
	 }

    private final Rank rank;
    private final Suit suit;
	 
    private Card(Rank rank, Suit suit) 
	 {
        this.rank = rank;
        this.suit = suit;
    }

    public Rank rank() 
	 { 
	     return rank;
	 }
	 
    public Suit suit() 
	 { 
	     return suit; 
	 }
	 
    public String toString()
	 { 
	     return rank + " of " + suit; 
	 }

    private static final List<Card> protoDeck = new ArrayList<Card>();

    static 
	 {
        for (Suit suit : Suit.values())
            for (Rank rank : Rank.values())
                protoDeck.add(new Card(rank, suit));
    }

    public static ArrayList<Card> newDeck() 
	 {
        return new ArrayList<Card>(protoDeck); 
    }		
}
import java.util.*;

public class Deal 
{
    public static void main(String args[]) {
        int numHands = 2;
        int cardsPerHand = 26;
        List<Card> deck  = Card.newDeck();
        Collections.shuffle(deck);
        for (int i=0; i < numHands; i++)
            System.out.println(deal(deck, cardsPerHand));
    }

    public static ArrayList<Card> deal(List<Card> deck, int n) 
	 {
         int deckSize = deck.size();
         List<Card> handView = deck.subList(deckSize-n, deckSize);
         ArrayList<Card> hand = new ArrayList<Card>(handView);
         handView.clear();
         return hand;
     }
}
import java.io.*;

public class Game 
{	
    Hand playerAHand = new Hand(1);
	 Hand playerBHand = new Hand(2);
	
	 int playerARank;
	 int playerBRank;
	
	 public void go()
	 {
		  TextIO.putln("This is a game of war\ntype d to draw a card...\nTo quit, type q at any time.\n");
		  String draw = "";
		  //playerAHand.printHand();
		  //playerBHand.printHand();
			
	     while (!playerAHand.getHand().isEmpty() && !playerBHand.getHand().isEmpty() && !draw.toLowerCase().equals("q") )
		  {
		      draw = TextIO.getln();
			
			   if (draw.toLowerCase().equals("d"))
			   {
			       playerARank = playerAHand.drawCard(1);
			       playerBRank = playerBHand.drawCard(2);
		
			       int win = Card.compareRank(playerARank, playerBRank);
		
			       determineWinner(win);
			
			       System.out.println("Player A has " + playerAHand.getSize() + " cards.");
			       System.out.println("Player B has " + playerBHand.getSize() + " cards.");
			
			       System.out.println();			
			   }		
		  }
	
		  if (playerAHand.getHand().isEmpty())
		  {
		      System.out.println("player B wins!");
		  }
		  else 
		      if (playerBHand.getHand().isEmpty())
			   {
				    System.out.println("player A wins!");
			   }
		      else
		      {
			   System.out.println("Quit!");
		      }		
     }
	
	  public void determineWinner(int win)
	  {
		   if (win == 1)
		   {
			    System.out.println("player A wins this round!");
			
			    playerAHand.getHand().add(playerAHand.getTopCard());
			    playerAHand.getHand().add(playerBHand.getTopCard());
			
			    playerAHand.removeTopCard();
			    playerBHand.removeTopCard();
		    }
		    else 
			     if (win == -1)
		        {
			         System.out.println("player B wins this round!");
			         playerBHand.getHand().add(playerBHand.getTopCard());
			         playerBHand.getHand().add(playerAHand.getTopCard());
			
			         playerBHand.removeTopCard();
			         playerAHand.removeTopCard();
		        }
		        else
		        {
			         if ((playerAHand.getHand().size() > 4) && (playerBHand.getHand().size() > 4))
				         war();
			         else
			         {
				          TextIO.putln("There are not enough cards to make War!");
				          playerAHand.getHand().add(playerBHand.getTopCard());
				          playerAHand.getHand().add(playerAHand.getTopCard());
				          playerAHand.removeTopCard();
				          playerBHand.removeTopCard();
			         }			
		        }
	  }
	
	  public void war()
	  {
		   System.out.println("WAR!");
		
		   System.out.println("Player B draws " + playerBHand.getHand().get(4) + "! "); 
		   System.out.println("Player A draws " + playerAHand.getHand().get(4) + "! "); 
	      int winner = Card.compareRank(playerAHand.getHand().get(4).getRank(), playerBHand.getHand().get(4).getRank());
		
		   if (winner == 1)
		   {
			    System.out.println("Player A takes Player B's cards!");
			
			    playerAHand.getHand().add(playerBHand.getTopCard());
			    playerAHand.getHand().add(playerAHand.getTopCard());
			    playerAHand.removeTopCard();
			    playerBHand.removeTopCard();
			
			    playerAHand.getHand().add(playerBHand.getTopCard());
			    playerAHand.getHand().add(playerAHand.getTopCard());
			    playerAHand.removeTopCard();
			    playerBHand.removeTopCard();
			
			    playerAHand.getHand().add(playerBHand.getTopCard());
			    playerAHand.getHand().add(playerAHand.getTopCard());
			    playerAHand.removeTopCard();
			    playerBHand.removeTopCard();
			
			    playerAHand.getHand().add(playerBHand.getTopCard());
			    playerAHand.getHand().add(playerAHand.getTopCard());
			    playerAHand.removeTopCard();
			    playerBHand.removeTopCard();
			
			    playerAHand.getHand().add(playerBHand.getTopCard());
			    playerAHand.getHand().add(playerAHand.getTopCard());
			    playerAHand.removeTopCard();
			    playerBHand.removeTopCard();			
		   }
		   else 
		       if (winner == -1)
		       {
			        System.out.println("Player B takes Player A's cards!");
			
			        playerBHand.getHand().add(playerAHand.getTopCard());
			        playerBHand.getHand().add(playerBHand.getTopCard());
			        playerBHand.removeTopCard();
			        playerAHand.removeTopCard();
			
			        playerBHand.getHand().add(playerAHand.getTopCard());
			        playerBHand.getHand().add(playerBHand.getTopCard());
		           playerBHand.removeTopCard();
			        playerAHand.removeTopCard();
			
			        playerBHand.getHand().add(playerAHand.getTopCard());
			        playerBHand.getHand().add(playerBHand.getTopCard());
			        playerBHand.removeTopCard();
			        playerAHand.removeTopCard();
			
			        playerBHand.getHand().add(playerAHand.getTopCard());
			        playerBHand.getHand().add(playerBHand.getTopCard());
			        playerBHand.removeTopCard();
			        playerAHand.removeTopCard();
			
			        playerBHand.getHand().add(playerAHand.getTopCard());
			        playerBHand.getHand().add(playerBHand.getTopCard());
			        playerBHand.removeTopCard();
			        playerAHand.removeTopCard();
		       }
		       else
		       {
			        System.out.println("It's a tie!");
			        playerAHand.getHand().add(playerAHand.getTopCard());
			        playerBHand.getHand().add(playerBHand.getTopCard());
			        playerBHand.removeTopCard();
			        playerAHand.removeTopCard();
		       }		
	  } 
 }

The Deal class is really just performing a function of the Game and has no intrinsic value as a stand-alone class.

Consider that any game of cards has a number of players and each receives a number of cards. Different games involve different numbers. Those parameters belong to the Game and should thus be defined there. Giving the Deck a method deal() that returns a single card so long as there are cards remaining in the deck most closely mirrors a real-world deck that you are trying to model in your code. The Game class just needs to cycle through the play hands and deal() a card to each until they have the requisite number of cards - which is what I showed above.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.