Jump to content

Game

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1
hoku2000_99

hoku2000_99

    Newbie

  • Members
  • PipPip
  • 24 posts
I need help making this poker game. I am mainly stuck on using a loop to switch at least 999999 pairs of cards (I thought it would be something a long the lines of while(pairs <= 999999)). Then when I deal my cards, I am not too sure how to make an instance variable of the DeckOfCards that will track where someone is at in the deck. Also, how would I deal two five card playing hands?

All help is appreciate.

public class Card
{

public final static int ACE = 1;
public final static int TWO = 2;
public final static int THREE = 3;
public final static int FOUR = 4;
public final static int FIVE = 5;
public final static int SIX = 6;
public final static int SEVEN = 7;
public final static int EIGHT = 8;
public final static int NINE = 9;
public final static int TEN = 10;
public final static int JACK = 11;
public final static int QUEEN = 12;
public final static int KING = 13;

public final static int CLUBS = 1;
public final static int DIAMONDS = 2;
public final static int HEARTS = 3;
public final static int SPADES = 4;

private final static int NUM_FACES = 13;
private final static int NUM_SUITS = 4;

private int face, suit;
private String faceName, suitName;


public Card ()
{
face = (int) (Math.random() * NUM_FACES) + 1;
setFaceName();

suit = (int) (Math.random() * NUM_SUITS) + 1;
setSuitName();
}


public Card (int faceValue, int suitValue)
{
face = faceValue;
setFaceName();

suit = suitValue;
setSuitName();
}


private void setFaceName()
{
switch (face)
{
case ACE:
faceName = "Ace";
break;
case TWO:
faceName = "Two";
break;
case THREE:
faceName = "Three";
break;
case FOUR:
faceName = "Four";
break;
case FIVE:
faceName = "Five";
break;
case SIX:
faceName = "Six";
break;
case SEVEN:
faceName = "Seven";
break;
case EIGHT:
faceName = "Eight";
break;
case NINE:
faceName = "Nine";
break;
case TEN:
faceName = "Ten";
break;
case JACK:
faceName = "Jack";
break;
case QUEEN:
faceName = "Queen";
break;
case KING:
faceName = "King";
break;
}
}


private void setSuitName()
{
switch (suit)
{
case CLUBS:
suitName = "Clubs";
break;
case DIAMONDS:
suitName = "Diamonds";
break;
case HEARTS:
suitName = "Hearts";
break;
case SPADES:
suitName = "Spades";
break;
}
}


public boolean isHigherThan (Card card2, boolean aceHigh)
{
boolean result = false;

if (face == card2.getFace())
{
if (suit > card2.getSuit())
result = true;
}
else
{
if (aceHigh && face == ACE)
result = true;
else
if (face > card2.getFace())
result = true;
}

return result;
}


public boolean isHigherThan (Card card2)
{
return isHigherThan (card2, true);
}


public int getFace ()
{
return face;
}
public int getSuit ()
{
return suit;
}

public String getFaceName ()
{
return faceName;
}


public String getSuitName ()
{
return suitName;
}


public String toString ()
{
return faceName + " of " + suitName;
}
}

import java.util.Random;

public class DeckOfCards
{
private Card [] deck ;
private int cardsDelt=0;

public DeckOfCards ()
{
deck=new Card[52];
int cardCount = 0;

for ( int suit = 0; suit <= 3; suit++ )
{
for ( int value = 0; value <= 12; value++ )
{

deck[cardCount] = new Card(value,suit);
cardCount++;
cardsDelt = 0;

}
}

}

public void shuffle()
{

for ( int index = 51; index > 0; index-- )
{
int rand = (int)(Math.random()*(index+1));
Card temp = deck[index];
deck[index] = deck[rand];
deck[rand] = temp;

cardsDelt = 0;

}

}

public Card dealCard()
{

if (cardsDelt == 52)
shuffle();
cardsDelt++;
return deck[cardsDelt – 1];
}


public int cardsLeft()
{

return 52 – cardsDelt;
}
public String toString()
{
return cardsLeft () + "cards left in the deck";
}

}

import javax.swing.*;

public class DeckOfCardsDriver
{

public static void main (String[] args)
{
Card obj;
DeckOfCards cards = new DeckOfCards ();

cards.shuffle();
cards.dealCard();

for ( int index = 0; index < 12; index++ )
{

System.out.println(cards.dealCard() + "\n" + "\n" + cards.dealCard());

}
}
}

When I run it for example some will say "Ace of null" how would I fix that?

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
I would change your model. Have the numbers 0-51 represent your cards. Face value = number % 13 + 1, suit = number / 13 (0=heart, 1=diamond, 2=club, 3=spade). Have a list of these value and pull one out of the list and into a hand list at random to deal a card.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog