Question

I'm currently writing a program based on stub poker and trying to deal cards to the...

I'm currently writing a program based on stub poker and trying to deal cards to the players, show their hands, and determine the winner, and lastly ask them to if they want to play another hand. I'm probably going to have to write a method to compare hands but i really need to just deal the cards and store in their hands Any help or suggestions? Here are my current classes.

public class PlayingCard {
   private final Suit suit;
   private final Rank rank;
   public PlayingCard(Rank newRank, Suit newSuit) {
       this.rank = newRank;
       this.suit = newSuit;
   }// Ending bracket of constructor
  
   public Rank getRank() {
       return this.rank;
   }// Ending bracket of method getRank
  
   public Suit getSuit() {
       return this.suit;
   }// Ending bracket of method getSuit
  
   public String toString() {
       return this.rank.getRank() + " of " + this.suit.getSuit();
   }// Ending bracket of method toString
  
   public String toStringSymbol() {
       return this.rank.getSymbol() + "" + this.suit.getSymbol();
   }// Ending bracket of method toString
  
   public int getRankValue() {
       return this.rank.ordinal() + 2;
   }// Ending bracket of method getRankVaule
   //So deuce will return a rank value of 2 seeing how deuce is in the position of 0.
  
   public boolean isSameSuit(PlayingCard otherCard) {
       return(this.getSuit() == otherCard.getSuit());
   }// Ending bracket of method isSameSuit
  
   public boolean isSameRank(PlayingCard otherCard) {
       return(this.getRank() == otherCard.getRank());
   }// Ending bracket of method isSameRank

}// Ending bracket of class PlayingCard

--------------------------------------------------------------

public enum Rank {
  
   DEUCE("Deuce", '2'), TREY("Trey", '3'), FOUR("Four", '4'),
   FIVE("Five", '5'), SIX("Six", '6'), SEVEN("Seven", '7'),
   EIGHT("Eight", '8'), NINE("Nine", '9'), TEN("Ten", 'T'),
   JACK("Jack", 'J'), QUEEN("Queen", 'Q'), KING("King", 'K'),
   ACE("Ace", 'A');
  
   private String rank;
   private char symbol;
  
   private Rank(String newRank, char newSymbol) {
       this.rank = newRank;
       this.symbol = newSymbol;
   }// Enidng bracket of constructor
  
   public String getRank() {
       return this.rank;
   }// Ending bracket of method getRank;
  
   public char getSymbol() {
       return this.symbol;
   }

}// Ending bracket of class Rank

-------------------------------------------

public enum Suit {
   HEARTS("Hearts", '\u2665'), DIAMONDS("Diamonds", '\u2666'),
   CLUBS("Clubs", '\u2663'), SPADES("Spades", '\u2660');
  
   private String suit;
   private char symbol;
  
   private Suit(String newSuit, char newSymbol) {
       this.suit = newSuit;
       this.symbol = newSymbol;
   }// Ending bracket of constructor
  
   public String getSuit() {
       return this.suit;
   }// Ending bracket of method
  
   public char getSymbol() {
       return this.symbol;
   }// Ending bracket of getSymbol

}// Ending bracket of enum Suit
----------------------------------------------------

import java.util.Random;

public class Deck {
  
   private PlayingCard[] cards;
   private static final int DECK_SIZE = 52;
   private Random randomizer;
  
   public Deck() {
       int arrayIndex = 0;
       this.cards = new PlayingCard[DECK_SIZE];
       this.randomizer = new Random();
      
       for(Suit suit : Suit.values()) {
           for(Rank rank : Rank.values()) {
               cards[arrayIndex++] = new PlayingCard(rank, suit);
           }//Ending bracket of inner for each loop
       }// Ending bracket of outer for each loop
   }// Ending bracket of Deck constructor
  
   public PlayingCard deal() {
       PlayingCard rv = null;
       int index = 0;
      
       do {
           index = randomizer.nextInt(DECK_SIZE);
           rv = cards[index];
       } while (rv == null);
       cards[index] = null;
       return rv;
   }// Ending bracket of PlayingCard deal method
  
   public void displayDeck() {
       for(PlayingCard card : cards) {
           System.out.println(card.toString());
       }// Ending bracket of for each loop
   }// Ending bracket of displayDeck method
  
   public void displayDeckSymbol() {
       for(PlayingCard card : cards) {
           System.out.println(card.toStringSymbol());
       }// Ending bracket of for each loop
   }// Ending bracket of displayDeck method

}// Ending bracket of class Deck

--------------------------------------------

public class Hand {

   private PlayingCard[] cards;
   private int currentNumberOfCards;
   private static final String COMMA_SPACE = ", ";
  
   public Hand(int numberOfCardsToHold) {
       this.cards = new PlayingCard[numberOfCardsToHold];
       this.currentNumberOfCards = 0;
   }// Ending bracket of constructor
  
   public void sort() {
       boolean isNotSorted = true;//sentinal value -
       PlayingCard tempCard = null;// use null if you do not have an object
       while(isNotSorted) {
           isNotSorted = false;
           for(int i = 0; i < this.cards.length - 1; ++i) {
               if(this.cards[i].getRankValue() < this.cards[i + 1].getRankValue()) {
                   tempCard = this.cards[i];
                   this.cards[i] = this.cards[i + 1];
                   this.cards[i + 1] = tempCard;
                   isNotSorted = true;
               }// Ending bracket of if
           }// Ending bracket of for
       }// Ending bracket of while
   }// Ending bracket of method sort
  
   public void addCard(PlayingCard newCard) {
       this.cards[this.currentNumberOfCards] = newCard;// the assigning a card the the array cards
       this.currentNumberOfCards++;
   }// Ending bracket of addCard
  
   public String toString() {
       StringBuffer sb = new StringBuffer();// to add string without extra memory
       for (PlayingCard card : this.cards) {
           sb.append(card.toString());// append means to add to the end
           sb.append(COMMA_SPACE);      
       }// Ending bracket of for each loop - Cards
       return sb.toString();//
   }// Ending bracket of method toString
  
   public String toStringSymbol() {
       StringBuffer sb = new StringBuffer();// to add string without extra memory
       for (PlayingCard card : this.cards) {
           sb.append(card.toString());
           sb.append(COMMA_SPACE);      
       }// Ending bracket of for loop
       return sb.toString();//
   }// Ending bracket of method toString

}// Ending bracket of class Hand

------------------------------------------------------

public class PokerPlayer {
   private String name;
   private Hand hand;
  
   public PokerPlayer(String newName, int numberOfCards) {
       this.setName(newName);
       this.setHand(new Hand(numberOfCards));
   }// Ending bracket of constructor
  
   public void addCard(PlayingCard newCard) {
       this.hand.addCard(newCard);;
   }// Ending bracket of method addCard
  
   public String getName() {
       return this.name;
   }// Ending bracket of method getNasme
  
   public void setName(String newName) {
       this.name = newName;
   }// Ending bracket of method setName
  
   public Hand getHand() {// We are breaking encapsulation doing this.
       return this.hand;
   }// Ending bracket of method getHand
  
   public void setHand(Hand newHand) {
       this.hand = newHand;
   }// Ending bracket of setHand

0 0
Add a comment Improve this question Transcribed image text
Answer #1

card.java

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

Rank.java

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

Suit.java

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

Deck.java

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

Hand.java

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

Poker.java

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

Add a comment
Know the answer?
Add Answer to:
I'm currently writing a program based on stub poker and trying to deal cards to the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java Write a complete program that implements the functionality of a deck of cards. In writing...

    Java Write a complete program that implements the functionality of a deck of cards. In writing your program, use the provided DeckDriver and Card classes shown below. Write your own Deck class so that it works in conjunction with the two given classes. Use anonymous objects where appropriate. Deck class details: Use an ArrayList to store Card objects. Deck constructor: The Deck constructor should initialize your ArrayList with the 52 cards found in a standard deck. Each card is a...

  • HELP NEED!! Can some modified the program Below in C++ So that it can do what...

    HELP NEED!! Can some modified the program Below in C++ So that it can do what the Problem below asked it today???? #include <iostream> #include <stdlib.h> #include <string> #include <time.h> /* time */ #include <sstream> // for ostringstream using namespace std; // PlayingCard class class PlayingCard{ int numRank; int numSuit; string rank; string suit; public: PlayingCard(); PlayingCard(int numRank,int numSuit); void setRankString(); void setSuitString(); void setRank(int numRank); void setSuit(int numSuit); string getRank(); string getSuit(); int getRankNum(); int getSuitNum(); string getCard(); };...

  • need help with code it won't compile. import java.util.ArrayList; /** * Counts the ranks of cards...

    need help with code it won't compile. import java.util.ArrayList; /** * Counts the ranks of cards in a hand. * * @author (your name) * @version (a version number or a date) */ public class CountRank { // instance variables - replace the example below with your own private int rankCount[]; private Hand hand; private int count; /** * Constructor for objects of class CountRank */ public CountRank(Hand h) { // initialise instance variables hand = h; rankCount = new...

  • HELP NEED!! Can some modified the program Below in C++ So that it can do what the Problem below asked it today???? #include <iostream> #include <stdlib.h> #include <string> #include...

    HELP NEED!! Can some modified the program Below in C++ So that it can do what the Problem below asked it today???? #include <iostream> #include <stdlib.h> #include <string> #include <time.h> /* time */ #include <sstream> // for ostringstream using namespace std; // PlayingCard class class PlayingCard{ int numRank; int numSuit; string rank; string suit; public: PlayingCard(); PlayingCard(int numRank,int numSuit); void setRankString(); void setSuitString(); void setRank(int numRank); void setSuit(int numSuit); string getRank(); string getSuit(); int getRankNum(); int getSuitNum(); string getCard(); };...

  • //main.cpp #include <iostream> #include <iomanip> #include "deck-of-cards.hpp" void RunAllTests() { int count; std::cin >> count; DeckOfCards...

    //main.cpp #include <iostream> #include <iomanip> #include "deck-of-cards.hpp" void RunAllTests() { int count; std::cin >> count; DeckOfCards myDeckOfCards; for (int i = 0; myDeckOfCards.moreCards() && i < count; ++i) { std::cout << std::left << std::setw(19) << myDeckOfCards.dealCard().toString(); if (i % 4 == 3) std::cout << std::endl; } } int main() { RunAllTests(); return 0; } //card.hpp #ifndef CARD_HPP_ #define CARD_HPP_ #include <string> class Card { public: static const int totalFaces = 13; static const int totalSuits = 4; Card(int cardFace, int...

  • I've created a Card class and I'm asked to implement a class called DeckOfCards that stores...

    I've created a Card class and I'm asked to implement a class called DeckOfCards that stores 52 objects of the Card class. It says to include methods to shuffle the deck, deal a card, and report the number of cards left in the deck, and a toString to show the contents of the deck. The shuffle methods should assume a full deck. I also need to create a separate driver class that first outputs the populated deck to prove it...

  • Given these three classes: Card, DeckOfCards, and DeckOfCardsTest. Extend the DeckofCards class to implement a BlackJack...

    Given these three classes: Card, DeckOfCards, and DeckOfCardsTest. Extend the DeckofCards class to implement a BlackJack class, which implements a BlackJack game. Please do not use any java applet on the coding. Hint: Use a test class to test above classes. Pulic class Card {    private final String face; // face of card ("Ace", "Deuce", ...)    private final String suit; // suit of card ("Hearts", "Diamonds", ...)    // two-argument constructor initializes card's face and suit    public...

  • NEED HELP TO CREATE A BLACKJACK GAME WITH THE UML DIAGRAM AND PROBLEM SOLVING TO GET...

    NEED HELP TO CREATE A BLACKJACK GAME WITH THE UML DIAGRAM AND PROBLEM SOLVING TO GET CODE TO RUN!! THANKS Extend the DeckofCards and the Card class in the book to implement a card game application such as BlackJack, Texas poker or others. Your game should support multiple players (up to 5 for BlackJack). You must build your game based on the Cards and DeckofCards class from the book. You need to implement the logic of the game. You can...

  • Here is a sample run of the tester program: Make sure your program follows the Java...

    Here is a sample run of the tester program: Make sure your program follows the Java Coding Guidelines. Consider the following class: /** * A store superclass with a Name and Sales Tax Rate */ public class Store {      public final double SALES_TAX_RATE = 0.06;      private String name;           /**      * Constructor to create a new store      * @param newName      */      public Store(String newName)      {           name = newName;      }     ...

  • Deck of Cards Program I need help printing a flush, which is showing the top 5...

    Deck of Cards Program I need help printing a flush, which is showing the top 5 cards of the same suite. Below is the code I already have that answers other objectives, such as dealing the cards, and finding pairs. Towards the end I have attempted printing a flush, but I cannot figure it out. public class Shuffler {    /**    * The number of consecutive shuffle steps to be performed in each call    * to each sorting...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT