Need help for this C++ programme

Problem statement
The program is to simulate a simple version of a poker game. The team is required to create a
class for the Card with at least the following attributes:

  • Suit. That represents one of these four: Spades, Hearts, Diamonds and Clubs.
  • Rank. Thirteen values running from two to ten, Jack, Queen, King, and Ace.
    The team must design the class Deck. This class must contains 52 cards; each unique by the
    suit and rank values. The class Deck should have a function call Shuffle, to initialize the
    cards in a random order into an array, or perhaps to allow the user to pick random cards from
    the array of 52 cards.
    Finally, a class Player should also be design, this class is to receive five cards from the deck,
    and this could be done with a function SetCard in the class player and a function
    HandOneCard from the Deck class. The class player should have a function CheckHand
    that display in the screen if there is any card with the same number grouped in pairs, trips or
    quads. Look at the example output.
    Poker Game Options :
    1 – Shuffle and hand
    2 – Exit
    3 – Additional functions
    Please enter an option : 1
    Shuffling…!
    5 :spades: K​:clubs: A​:heart: K​:heart: 5​:diamonds:
    You have two pairs : [5, K]
    Press any key to go back to main menu…
    Additional marks for the team that checks for other hands e.g. flush, full house.
    Notes :
    Suit ASCII Code
    Spade 0x03
    Heart 0x04
    Diamond 0x05
    Club 0x06

<snip/>

Hi fairladyz_nismo, welcome to the forums,

I guess you have the requirements sketched out fairly well. What code do you have so far and what problems are you having with it?

You’ll find the [fphp]shuffle[/fphp] function useful.

Using numbers 1 through 52 for the cards you can use math to determine face and suit. Let $c be the number of any given card.

ceil($c / 13) returns 1 to 4, suites in alphabetical order (club, diamond, heart, spade)

($c % 13) + 1 returns face value.

This way you don’t need to try to encode the card information. You still need an array to track who has which card. To help with that I’ll give you a foundational class to work with, ReadOnlyArray.

ReadOnlyArray implements iteration, array access, countable and so, as an object it can act like an array in many ways. By extending it you can add behaviors specific to your planned deck and player classes.

Note, unlike PHP’s internal ArrayObject, ReadOnlyArray only protects its storage, it doesn’t declare it as private. As a result your extending classes can modify the storage as needed for your methods. Here’s the class.


<?php
namespace Gazelle;
/**
 * ReadOnlyArray
 * 
 * What it says on the tin - An array that cannot be written to.
 * Extended by children which change themselves in special ways.
 * 
 * Why not use ArrayObject? Well, most noticably is that in order
 * to enforce non-writability you'll end up overriding more methods
 * than this implements. Second, this class' storage is merely
 * protected instead of being private as it is with ArrayObject.
 * This gives you a bit more flexibility when extending.
 * 
 * All methods of this class are required by its interfaces. See
 * PHP.net for more information on them and their precise purpose.
 * 
 * @author Michael
 * @package Gazelle Core
 *
 */
class ReadOnlyArray implements \\Iterator, \\ArrayAccess, \\Countable, \\Serializable {
	/**
	 * Current Index of storage.
	 * @var mixed
	 */
	protected $index = '';
	
	/**
	 * The array of data that we shield to make it ReadOnly by the
	 * rest of the API except our children.
	 * @var array
	 */
    protected $storage = array();  

    /**
     * CONSTRUCT - take an array and bind to storage. This is the
     * only time this class can be written to from the outside.
     * @param $array
     */
    public function __construct(array $array ) {
        $this->storage = $array;
        reset($this->storage);
        $this->index = key($this->storage);
    }

    /**
     * ITERATOR implementation. Sets index to first element.
     */
    public function rewind() {
        reset($this->storage);
    }

    /**
     * ITERATOR implementation. Returns current value.
     */
    public function current() {
        return $this->storage[ $this->index ];
    }

    /**
     * ITERATOR implementation. Returns current key.
     */
    public function key() {
        return $this->index;
    }

    /**
     * ITERATOR implementation. Advance index to next element.
     */
    public function next() {
 		next($this->storage);
 		$this->index = key($this->storage);
    }

    /**
     * ITERATOR implementation. Detects end of array.
     */
    public function valid() {
        return isset($this->storage[$this->index]);
    }
    
    /**
     * ARRAY ACCESS implementation. Set a value - disabled.
     * @param string $offset
     * @param mixed $value
     * @throws Exception
     */
	public function offsetSet($offset, $value) {
        throw new Exception('You cannot write values to a Read Only Array after it is created.');
    }
    
    /**
     * ARRAY ACCESS implementation. Detect if an element is set.
     * @param $offset
     */
    public function offsetExists($offset) {
        return isset( $this->storage[$offset] );
    }
    
    /**
     * ARRAY ACCESS implementation. Unset a value - disabled.
     * @param string $offset
     * @throws Exception
     */
    public function offsetUnset($offset) {
        throw new Exception('You cannot delete values from a Read Only Array after it is created.');
    }
    
    /**
     * ARRAY ACCESS implementation. Get a value.
     * @param $offset
     * @throws Exception
     */
    public function offsetGet($offset) {
        if ( isset($this->storage[$offset] )) {
        	return $this->storage[$offset];
        } else {
        	throw new Exception("$offset does not exist");
        }
    }
    
    /**
     * COUNTABLE implementation
     */
    public function count() {
    	return count($this->storage);
    }
    
    /**
     * SERIALIZABLE implementation
     */
    public function serialize() {
    	return serialize($this->storage);
    }
    
    /**
     * SERIALIZABLE implementation
     * @param $data
     */
    public function unserialize( $data ) {
    	$this->storage = unserialize($data);
    }
    /**
     * Return the stored array. Method name matches it's equivalent in ArrayObject.
     */
    public function getArrayCopy() {
    	return $this->storage;
    }
}

Finally, when manipulating that storage you can use array_shift to “draw” off the top of the array.

I would begin from here with a “stack” class representing a stack of cards. A deck is one such stack, and so is a player’s hand. Methods common to both (receiving cards, giving cards) go to this class before doing the either the deck or player.

@Mittineague
i am supppose to use C++ codes and i am not very good in programming, basically i need help in the program

this is what i have done kinda confused…

#include <iostream>
#include <string>
#include <iomanip>
#include "Header.h"
using namespace std;

void Card :: SetCard (string s, string r)
{ 
Suit = s;
Rank = r;
S_Card = Rank + Suit;
}

string Card :: GetCard ()
{
return S_Card;
}

void Deck :: SetCard ()
{
char i,j,k=0;
for ( i=1; i<14; i++)
{
for ( j=0x03; j<=0x06 ; j++ )
{
Card :: SetCard (j,i);
Card[k] = Card :: GetCard ();
k++;
}
}
}

void Deck :: displayCard ()
{
int i;
for (i=0; i<50 ; i++)
cout << Card[i] << " ";
}
void main()
{
Deck Testing;
Testing.SetCard();
Testing.displayCard();
}