Chris Pollett>Old Classes>PIC 10a, Spring 2000>Hw5>Hw5 Solutions
// Program Name: CounterType.cpp
//
// Purpose: contains the CounterType class and a driver program.
// The CounterType class stores a nonnegative integer
// and besides two contructors supports increment, decrementing
// getting the counter value, and output the counter value to
// an ostream.
//
// Known Bugs: none
//
#include<iostream.h>
//
//
// Classes
//
//
// Class name: CounterType
//
// Purpose: Stores a nonnegative integer called a counter.
// Has methods which allows one to construct
// a CounterType object, add one to the counter,
// subtract one from the counter, get the current
// counter value, and output the counter to an ostream.
//
// Known Bugs: none
class CounterType
{
public:
CounterType(); //constructor. Sets counter to 0.
CounterType( int i); // constructor. Sets counter to i.
int increment(); // Postcondition: counter++
int decrement(); // Postcondition: if counter<=0
// then it is unchanged, else counter--
int getCounter(); // returns current counter value/
int output( ostream& out); //directs counter value to
// stream out.
private:
int counter; // this is where the counter value stored
};
// Method name: CounterType()
//
// Purpose: creates a CounterType object with counter set to 0.
//
// Known Bugs: none
CounterType::CounterType()
{
counter=0;
}
// Method name: CounterType(int)
//
// Purpose: creates a CounterType object with counter set to the value
// passed
//
// Known Bugs: if i <0 this constructor has a side-effect in that
// it prints an error to the screen. It then sets
// the counter to 0.
CounterType::CounterType(int i)
{
if( i < 0)
{
cout << "Attempt to give a negative counter value\n"
<< "setting counter to 0.\n";
counter = 0;
}
else
counter = i;
}
// Method name: increment()
//
// Purpose: adds one to the counter
//
// Known Bugs: none
CounterType::increment()
{
counter++;
}
// Method name: decrement()
//
// Purpose: if counter is positive subtract one from its value
// otherwise leaves it unchanged
//
// Known Bugs: none
CounterType::decrement()
{
if(counter > 0) counter--;
}
// Method name: getCounter()
//
// Purpose: returns current value of the counter
//
// Known Bugs: none
CounterType::getCounter()
{
return counter;
}
// Method name: output(ostream& )
//
// Purpose: directs the value of the counter to the specified ostream.
//
// Known Bugs: none
CounterType::output( ostream& out)
{
out << counter << endl;
}
//
//
// Function Prototypes for Driver program
//
//
void PrintMenu(); // Prints driver menu
int GetChoice(int lo, int hi);
// Precondition: lo < hi have been assigned values
//
// Postcondition: has gotten a choice from the user between lo and hi
// and returned it
//
//
//
// Global Variables
//
//
CounterType test;
//
//
// Function Definitions
//
//
// Function name: main()
//
// Purpose: Prints a menu and allows the user to test CounterType
// class
//
// Known Bugs:
int main()
{
bool keepTesting = true;
int count;
while( keepTesting)
{
PrintMenu();
switch(GetChoice(1,7))
{
case 1:
test = CounterType();
break;
case 2:
cout << "\nEnter an integer value for the counter:";
cin >> count;
test = CounterType(count);
break;
case 3:
test.increment();
break;
case 4:
test.decrement();
break;
case 5:
cout << "\nTesting getCounter()...\n";
cout << "According to getCounter\n";
cout << "current counter value is:"
<< test.getCounter() << endl;
break;
case 6:
cout << "\nTesting output(cout)...\n";
test.output(cout);
break;
case 7:
keepTesting =false;
}
}
return 0;
}
// Function name: PrintMenu()
//
// Purpose: prints driver program's menu
//
// Known Bugs:
void PrintMenu()
{
cout << "\nCounterType testing program\n";
cout << "===========================\n";
cout << "(1) Use 0-ary constructor\n";
cout << "(2) Use 1-ary constructor\n";
cout << "(3) Increment counter\n";
cout << "(4) Decrement counter\n";
cout << "(5) Test getCounter()\n";
cout << "(6) Test output to stream\n";
cout << "(7) Quit test program\n";
}
// Function name: GetChoice(int lo, int hi)
//
// Purpose: if lo < hi have been assigned values then gets a choice from
// the user between lo and hi
// and returns it
//
// Known Bugs: Can screw up if a non-integer input by user
int GetChoice(int lo, int hi)
{
int choice = lo-1;
bool flag =true;
do{
cout << "\nPlease enter a number between " << lo
<< " and " << hi << ": ";
cin >> choice;
if( choice < lo || choice > hi)
cout << "Invalid choice.\n";
else flag = false;
}while(flag);
return choice;
}
// Program Name: Blackjack.cpp
//
// Purpose: A short program to score blackjack hands.
//
// Comments: A card is a single character. '2'-'9' are as would guess
// 'a' = Ace, 't' = 10, 'j'= Jack, 'q' = Queen, and 'k'= King
//
#include<iostream.h>
void GetCardsAndScore(int n); //get n cards from user then calculates
//the value of this hand and prints it
int ScoreCard(char card); //returns what an individaul card is worth
// Function name: main
//
// Purpose: main loop for Blackjack program. Prints welcome then
// gets number of cards. Calls GetCardAndScore
// to get each card in turn and calculates and prints score.
// Then asks if want to play again.
//
// Known Bugs: none
int main()
{
bool keepPlaying=true;
int numCards;
char playAgain;
while(keepPlaying)
{
cout << "\nWelcome to the Blackjack game! \n\n";
cout << "Enter the number of cards in the blackjack hand: ";
cin >> numCards;
GetCardsAndScore(numCards);
cout << "\nWould you like to play again? (Type y if yes)\n";
cin >> playAgain;
if(playAgain != 'y') keepPlaying=false;
}
}
// Function name: GetCardsAndScore
//
// Purpose: Gets n cards from user. It then calculates and prints
// their blackjack score. If this is > 21 busted is printed.
//
// Known Bugs: none
void GetCardsAndScore(int n)
{
char card;
int sum =0;
bool sawAce = false;
for(int i = 0; i <n; i++)
{
cout << "\nPlease enter card "<< i << ": ";
cin >> card;
sum +=ScoreCard(card);
if(card=='a' || card == 'A') sawAce = true;
}
if(sawAce && sum <= 11) sum += 10;
if(sum <22)
cout << "\nThe score of that hand is: " << sum <<endl;
else
cout << "\nBusted.\n";
}
// Function name: ScoreCard
//
// Purpose: returns the value of what an individual card is worth.
//
// Known Bugs: none
int ScoreCard(char card)
{
switch(card)
{
case 'A':
case 'a':
return 1;
case 'T': case 'J': case 'Q': case 'K':
case 't': case 'j': case 'q': case 'k':
return 10;
case '2': case '3': case '4': case '5':
case '6': case '7': case '8': case '9':
return (int)(card - '0'); // viewing characters
// as ASCII number
// then subtracting '0' gives
// between 2 and 9
default:
cout << "\nThat was a bogus card. This hand probably\n";
cout << "won't be accurately scored!\n\n";
return 0;
}
}