C++ We endeavor to set up some classes that can be used in future programs that
ID: 3820728 • Letter: C
Question
C++
We endeavor to set up some classes that can be used in future programs that involve playing card games with a human or simulating card games entirely by a computer. There are two basic classes we'll need this week:
Card: A class like the one presented in the modules, but with a few changes.
Hand: A class that represents the cards held by a single player.
Notice that I am using the char 'T' to describe the value 10. (Ignore the Joker, which we will not need.)
The dealer uses a Deck object (not implemented this week) to deal Hand objects to the players. The dealer may or may not be a player who gets a hand of his own (poker dealers in casinos don't receive a hand, but most other games involve the dealer getting a hand).
Card: The Card class has two members: value (a char) and suit (an enum). But we add a new bool, errorFlag, which can inform a client that a card is in an illegal state. We'll want the usual constructors, mutators, accessors and toString()methods for the class. We only allow standard cards, like ('A', clubs), ('9', hearts) and ('T', diamonds), no jokers or other special cards.
Hand: As you can see, a Hand object usually contains several cards, so we'll need an array of Card objects (myCards) as the principal member of the Hand class. Since each game deals a different number of cards into its players hands, and even within a game the number of cards in a hand will increase or decrease, we must keep track of this with an intvalue (numCards). We'll need constructors, mutators, etc., of course. We'll also want a way for the hand to receive a card (from the deck or somewhere else), and play a card (to the table or to another player). These two methods will be called takeCard() and playCard(), respectively. Since this class has no information about the game being played, it always puts new cards received by takeCard() into the next available location of the array (index position numCards) and plays a card via playCard() from the highest occupied location (index position numCards - 1). The client game application would somehow prepare this highest position with the correct card to be played before calling Hand'splayCard() method. This detail is not our concern.
Phase 1: The Card Class
A Public enum Type
Define the Suit enum, { clubs, diamonds, hearts, spades }, inside the Card class prototype.
Private Member Data
Include three members:
Public Methods
Card(char value = 'A', Suit suit = spades) - The constructor should call the proper mutator(s). Because we have the errorFlag member, the constructor (via the mutator), can set this flag to true when it gets bad data; it does not have to assign default values upon receipt of bad data. This is a new technique for us. The default card (no parameters passed) is the ('A', spades).
string toString() - a stringizer that the client can use prior to displaying the card. It provides a clean representation of the card. If errorFlag == true, it should return correspondingly reasonable reflection of this fact (something like "[ invalid ]" rather than a suit and value).
bool set(char value, Suit suit) - a mutator that accepts the legal values established in the earlier section. When bad values are passed, errorFlag is set to true and other values can be left in any state (even partially set). If good values are passed, they are stored and errorFlag is set to false. Make use of the private helper, listed below.
No mutator for errorFlag - that would not make sense.
Accessors for suit, value and errorFlag.
bool equals(Card card) - returns true if all the fields (members) are equal and false, otherwise.
Private Methods
static bool isValid(char value, Suit suit) - a static helper method that returns true or false, depending on the legality of the parameters. Note that, although it may be impossible for suit to be illegal (due to its enum-ness), we pass it, anyway, in anticipation of possible changes to the type from enum to, say, char or int, someday. We only need to test value, at this time.
Note: we don't need individual mutators for value or suit since they would not be useful for this particular class.
Recommended test of Card class
Instantiate at least three cards, one of them, passing illegal values. Display all three. Then make good card bad using set() with an illegal value, and turn the bad card "good" by setting a legal value, and re-display. This is a simple test, but you can test more thoroughly if you wish.
Example Test Run of Card Class
Phase 2: The Hand Class
Static Class Constants
Define a const public int value like MAX_CARDS and set it to something like 30 or 50 so a runaway program can't try to create a monster array.
Private Member Data
Public Methods
Hand() - a default constructor.
void resetHand() - remove all cards from the hand (in the simplest way).
bool takeCard(Card card) - adds card to the next available position in the myCards array if the parameter is an error-free Card object and if there is room in the Hand object for another card (according to MAX_CARDS). It returns false if the Hand was full and true otherwise, even if card was an invalid (error-containing) Card. So, if card is invalid but there would have been room for it, the method will return true, even though it did not add card to the hand.
Card playCard() - returns and removes (effectively, not physically) the card in the top occupied position of the array.
string toString() - a stringizer that the client can use prior to displaying the entire hand.
Accessor for numCards.
Card inspectCard(int k) - Accessor for an individual card. Returns a card with errorFlag = true if k is bad.
Recommended test of Hand class
Create between two and five explicit Card objects and one Hand object. Use takeCard() on these few cards (resulting in many, unavoidable "duplicates" in the hand) in a loop to populate the hand until the maximum allowable cards is met (use this criterion to end the loop). Display the hand using toString(). Next, play each card in a loop, until the hand is empty. Display the card played as it is played, and finally, display the (now empty) hand, verifying that no cards remain. At some point in your program, test inspectCard() with both legal and illegal int arguments.
Example Test Run of Hand Class
c ar BICYCLE USAnsadaoCo JOKER 3 & N AExplanation / Answer
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package May;
/**
*
* @author paramesh
*/
import java.util.*;
public class Card implements Comparable<Card> {
private int value;
private String suit;
// constructor
public Card(String val, String _suit) {
if (val.equals("2") || val.equals("3") || val.equals("4") || val.equals("5") || val.equals("6") || val.equals("7") || val.equals("8") || val.equals("9") || val.equals("10") || val.equals("11") || val.equals("12") || val.equals("13") || val.equals("14")) {
value = Integer.parseInt(val);
} else {
if (val.equals("J")) {
value = 11;
} else if (val.equals("Q")) {
value = 12;
} else if (val.equals("K")) {
value = 13;
} else if (val.equals("A")) {
value = 14;
}
}
suit = _suit;
}
// value getter method
public String value() {
String res = "";
// check card is number
if (value >= 2 && value <= 10) {
res = Integer.toString(value);
} // scheck it is face card?
else if (value >= 11 && value <= 14) {
if (value == 11) {
res = "J";
} else if (value == 12) {
res = "Q";
} else if (value == 13) {
res = "K";
} else if (value == 14) {
res = "A";
} else {
res = "INVALID";
}
}
return res;
}
// value getter method
public int getValue() {
return value;
}
//suit getter method
public String suit() {
return suit;
}
// check card equal or not
public boolean equals(Card card) {
boolean res = false;
if (this.getValue() == card.getValue()) {
res = true;
}
return res;
}
// comparing cards
public int compareTo(Card card) {
int res = 0;
if (this.getValue() > card.getValue()) {
res = 1;
} else if (card.getValue() > this.getValue()) {
res = -1;
}
return res;
}
public String toString() {
return value() + " of " + suit();
}
}
-------------------------------------------------------------------------------------------------------------------------------------
import java.util.*;
public class Hand implements Comparable<Hand> {
private ArrayList<Card> myCards;
private int[] values;
private String[] suits;
private int numCards = 0;
// constructor
public Hand(ArrayList<Card> cards) {
myCards = cards;
values = values();
suits = suits();
Arrays.sort(values);
Arrays.sort(suits);
numCards = cmyCards.size();
}
// getter methods
public ArrayList<Card> cards() {
return myCards;
}
// mwthod to get the rank
public double getRanking() {
double _rank = 0;
if (checkIsStraightFlush() > 8.0) {
_rank = checkIsStraightFlush();
} else if (checkFourOfaKind() > 7.0) {
_rank = checkFourOfaKind();
} else if (checkFullHouse() > 6.0) {
_rank = checkFullHouse();
} else if (checkFlush() > 5.0) {
_rank = checkFlush();
} else if (checkStraigt() > 4.0) {
_rank = checkStraigt();
} else if (checkThreeOfaKind() > 3.0) {
_rank = checkThreeOfaKind();
} else if (checkTwoPair() > 2.0) {
_rank = checkTwoPair();
} else if (checkPair() > 1.0) {
_rank = checkPair();
}
return _rank;
}
// generate values and return
private int[] values() {
int[] _res = new int[5];
for (int i = 0; i < myCards.size(); i++) {
_res[i] = myCards.get(i).intValue();
}
return _res;
}
// creating suits array and returning
private String[] suits() {
String[] _res = new String[5];
for (int i = 0; i < myCards.size(); i++) {
_res[i] = myCards.get(i).suit();
}
return _res;
}
//checking for pairing
public double checkPair() {
double _res = 0.0;
for (int i = 0; i < values.length - 1; i++) {
if (values[i] == values[i + 1]) {
_res = 1.0 + (values[i] * 0.01);
}
}
return _res;
}
// check for _tempTwo pair
public double checkTwoPair() {
double _res = 0.0;
double value = 0.0;
int _tempCounter = 0; // total pairs
for (int i = 0; i < values.length - 1; i++) {
if (values[i] == values[i + 1]) {
_tempCounter++;
value = values[i] * 0.01;
}
}
if (_tempCounter == 2) {
_res = 2.0 + value;
}
return _res;
}
// check three of a kind
public double checkThreeOfaKind() {
double _res = 0.0;
int _tempCounter = 0;
for (int i = 0; i < values.length - 2; i++) {
if (values[i] == values[i + 1] && values[i] == values[i + 2]) {
_res = 3.0 + (values[i] * 0.01);
}
}
return _res;
}
/checking for straight
public double checkStraigt() {
double _res = 0.0;
for (int i = 0; i < values.length - 1; i++) {
if (values[i] == values[i + 1] - 1) {
_res = 4.0 + (values[i + 1] * 0.01);
} else {
_res = 0.0;
}
}
return _res;
}
// check for flush
public double checkFlush() {
double _res = 0.0;
String suit = suits[0];
for (int i = 0; i < suits.length; i++) {
_res = 5.0 + (values[i] * 0.01);
if (suits[i].equals(suit) == false) {
_res = 0.0;
}
}
return _res;
}
// check for full house
public double checkFullHouse() {
double _res = 0.0;
boolean _tempOne = false;
boolean _tempTwo = false;
for (int i = 0; i < values.length - 2; i++) {
if (values[i] == values[i + 1] && values[i] == values[i + 2]) {
_tempOne = true;
}
}
if (values[3] == values[4]) {
_tempTwo = true;
}
if (_tempOne && _tempTwo) {
_res = 6.0 + (values[values.length - 1] * 0.01);
}
return _res;
}
// check four of a kind
public double checkFourOfaKind() {
double _res = 0.0;
int _tempCounter = 0;
for (int i = 0; i < values.length - 3; i++) {
if (values[i] == values[i + 1] && values[i] == values[i + 2] && values[i] == values[i + 3]) {
_res = 7.0 + (values[i] * 0.01);
}
}
return _res;
}
// check is straight flush
public double checkIsStraightFlush() {
double _res = 0.0;
if (checkStraigt() > 4.0 && checkFlush() > 5.0) {
_res = 8.0 + checkStraigt() - 4.0;
}
return _res;
}
// compare method used for sorting
public int compareTo(Hand secondHand) {
int _res = 0;
int _tempRank = (int) this.getRanking();
int _tempSecondRank = (int) secondHand.getRanking();
double _tempHighCard = this.getRanking() - _tempRank;
double _tempSecondHighCard = secondHand.getRanking() - _tempSecondRank;
int _tempHighCard = (int) _tempHighCard;
int -tempSecondHighCard = (int) _tempSecondHighCard;
if (_tempRank == _tempSecondRank) { // same rank?
if (_tempHighCard > -tempSecondHighCard) {
_res = 1;
} else if (-tempSecondHighCard > _tempHighCard) {
_res = -1;
} else {
_res = 0;
}
} else if (_tempRank > _tempSecondRank) { // nor more rank
_res = 1;
} else if (_tempSecondRank > _tempRank) { // if visitor card has high rank
_res = -1;
} else {
_res = 0;
}
return _res;
}
// tostring method to print obect instance
public String toString() {
String _res = "";
_res += myCards.get(0).toString();
for (int i = 1; i < 5; i++) {
_res += ", " + myCards.get(i).toString();
}
return _res;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.