Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Purpose: To practice arrays of records, and stepwise program development. 1.

ID: 3682928 • Letter: C

Question

C++

Purpose: To practice arrays of records, and stepwise program development.

1. Define a record data type for a single playing card. A playing card has a suit ('H','D','S',or 'C'), and a value (1 through 13). Your record data type should have two fields: a suit field of type char, and a value field of type int.
2. In main(), declare an array with space for 52 records, representing a deck of playing cards.
3. Define a function called initialize() to initialize a deck of cards. It will take an array as a parameter, but will return nothing. You need to store the 13 cards of each of the four suits ('H','D','S',or 'C') in this array, but the order of the cards is not too important. In main(), call your function, giving it your array as input.
4. Stop here, to build and run your program. You may not know whether it is working yet, but it should build and run before going on.
5. Define a function called displaySingleCard() to display a single playing card. Your function should take a record, and display the value and the suit, as follows: The values 2-10 are displayed as numbers. The value 1 should be displayed as A (the "ace"), and the values 11,12,13 should be displayed as J, Q, K ("jack", "queen", "king"), respectively. Display the value first, and then the suit. Here are some examples: 5D AC 10H JH QS. Don't use endl in your function here.
6. Define a function called displayCards() to display an array of playing cards (it should call the previous function, displaySingleCard() repeatedly). Your function should take an array and its size as parameters, and it should display all the cards in the array on a single line in the console. Note that this function can be used to display a full deck (52 cards), or the hand of a player (see part 6 below). You should display the value and the suit as in the following example of a 5 card hand: 5D AC 10H JH QS
7. Demonstrate and test your program so far by calling your function displayCards() in main() on the entire deck. Make sure to check the console to see that you have every card of every suit!
8. Define a function to randomly shuffle the cards in the deck. One way to randomly shuffle the elements of an array is to repeatedly generate two random offsets for the array, and swap the two elements at those offsets (you can use the expression (rand() % 52) to create a random offset into a full deck of cards). If this random swapping is done a lot of times (a few hundred for a deck of 52 cards), then you will have a fairly random ordering of the cards at the end.
9. Demonstrate and test your program by calling your shuffle function in main() on the entire deck, and using your display function in main() to show the shuffled deck. Make sure to check the console to be sure that you still have every card (and that nothing got lost in the shuffle).
10. Declare arrays to store a "hand" of cards for 4 players, assuming each player receives 5 cards. To store each player's hand, you could have a single one-dimensional array for each player that contains the cards in their hand; or you could use a two-dimensional array that has an array of cards for each player.
11. In main(), "deal out" (i.e., copy) the cards from your shuffled deck to each player's hand, and display the 4 players' hands to the console as if at the start of a game. You should use your function from part 4 above to do most of the display work. For example, you might display this: Player 1: 5D 10H QS 8C JH Player 2: JC 2H 8D AS 9C Player 3: 7H 6H 9H 10S 8H Player 4: QD JD QC KH JC
12. Write a function that will find the best card in a player's hand. The best card is the card with the highest value. For this question, assume that aces are "low", and assume that the suit of a card is irrelevant. You just want to find the highest value. Use your function in main() so that you can display the best card from each player, e.g., Player 1's best card: QS Player 2's best card: JC Player 3's best card: 10S Player 4's best card: KH If there is a tie, it doesn't matter which of the equal-valued cards you display.

What to hand in:
1. Your program in a text file called a8q1.cpp.
2. A text file called a8q1_testing.tex (.rtf or .doc are okay too) demonstrating that your program works, showing the result of tasks 11 and 12 above.

Explanation / Answer

#ifndef CARD_H

#define CARD_H

#include <string>

const int SUIT_MAX(4);

const int RANK_MAX(13);

class Card

{

    friend class Deck; // Deck Class needs to access to Card Class but not vice versa

public:

    explicit Card();

    explicit Card(const int &suit, const int &rank);

    std::string Card2Str() const;

private:

    int generate_suit();

    int generate_rank();

    int get_suit() const;

    int get_rank() const;

    int m_suit;

    int m_rank;

};

#endif

I would like to know whether I'm going in a correct way or not in building my Poker game. So far, I've implemented card and deck classes and I would like to see your feedback about my work. Feel free to criticize the code of any regard (organization, order, comments ... etc)

card.h

#ifndef CARD_H

#define CARD_H

#include <string>

const int SUIT_MAX(4);

const int RANK_MAX(13);

class Card

{

    friend class Deck; // Deck Class needs to access to Card Class but not vice versa

public:

    explicit Card();

    explicit Card(const int &suit, const int &rank);

    std::string Card2Str() const;

private:

    int generate_suit();

    int generate_rank();

    int get_suit() const;

    int get_rank() const;

    int m_suit;

    int m_rank;

};

#endif

card.cpp

#include <stdlib.h>     /* srand, rand */

#include "card.h"

#include <iostream>

const std::string SUIT[SUIT_MAX] = {"S", "H", "D", "C"};

const std::string RANK[RANK_MAX] = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};

Card::Card()

{

   m_suit = generate_suit();

   m_rank = generate_rank();

}

Card::Card(const int &suit, const int &rank) : m_suit(suit), m_rank(rank)

{

}

int Card::generate_suit()

{

    return rand() % (SUIT_MAX-1) + 0;

}

int Card::generate_rank()

{

    return rand() % (RANK_MAX-1) + 0;

}

std::string Card::Card2Str() const

{

    return SUIT[get_suit()] + RANK[get_rank()];

}

int Card::get_suit() const

{

    return m_suit;

}

int Card::get_rank() const

{

    return m_rank;

}

#ifndef DECK_H

#define DECK_H

#include <vector>

#include <iostream>

#include <fstream>

#include "card.h"

using namespace std;

class Deck

{

public:

      explicit Deck();

      void print_Deck() const;

      void getOneCard();

private:

    std::vector<Card> m_deck;

};

#endif

#include <iostream>

#include "deck.h"

Deck::Deck()

{

    for (unsigned int i(0); i < SUIT_MAX; ++i)

    {

        for (unsigned int j(0); j < RANK_MAX; ++j)

        {

            Card card(i, j);

            m_deck.push_back(card);

        }

    }

}

void Deck::print_Deck() const

{

    unsigned int count(1);

    for (unsigned int i(0); i < m_deck.size(); ++i)

    {

        std::cout << m_deck[i].Card2Str() << " ";

        if ( count == 13 )

        {

          std::cout << std::endl;

          count = 0;

        }

        ++count;

    }

}

void Deck::getOneCard()

{  

    Card cd(m_deck.back().get_suit(), m_deck.back().get_rank());

    m_deck.pop_back();

    std::cout << cd.Card2Str() << std::endl;

}

main.cpp

#include <iostream>

#include <vector>

#include <stdlib.h>     /* srand, rand */

#include <time.h>       /* time */

#include <string>

#include "card.h"

#include "deck.h"

int main()

{

    srand (time(NULL));

    Deck _deck;

    _deck.print_Deck();

    _deck.getOneCard();

    std::cout << std::endl;

    _deck.print_Deck();

    std::cout << std::endl;

    return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote