splitting a deck into two separate hands

implementing a class for each players hand

Page 1 of 1

9 Replies - 2759 Views - Last Post: 13 February 2009 - 05:09 PM Rate Topic: -----

#1 hezfast2  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 19-May 08

splitting a deck into two separate hands

Post icon  Posted 26 May 2008 - 12:37 PM

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 Hands class. My code is in components, I have a Deck, Card, Game, War (main) classes. I have the Game set up to use a playerAHand and a playerBHand in a Hands class, 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;
	 } 
}


This post has been edited by hezfast2: 26 May 2008 - 12:46 PM


Is This A Good Question/Topic? 0
  • +

Replies To: splitting a deck into two separate hands

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: splitting a deck into two separate hands

Posted 26 May 2008 - 12:53 PM

View Posthezfast2, on 26 May, 2008 - 12:37 PM, said:

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, one class with half the deck for player A and one class with the other half for player B. My code is in components, I have a Deck, Card, Game, War (main) classes. I have the Game set up to use a playerAHand class and a playerBHand class, but I'm having a hard time wrapping my head around creating those classes with the deck. 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.


Don't see why you would need to classes for playerAHand and playerBHand

just have a class Hand and make

Hand playerA = new Hand(list1);
Hand playerB = new Hand(list2);

also your switch() take a lot of room for nothing better to

class Card {
	static final String[] representation = {"", "", "2", "3",....,"Queen", "King", "Ace"};
	static final String[] color = {"", "Hart", "Diamond", "Spade", "Clubs"};

	public String toString() {
		 return representation[rank] + " of " + color[suite];
	}


This post has been edited by pbl: 26 May 2008 - 12:54 PM

Was This Post Helpful? 0
  • +
  • -

#3 hezfast2  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 19-May 08

Re: splitting a deck into two separate hands

Posted 26 May 2008 - 12:57 PM

View Postpbl, on 26 May, 2008 - 12:53 PM, said:

View Posthezfast2, on 26 May, 2008 - 12:37 PM, said:

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, one class with half the deck for player A and one class with the other half for player B. My code is in components, I have a Deck, Card, Game, War (main) classes. I have the Game set up to use a playerAHand class and a playerBHand class, but I'm having a hard time wrapping my head around creating those classes with the deck. 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.


Don't see why you would need to classes for playerAHand and playerBHand

just have a class Hand and make

Hand playerA = new Hand(list1);
Hand playerB = new Hand(list2);

also your switch() take a lot of room for nothing better to

class Card {
	static final String[] representation = {"", "", "2", "3",....,"Queen", "King", "Ace"};
	static final String[] color = {"", "Hart", "Diamond", "Spade", "Clubs"};

	public String toString() {
		 return representation[rank] + " of " + color[suite];
	}



I will try your suggestion, I used switch blocks because I cannot use static methods for this project (It was the only way I could think of to do it).
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: splitting a deck into two separate hands

Posted 26 May 2008 - 01:03 PM

View Posthezfast2, on 26 May, 2008 - 12:57 PM, said:

I will try your suggestion, I used switch blocks because I cannot use static methods for this project (It was the only way I could think of to do it).


There are no static method.
Two static array of String thow. But nothing forces you to make them static. Remove the static keyword. You will just have an instance of all String in every card.

If you can't have static method How will you start your game ? main() is a static method

This post has been edited by pbl: 26 May 2008 - 01:04 PM

Was This Post Helpful? 0
  • +
  • -

#5 hezfast2  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 19-May 08

Re: splitting a deck into two separate hands

Posted 26 May 2008 - 01:16 PM

View Postpbl, on 26 May, 2008 - 01:03 PM, said:

View Posthezfast2, on 26 May, 2008 - 12:57 PM, said:

I will try your suggestion, I used switch blocks because I cannot use static methods for this project (It was the only way I could think of to do it).


There are no static method.
Two static array of String thow. But nothing forces you to make them static. Remove the static keyword. You will just have an instance of all String in every card.

If you can't have static method How will you start your game ? main() is a static method


we cannot use static methods other than the main method, sorry I wasn't clear on that. I originally created a much shorter method, but ended up using the switch not realizing that I didn't have to use the static modifier
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: splitting a deck into two separate hands

Posted 26 May 2008 - 01:27 PM

By the way your code should be

		 if (handNum == 1)
			   tempHand = deck.subList(0, 25);
		  if (handNum == 2)
			   tempHand = deck.subList(26, 51);



not

		 if (handNum == 1)
			   tempHand = deck.subList(0, 26);
		  if (handNum == 2)
			   tempHand = deck.subList(26, 52);
		


Was This Post Helpful? 0
  • +
  • -

#7 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5882
  • View blog
  • Posts: 12,760
  • Joined: 16-October 07

Re: splitting a deck into two separate hands

Posted 26 May 2008 - 04:12 PM

First, lets look at your Deck class. What is the point of this class and what are you going to do with it? More to the point, what is a "deck" anyway?

A deck of cards is really just a collection of one or more cards. So first, it is a programmatic collection. What does it do beyond that? It can shuffle, it can have a complete set of cards, it can deal. Why don't you have a "deal" method? Also, a player's hand is their personal deck, no sense in making something more until we figure out more properties a hand might have.


Here's a deck class:

class Deck extends LinkedList<Card> {
	public Deck() { }
	
	// sometimes you want to fill the deck, sometimes you dont
	public void initFullDeck() {
		this.clear();
		for (int suit = 1; suit <= 4; suit++) {
			for (int rank = 2; rank <= 14; rank++) {
				this.add( new Card(rank, suit) );
			}
		}
	}
	
	public void shuffle() {
		// if you have to call this canned method multiple times
		// to get what you want, write your own.
		// also, this is a static method ;)/>
		Collections.shuffle(this);
	}
	
	// how the hell can you have a "Deck" class 
	// without a "deal" method
	public Card deal() {
		if(this.isEmpty()) { return null; }
		return this.removeFirst();
	}
 
	public String toString() {
		String s = "";
		for(Card card : this) {
			if (s!="") { s += ", "; }
			s += card.toString();
		}
		return s;
	}
	
	// this doesn't really belong here
	//public List<Card> splitDeck(int handNum)
}



The whole splitDeck idea doesn't make sense to a deck, it just holds the cards. You'd deplete the deck with a deal method. Here's what a Game class might look like:

class Game {
	private Deck[] players;
	private Deck deck;
	
	public Game(int playerCount) {
		this.players = new Deck[playerCount];
		for(int i=0; i<this.players.length; i++) {
			this.players[i] = new Deck();
		}
		this.deck = new Deck();
		this.start();
	}
	
	public void start() {
		this.deck.initFullDeck();
		this.deck.shuffle();
		for(Deck player : this.players) {
			player.clear();
		}
	}
	
	public void dealToPlayers(int cardCount) {
		for(int i=0; i<cardCount; i++) {
			for(Deck player : this.players) {
				player.add( this.deck.deal() );
			}
		}
	}
}



Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: splitting a deck into two separate hands

Posted 26 May 2008 - 04:21 PM

When baavgay talks/writes I stay wordless
Was This Post Helpful? 0
  • +
  • -

#9 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: splitting a deck into two separate hands

Posted 26 May 2008 - 05:11 PM

View Postpbl, on 26 May, 2008 - 04:21 PM, said:

When baavgay talks/writes I stay wordless


Agreed...Mostly because I'm afraid I'd confuse people.
Was This Post Helpful? 0
  • +
  • -

#10 confuzzeld  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 30-September 08

Re: splitting a deck into two separate hands

Posted 13 February 2009 - 05:09 PM

ok im doing the same sort of project with a few minor add ons
i have to have "minimum" 3 classes Deck, Card and void Main
within the deck class i have to have 5 different methods

dealHands()
shuffle()
topCard()
cutDeck()
and reset()

however i have encountered a problem with dealHands and cutDeck
this is what i have in my Deck Class:

import javax.swing.JOptionPane;
import java.util.*;

public class Deck
{
		private static Random r = new Random();
		ArrayList <Card> cards = new ArrayList <Card>();
		Scanner kbd = new Scanner(System.in);
		
		public Deck() 
		{
			createCards();
			for(Card c: cards) 
			{
//				System.out.println(c.toString());
			}

		}
		
		public void createCards()
		{
			for (int i = 0; i < 13; i++) 
			{
				cards.add(new Card(i+1, Card.SPADES));
			}
			for (int i = 0; i < 13; i++) 
			{
				cards.add(new Card(i+1, Card.HEARTS));
			}
			for (int i = 0; i < 13; i++) 
			{
				cards.add(new Card(i+1, Card.CLUBS));
			}
			for (int i = 0; i < 13; i++) 
			{
				cards.add(new Card(i+1, Card.DIAMONDS));
			}
		}
		
		public void printCard(Card c)
		{
			
		}
		public void printDeck()
		{
			for(Card c: cards) 
			{
				System.out.println(c.toString());
			}
		}
		
		public void reset()
		{
			cards.clear();
			createCards();
			printDeck();
		}
		public void shuffle()
		{
			Collections.shuffle(cards);
			System.out.println(cards);
		}

		public Card drawTopCard()
		{
			return cards.remove(0);
		}

		public void cutDeck()
		{
			 int temp = r.nextInt(51);
			 int count = 0;
			 for(int i = temp; i < 51; i++)
			 {
				 cards.add(0 + count, cards.remove(i));
				 count++;
			 }

		}
		/**
		 * Prints an error message if there is a invalid input of number of cards for a hand
		 */
		public static void invalidCardError()
		{
			JOptionPane.showMessageDialog(null, "Please enter in valid Number of Cards, Must be within 1 - 26", "Invalid number Error", JOptionPane.ERROR_MESSAGE);
		}
		/**
		 * Prompts user to enter in Number of Cards to be dealt
		 * 
		 */
		public void deal() 
		{
			String howMany = "";
			
			howMany = JOptionPane.showInputDialog("How many cards would you like? ");
			try{int number = Integer.parseInt(howMany);
			}
			catch(Exception e){invalidCardRead()}
			
			if(!number > 0 && number < 52)
			{
				invalidCardError();
				howMany = JOptionPane.showInputDialog("How many cards would you like? ");
			}
			else
						   
			for(int i = 0; i < number; i++)
			{
				drawTopCard();
			}
			
		}


}



this is my main class:


import javax.swing.JOptionPane;
import java.util.*;
/**
 * Write a description of class mainMenu here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MainProgram
{
	public static String welcomeMessage()
	{
		String input = "";				
		input = JOptionPane.showInputDialog("Welcome To The Card Game \n Please enter you choice: \n N - New Deck\n S - Shuffle Deck\n T - Top Card\n C - Cut the Deck\n D - Deal Hands\n Q - Quit");
		return input;
	}
	public static void main(String[] args)
	{
		String input = "";
		Deck deck = new Deck();
		input = welcomeMessage();

		if(input.equalsIgnoreCase("N"))
		{
			deck.reset();
		}
		else if (input.equalsIgnoreCase("S"))
		{
			deck.shuffle();
		}
		else if (input.equalsIgnoreCase("T"))
		{
		   deck.drawTopCard();
		}
		else if (input.equalsIgnoreCase("C"))
		{
		   deck.cutDeck();
		}
		else if (input.equalsIgnoreCase("D"))
		{
			String howMany = "";			
			howMany = JOptionPane.showInputDialog("How many cards would you like? ");
			try{int number = Integer.parseInt(howMany);
				deck.deal(number);
			}
			catch(Exception e){deck.invalidCardRead();}
		}

		}
		else if (input.equalsIgnoreCase("Q"))
		{
			
		}
		else
			input = JOptionPane.showInputDialog("Welcome To The Card Game \n Please enter you choice: \n N - New Deck\n S - Shuffle Deck\n T - Top Card\n C - Cut the Deck\n D - Deal Hands\n Q - Quit");
			
	}
	public static void isThereAnythingElse()
	{
		int selection = JOptionPane.showOptionDialog(null, "Is there anything else you would like to do?", JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_OPTION);
		if (selection == JOptionPane.YES_OPTION)
		{
			welcomeMessage();
		}
		else{
			System.exit(0);
		}
	}
}

for the deal() method, i know that i shouldnt have user inputs in and its been changed in my main class to do all the processing
im having troubles doing a try/catch statement right now

any ideas?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1