Step 1: Implement the Card class from Assignment 2 In another assignment, you mu
ID: 3880560 • Letter: S
Question
Step 1: Implement the Card class from Assignment 2
In another assignment, you must implement several C++ classes and use them to write a Go Fish game. In this lab, we’ll implement and test a few of those classes. The first one you’ll implement is the Card class, which represents a single playing card:
Step 2: Implement the Deck class from Assignment 2
Once you have a class to represent a single card, you can implement your class to represent a whole deck of 52 cards:
Step 3: Write a small application to deal a hand of cards
In order to test your classes, write a small program that uses them to do the following things:
Initializes a new deck of 52 cards.
Shuffles that deck.
Deals a hand of 7 cards.
Prints the contents of that hand to the console.
Put this program in deal_hand.cpp. Also, add a Makefile to compile your program.
class Card f private: int rank: 1/ Should be in the range 0-12 int suit; /I Should be in the range 0-3. public // constructors, destructor, accessors, and mutators l;Explanation / Answer
dealhand.cpp
--------------------------------------------------------------------------------------
#include "card.hpp"
#include "deck.hpp"
#include "hand.hpp"
#include "player.hpp"
#include "game.hpp"
int main() {
bool playing = true;
while (playing) {
game game1;
game1.gamestart();
cout << endl << endl << "If you would like to play again, enter 1, if not, enter 0!" << endl;
cin >> playing;
}
return 0;
}
-------------------------------------------------------------------------------------
card.cpp
-------------------------------------------
#include "card.hpp"
card::card() {
rank = 0;
suit = 0;
}
card::~card() {
}
void card::setcard(int r, int s) {
rank = r;
suit = s;
}
int card::getrank() {
return rank;
}
int card::getsuit() {
return suit;
}
-------------------------------------------------------------------------------
card.hpp
---------------------------------------
#ifndef card_
#define card_
using namespace std;
class card {
private:
int rank;
int suit;
public:
card();
~card();
void setcard(int r, int s);
int getsuit();
int getrank();
};
#endif
-----------------------------------------------------------------------------
deck.cpp
--------------------------------------
#include "deck.hpp"
deck::deck() {
n_cards = 52;
cardplace = 0;
int i = 0;
for (int j = 0; j < 4; j++) {
for (int k = 0; k < 13; k++) {
cards[i].setcard(k, j);
i++;
}
}
}
deck::~deck() {
}
card deck::dealcard() {
cardplace++;
return cards[cardplace-1];
}
void deck::shuffledeck() {
int index, cardleft = n_cards - cardplace;
srand(time(NULL));
for (int i = cardplace; i < n_cards; i++) {
index = rand() % cardleft + cardplace;
card temp = cards[index];
cards[index] = cards[i];
cards[i] = temp;
}
}
card deck::getcard(int i) {
return cards[i];
}
------------------------------------------------------------------------------
deck.hpp
------------------------------------
#ifndef deck_
#define deck_
#include <iostream>
#include <time.h>
#include <stdlib.h>
#include "card.hpp"
using namespace std;
class deck {
private:
card cards[52];
int n_cards;
int cardplace;
public:
deck();
~deck();
card dealcard();
void shuffledeck();
card getcard(int);
};
#endif
-------------------------------------------------------------------------------
game.cpp
---------------------------------------------
#include "game.hpp"
game::game() {
finished = false;
deck1 = new deck;
deck1->shuffledeck();
players[0].playermakehand(deck1);
players[1].playermakehand(deck1);
}
void game::gprinthand(int n) {
players[n].printhand();
}
game::~game() {
delete deck1;
}
void game::pause(int i) {
char pause;
string prompt[2] = { "the computer's", "your" };
cout << "Press enter to continue to " << prompt[i] << " turn" << endl;
cin.ignore(!i);
pause = cin.get();
system("clear");
}
bool game::playerbase() {
bool turn = true;
while (turn) {
turn = this->playerturn(0);
this->playerbooks(0);
this->playerstatus(0);
if (players[0].getn_books() + players[1].getn_books() == 13) {
return true;
}
}
}
bool game::cpubase() {
bool turn = true;
while (turn) {
turn = this->cputurn(1);
this->playerbooks(1);
cout << "The computer has " << players[1].getn_books() << " book(s)" << endl;
if (players[0].getn_books() + players[1].getn_books() == 13)
return true;
}
}
void game::gamestart() {
cout << "Welcome to Go Fish!" << endl;
while (!finished) {
if (this->playerbase())
break;
this->pause(0);
if (this->cpubase())
break;
this->pause(1);
}
if (players[0].getn_books() > players[1].getn_books()) {
cout << "Congratulations, you won with " << players[0].getn_books() << " books!!" << endl;
}
else
cout << "The computer won with " << players[1].getn_books() << " books, better luck next time!" << endl;
}
bool game::cputurn(int n) {
cout << "It is the cpu's turn!" << endl;
int search = players[n].randomcard();
cout << "The computer searched for " << search << endl;
cout << "You had " << searchhand(!n, search) << endl;
if (searchhand(!n, search) == 0) {
int fished = gofish(n);
if (fished == search) {
cout << "The computer drew a " << search << endl;
return true;
}
}
else {
cout << "You gave the computer " << searchhand(!n, search) << " " << search << "(s)" << endl;
stealcards(n, search, searchhand(!n, search));
return true;
}
return false;
}
void game::playerbooks(int n) {
for (int i = 0; i < 13; i++) {
if (searchhand(n, i) == 4) {
players[n].bookset(i);
if (n == 0) {
cout << "You created a book of " << i << "'s" << endl << endl;
}
else
cout << "The computer created a book of " << i << "'s" << endl << endl;
}
}
}
bool game::playerturn(int n) {
cout << "It is your turn! "<< endl;
if (players[n].pgethand().howmany() == 0) {
cout << "Your hand is empty! Draw a card!" << endl;
gofish(n);
}
this->playerstatus(n);
cout << "What would you like to search for?" << endl;
int search = isgood(n);
cout << endl << "You searched for " << search << endl;
cout << "your opponent had " << searchhand(!n, search) << endl << endl;
if (searchhand(!n, search) == 0) {
cout << "Go Fish!!" << endl << endl;
int fished = gofish(n);
if (fished == search) {
return true;
}
}
else {
stealcards(n, search, searchhand(!n, search));
return true;
}
return false;
}
int game::gofish(int n) {
int fished = players[n].addcard(deck1->dealcard(), n);
return fished;
}
void game::stealcards(int n, int search, int numcards) {
hand* temphand = new hand;
players[!n].newhand(temphand);
card* stolen = new card[numcards];
int j = 0;
for (int i = 0; i < temphand->howmany(); i++) {
if (search == temphand->gethand()[i].getrank()){
stolen[j] = temphand->gethand()[i];
j++;
temphand->removecard(i);
i--;
}
}
players[!n].copyhand(temphand);
players[n].addhand(stolen, numcards);
delete[] stolen;
delete temphand;
}
int game::isgood(int n) {
bool bad = true;
string temp;
while (bad) {
cin >> temp;
if (searchhand(n, atoi(temp.c_str())) == 0) {
cout << "Please enter a rank that you have!" << endl;
}
else
bad = false;
}
return atoi(temp.c_str());
}
int game::searchhand(int n, int search) {
int searchnum = 0;
hand* temphand = new hand;
this->players[n].newhand(temphand);
for (int i = 0; i < temphand->howmany(); i++) {
if (search == temphand->gethand()[i].getrank()) {
searchnum++;
}
}
delete temphand;
return searchnum;
}
void game::playerstatus(int n) {
cout << "Your hand is: " << endl;
this->gprinthand(n);
cout << "You have " << players[n].getn_books() << " book(s)" << endl;
if (players[n].getn_books() > 0) {
cout << "Your books are: ";
for (int i = 0; i < players[n].getn_books(); i++) {
cout << players[n].getbooks()[i];
if (i != players[n].getn_books() - 1) {
cout << ", ";
}
}
cout << endl;
}
}
-----------------------------------------------------------------------------------
game.hpp
---------------------------------
#ifndef _game
#define _game
#include "player.hpp"
#include "hand.hpp"
#include <cstdlib>
class game {
private:
deck* deck1;
player players[2];
bool finished;
//int realplayers;
public:
game();
void gprinthand(int);
void gamestart();
~game();
void playerstatus(int n);
bool playerturn(int);
int isgood(int);
int searchhand(int, int);
void stealcards(int, int, int);
int gofish(int);
void playerbooks(int);
bool cputurn(int);
void pause(int i);
bool playerbase();
bool cpubase();
};
#endif
----------------------------------------------------------------------------
hand.cpp
--------------------------------------
#include "hand.hpp"
int hand::howmany() {
return n_cards;
}
hand::hand(const hand& other) {
this->n_cards = other.n_cards;
this->cards = new card[this->n_cards];
for (int i = 0; i < this->n_cards; i++) {
this->cards[i] = other.cards[i];
}
}
void hand::operator=(const hand& other) {
this->n_cards = other.n_cards;
this->cards = new card[this->n_cards];
for (int i = 0; i < this->n_cards; i++) {
this->cards[i] = other.cards[i];
}
}
hand::hand() {
n_cards = 0;
}
card* hand::gethand() {
return cards;
}
void hand::setcard(card deal) {
cards = addcard();
cards[n_cards] = deal;
n_cards++;
}
card* hand::addcard() {
card* temp;
temp = new card[n_cards + 1];
for (int i = 0; i < n_cards; i++) {
temp[i] = cards[i];
}
if (n_cards > 0) {
delete[] cards;
}
return temp;
}
void hand::removecard(int bcard) {
n_cards--;
card* temp = new card[n_cards];
int j = 0;
for (int i = 0; i < n_cards+1; i++) {
if (i != bcard) {
temp[j] = this->cards[i];
j++;
}
}
delete[] cards;
cards = temp;
}
hand::~hand() {
if (n_cards > 0) {
delete[] cards;
}
}
-----------------------------------------------------------------------
hand.hpp
-----------------------------------------
#ifndef hand_hpp
#define hand_hpp
#include "deck.hpp"
class hand {
private:
card* cards;
int n_cards;
public:
hand(const hand&);
hand();
card* gethand();
void setcard(card);
card* addcard();
~hand();
int howmany();
void operator= (const hand&);
void removecard(int);
};
#endif
---------------------------------------------------------------------
player.cpp
---------------------------------
#include "player.hpp"
player::player() {
books = new int[1];
n_books = 0;
}
void player::playermakehand(deck* deck1) {
for (int i = 0; i < 7; i++) {
myhand.setcard(deck1->dealcard());
}
}
void player::psethand(hand temp) {
myhand = temp;
}
hand player::pgethand() {
return myhand;
}
player::~player() {
delete[] books;
}
int player::addcard(card newcard, int n) {
myhand.setcard(newcard);
if (n == 0) {
cout << endl << "You drew a " << newcard.getrank() << endl;
}
return newcard.getrank();
}
void player::printhand() {
for (int i = 0; i < myhand.howmany() ; i++) {
cout << "Card " << i + 1 << " is rank: ";
cout << myhand.gethand()[i].getrank() << " and suit: " << myhand.gethand()[i].getsuit() << endl;
}
}
void player::bookset(int book) {
int* temp = new int[n_books + 1];
for (int i = 0; i < n_books; i++) {
temp[i] = books[i];
}
temp[n_books] = book;
n_books++;
delete[] books;
books = temp;
removebook(book);
}
void player::removebook(int book) {
for (int i = 0; i < myhand.howmany(); i++) {
if (myhand.gethand()[i].getrank() == book) {
myhand.removecard(i);
i--;
}
}
}
int* player::getbooks() {
return books;
}
int player::getn_books() {
return n_books;
}
void player::newhand(hand* temphand) {
*temphand = myhand;
}
void player::copyhand(hand* temp) {
myhand.~hand();
myhand = *temp;
}
void player::addhand(card* stolen, int numcards) {
for (int i = 0; i < numcards; i++) {
myhand.setcard(stolen[i]);
}
}
int player::randomcard() {
srand(time(NULL));
int randcard = rand() % myhand.howmany();
return myhand.gethand()[randcard].getrank();
}
-------------------------------------------------------------------------------------
player.hpp
----------------------------------------
#ifndef player_
#define player_
#include "hand.hpp"
#include <iostream>
class player {
private:
hand myhand;
int* books;
int n_books;
public:
player();
void playermakehand(deck*);
~player();
hand pgethand();
void psethand(hand);
void printhand();
void bookset(int book);
int* getbooks();
int getn_books();
void newhand(hand*);
void copyhand(hand*);
void addhand(card*, int);
int addcard(card, int);
void removebook(int book);
int randomcard();
};
#endif
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.