hello again everyone
to pick up where i left off, i took the advice of a member and split my poker game up into different classes which i will post below
1)i need help with two errors in the poker class which i have commented out as compiler error messages
2)is there anyway i could make these 6 classes into two classes where the base class is the card class with the other classes and the derived class is the poker class
3)can someone help me figure out how i can make the game for 2 to four players
thank you so much for your help
public class Card
{
Suit suit;
Rank rank;
public Card(Suit s, Rank r)
{
suit = s;
rank = r;
discarded = false;
}
public Card(Card c)
{
suit = c.suit;
rank = c.rank;
discarded = false;
}
private boolean discarded;
public String toString()
{
return rank.toString() +" of "
+ suit.toString() + " ";
}
public Suit getSuit()
{
return suit;
}
public Rank getRank()
{
return rank;
}
public boolean isDiscarded()
{
return discarded;
}
public void setDiscarded(boolean value)
{
discarded = value;
}
}
public class Rank
{
int p;
Rank(int i)
{
p = i;
}
public String toString()
{
if (p > 1 && p < 11)
return String.valueOf(p);
else if (p == 1)
return "Ace";
else if (p == 11)
return "Jack";
else if (p == 12)
return "Queen";
else if (p == 13)
return "King";
else
return "error";
}
public int getValue()
{
return p;
}
}
public class Suit
{
public static final int CLUBS = 1;
public static final int DIAMONDS = 2;
public static final int HEARTS = 3;
public static final int SPADES = 4;
int suitValue;
Suit(int i)
{
suitValue = i;
}
public String toString()
{
switch (suitValue)
{
case CLUBS:
return "Clubs";
case DIAMONDS:
return "Diamonds";
case HEARTS:
return "Hearts";
case SPADES:
return "Spades";
default:
return "error";
}
}
}
public class Deck //a deck of playing cards
{
Card[] deck;
private int cardsUsed;
public Deck()
{
deck = new Card[52];
for (int i = 0; i < deck.length; i++)
{
deck[i] = new Card(new Suit(i / 13 + 1),
new Rank(i % 13 + 1));
cardsUsed = 0;
}
}
public void shuffle()
{
for (int i = 0; i < deck.length; i++)
{
int k = (int)(Math.random() * 52);
Card t = deck[i]; //Card t = new Card(deck[i]);
deck[i] = deck[k];
deck[k] = t;
}
for(int i = 0; i < deck.length; i++)
deck[i].setDiscarded(false);
cardsUsed = 0;
}
public int cardsLeft()
{
// As cards are dealt from the deck, the number of cards left
// decreases. This function returns the number of cards that
// are still left in the deck.
return 52 - cardsUsed;
}
public Card dealCard()
{
if (cardsUsed == 52)// Deals one card from the deck and returns it.
shuffle();// keeps cards from repeating
cardsUsed++;
return deck[cardsUsed - 1];
}
public String toString()
{
String t = "";
for (int i = 0; i < 52; i++)
{
if ( (i + 1) % 5 == 0)
t = t + deck[i] + "\n";
else
t = t + deck[i];
}
return t;
}
}
public class Hand
{
Card[] cards;
public Hand()
{
cards = new Card[5];
}
public void setCard(int i, Card c)
{
cards[i] = c;
}
public String toString()
{
String str = "";
for(int i = 0; i < cards.length; i++)
{
System.out.println((i+1) + " of " + cards[i]);
/*str += "\t" + (i+1) + ": ";
str += cards[i];*/
if(cards[i].isDiscarded() == true)
str += " DISCARDED";
str += "\n";
}
return str;
}
public boolean OnePair()
{
String[] values = new String[5];
int counter = 0;
//Put each cards numeric value into array
for(int i = 0; i < cards.length; i++)
{
values[i] = cards[i].getRank().toString();
}
//Loop through the values. Compare each value to all values
//If exactly two matches are made - return true
for(int row = 0; row < values.length; row++)
{
for(int col = 0; col < cards.length; col++)
{
if(values[row].equals(cards[col].getRank().toString()))
counter++;
if(counter == 2)
return true;
}
counter = 0;
}
return false;
}
public boolean TwoPair()
{
String[] values = new String[5];
int counter = 0;
int sum = 0;
for(int i = 0; i < cards.length; i++)
{
values[i] = cards[i].getRank().toString();
}
for(int x = 0; x < values.length; x++)
{
for(int y = 0; y < cards.length; y++)
{
if(values[x].equals(cards[y].getRank().toString()))
{
counter++;
}
}
if(counter > 1)
{
sum++;
counter = 0;
}
if(sum == 4)
return true;
}
return false;
}
public boolean ThreeOfAKind()
{
String[] values = new String[5];
int counter = 0;
for(int i = 0; i < cards.length; i++)
{
values[i] = cards[i].getRank().toString();
}
for(int row = 0; row < values.length; row++)//Same process as finding one pair, except return true for 3 matches
{
for(int col = 0; col < cards.length; col++)
{
if(values[row].equals(cards[col].getRank().toString()))
counter++;
if(counter == 3)
return true;
}
counter = 0;
}
return false;
}
public boolean Straight()
{
int[] values = new int[5];
int pos;
int temp;
//Set values in array
for(int i = 0; i < cards.length; i++)
{
values[i] = cards[i].getRank().getValue();
//If the card is an Ace
if(values[i] == 1)
values[i] = 14;
}
//Sort Numerically
for(int i = 1; i < values.length; i++)
{
pos = i;
while(pos != 0)
{
if(values[pos] < values[pos-1])
{
temp = values[pos];
values[pos] = values[pos-1];
values[pos-1]= temp;
}
pos--;
}
}
//Test for Straight
//Each sucessive card should be +1
for(int i = 0; i < values.length - 1; i++)
{
if(values[i] != values[i+1] - 1)
return false;
}
return true;
}
public boolean Flush()
{
String card = cards[0].getSuit().toString();//Store the suit of the first card
for(int i = 1; i < cards.length; i++) //Compared every other card's suit to that one
{ //Returns false if any suits do not match
if(!(cards[i].getSuit().toString().equals(card)))
return false;
}
return true;
}
public boolean StraightFlush()
{
if(Straight() == true && Flush() == true)//If theres a straight and a flush present
return true;
else
return false;
}
public boolean RoyalStraightFlush()//check to see if cards are royal straight flush
{
if(Flush() == false || Straight() == false)
return false;
int[] values = new int[5];
int pos;
int temp;
for(int i = 0; i < cards.length; i++)//Set values in array
{
values[i] = cards[i].getRank().getValue();
if(values[i] == 1)//If the card is an ace
values[i] = 14;
}
for(int i = 1; i < values.length; i++)//Sort the values numerically
{
pos = i;
while(pos != 0)
{
if(values[pos] < values[pos-1])
{
temp = values[pos];
values[pos] = values[pos-1];
values[pos-1]= temp;
}
pos--;
}
}
if(values[0] == 10)//Royal Straight flush is a straight flush, with the lowest card being a 10
return true;
return false;
}
public void discardCard(int index)
{
if(cards[index].isDiscarded() == false)
cards[index].setDiscarded(true);
else
cards[index].setDiscarded(false);
}
public void replaceDiscarded(Deck deck)
{
for(int i = 0; i < cards.length; i++)
{
if(cards[i].isDiscarded() == true)
setCard(i, deck.dealCard());
}
}
}
import java.util.Scanner;
public class Poker
{
private static Scanner kb = new Scanner(System.in);
private static Hand hand;
private static Deck deck;
public static void main(String[] args)
{
String handValue;
deck = new Deck();
hand = new Hand();
System.out.println("Let's play Poker!\n");
int x = 0;
while(x != -1)
{
deck.shuffle(); //Shuffle deck and deal the hand
for (int i = 0; i<5; i++)
hand.setCard(i, deck.dealCard());
System.out.println("Your Current Hand Is:");//Print Hand
System.out.println(hand);
doDiscard();//Discarding
System.out.println("\nYour Final Hand Is:");//Show final hand
System.out.println(hand);
System.out.println();
handValue = getHandValue();//Display hand value
System.out.println("Hand Value: " + handValue);
//Play Again?
System.out.println("Do you want to play again? Enter a -1 to quit or 1 to play again.");
int x = keyboard.nextInt();
}
private static String getHandValue()//compiler error message: illegal start of expression
{
if(hand.RoyalStraightFlush() == true)
return "Royal Straight Flush";
else if(hand.StraightFlush() == true)
return "Straight Flush";
else if(hand.Flush() == true)
return "Flush";
else if(hand.Straight() == true)
return "Straight";
else if(hand.ThreeOfAKind() == true)
return "Three of a Kind";
else if(hand.TwoPair() == true)
return "Two Pair";
else if(hand.OnePair() == true)
return "One Pair";
else
return "Nothing";
}
private static void doDiscard()
{
int discard = -1;
System.out.println("Enter the numbers of the cards you wish to discard.");
System.out.println("Entering the number of a discarded card retrieves it.");
System.out.println("Enter 0 to stop discarding.");
while(discard != 0)
{
discard = getDiscard();
if(discard == 0)
break;
hand.discardCard(discard - 1);
System.out.println(hand);
System.out.println("Select another card or 0 to complete the discard.");
}
hand.replaceDiscarded(deck);
}
private static int getDiscard()
{
String input;
int num = -1;
if(num < 0 || num > 5)
{
System.out.println("Error: invalid number.");
System.exit(0);
}
return num;
}
}
}//compiler error message: class, interface, or enum expected