cpp assignment

profiletroook61
lab_3_4.zip

Lab 3, 4/.DS_Store

__MACOSX/Lab 3, 4/._.DS_Store

Lab 3, 4/Card.h

#ifndef CARD_H #define CARD_H //Defining a card entity //Style says to put class in UpperCamelCase #include <string> using namespace std; class Card { private: string _suit; int _value; string _name; //Default visibility of a class is private. To //change the visibility, we must declare a "public" //zone public: Card(string suit = "Blank", int value = 0, string name = "Unknown") { setSuit(suit); setName(name); setValue(value); } string getSuit() { return _suit; } void setSuit(string suit) { if(suit == "Hearts" || suit == "Diamonds" || suit == "Spades" || suit == "Clubs" || suit == "Blank") { _suit = suit; } else { //AC TODO: remember how to throw exceptions //string message = "bad suit"; //throw exception(message); } } int getValue() { return _value; } void setValue(int value) { if(value >= 0) { _value = value; } } string getName() { return _name; } void setName(string name) { _name = name; } }; #endif // CARD_H

__MACOSX/Lab 3, 4/._Card.h

Lab 3, 4/Deck.h

#ifndef DECK_H #define DECK_H #include "Card.h" //represents deck of potential cards class Deck { private: Card _cards[52]; int _played_cards = 0; public: //Constructors are like default initialization routines //make sure they're public! Deck() { createDeck(); shuffleDeck(); } //creates a deck of 52 cards void createDeck() { _played_cards = 0; string suits[] = {"Hearts", "Spades", "Clubs", "Diamonds"}; string names[] = {"Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King", "Ace"}; int values[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11}; int card_counter = 0; for(int i = 0; i < 4; i++) { for(int j = 0; j < 13; j++) { //create new card of appropriate name, suit, and value Card next_card{suits[i], values[j], names[j]}; //throw card into deck in next available spot _cards[card_counter] = next_card; card_counter++; } } } void shuffleDeck() { //reset played cards counter _played_cards = 0; //i keeps track of cards that have not yet been shuffled for(int i = 51; i > 0; i--) { int next_card_index = rand() % i; //grab random card to place at end of deck Card found_card = _cards[next_card_index]; //grab last card Card last_card = _cards[i]; //swap these two cards _cards[i] = found_card; _cards[next_card_index] = last_card; } } //returns the next card from the deck Card getNextCard() { //are we out of cards? If so, shuffle. //Philosophical note: is this what we want? if(_played_cards > 51) { shuffleDeck(); } //grab next card Card next = _cards[_played_cards]; //increment counter _played_cards++; //return card to caller return next; } }; #endif // DECK_H

__MACOSX/Lab 3, 4/._Deck.h

Lab 3, 4/lab3.docx

In this lab, you will start the process of building a blackjack game. In order to complete this task, you must make the following modifications to our Hand class:

Put the Hand class in its own file called "Hand.h". Remember to include #ifndef blocks!

Add the following public functions to the hand class:

void addCardToHand – adds a new card to the player's hand

int getHandValue – returns the total value of the player's hand. This is used to determine whether or not the player has busted.

Once you implement these functions, you are ready to implement the game logic:

Begin by dealing the player 2 starting cards.

Until the player no longer wants any more cards OR the player has busted (gone over 21):

Deal the player a new card. Add this card to the player's current hand.

Sample Output

__MACOSX/Lab 3, 4/._lab3.docx

Lab 3, 4/lab4.docx

In this lab, you will complete the game of blackjack that we started in Lab 3. Note that this lab does not require you to use classes, but it would be a very good idea as it will help when implementing PA2. Here's the complete flow of the game:

Program Start

Output an introduction

Starting with the player, do the following

Deal the player a card.

Add the card's value to the player's total hand.

Output the value of the card just dealt and the player's current hand total (score)

Ask the player if he would like to hit or stand

If the player hits, go back to #1

After the player busts (goes over 21) or stands

If the player has not busted, begin computer's play

Deal the computer a card

Add the value of the card to the computer's hand total (score)

If a card causes the computer to bust (go over 21), output a message. Go to step #7.

If the computer's hand total is less than 17, it must take another card (hit). Go to step #2.

If the computer's hand is 17 or more, the computer stands.

Stop computer play

When both player and computer are done

If the player has not busted (gone over 21) and the computer busted, the player wins

If the player has not busted, the computer has not busted, and the player's hand total is greater than the computer, then the player wins

If the player has not busted, the computer has not busted, and the player's hand total is less than the computer's then the computer wins

If the player has busted, then the computer wins.

Sample Output

__MACOSX/Lab 3, 4/._lab4.docx

Lab 3, 4/main (1).cpp

#include <iostream> #include <string> #include <ctime> //for time #include <cstdlib> //for rand function #include "Card.h" using namespace std; //represents player or computer hand class Hand { //remember to make variables public public: int value; Card cards[5]; int card_count = 0; void addCardToHand(Card card1) { cards[card_count] = card1; card_count++; } int getHandValue() { int hand_total = 0; for(int i = 0; i < card_count; i++) { hand_total = hand_total + cards[i].getValue(); } return hand_total; } }; //represents person playing the game class Player { public: Hand hand; string name; int money; }; int main() { //Classes should be UpperCamelCase //(each new word in a name begins with a capital letter) //member function names are lowerCamelCase //(first word is not capitalized, all other words begin w/ caps) //variable names should be under_scored //(each word in name is separated by an underscore _ ) srand(time(NULL)); Card blank{}; Card blank1{"Hearts"}; Card blank2{"Hearts", 2}; Card blank3{"Hearts", 2, "Two"}; Deck some_deck{}; for(int i = 0; i < 520; i++) { Card some_card = some_deck.getNextCard(); cout << some_card.getName() << " of " << some_card.getSuit() << endl; } Deck other_deck; other_deck.createDeck(); other_deck.shuffleDeck(); return 0; }

__MACOSX/Lab 3, 4/._main (1).cpp