Assignment 1 Solutions
Solution to player.cc
/******************************************************************* * Name: Brenda Student * Date: 1/30/14 * Course: CSCI 132 Section 01 * Assignment: Assignment 1 * Instructor: Royden * Program: player.cc * Purpose: Implementation of player class for use in the Dice Poker game. ***************************************************************************/ #include "player.h" Player::Player(void ) { /* Pre: None Post: Player object is created with zero points, zero rank and the dice are all zero. */ points = 0; rank = 0; for (int i = 0; i < numDice; i++ ) { diceArray[i] = 0; } } void Player::clearRank(void) { /* Pre: Player object exists. Post: rank of player object is set to zero. */ rank = 0; } void Player::clearPoints(void) { /* Pre: Player object exists. Post: points for player object is set to zero. */ points = 0; } void Player::throwDice(void) { /* Pre: Player object exists. Post: Each die is assigned a random number betwwen 1 and 6. */ for (int i = 0; i < numDice; i++ ) { diceArray[i] = (int)Randomize(6); cout << "Die #" << i+1 << " is a " << diceArray[i] << endl; } computeRank(); } void Player::showRank(void) { /* Pre: Player object exists and throwDice has been called. Post: A message is printed to the monitor corresponding the the rank of the current values of the dice. */ switch(rank) { case 0: cout << "No Score, 0 points." << endl; break; case 1: cout << "One Pair, 1 point." << endl; break; case 2: cout << "Two Pairs, 2 points." << endl; break; case 3: cout << "Three of a kind, 3 points." << endl; break; case 4: cout << "Full House, 4 points. " << endl; break; case 5: cout << "Four of a kind, 5 points. " << endl; break; case 6: cout << "Five of a kind, 6 points." << endl; break; } } int Player::getRank(void) { /* Pre: Player object exists. Post: Returns the rank of player object. */ return rank; } int Player::getPoints( void ) { /* Pre: Player object exists. Post: Returns the number of points for the player object */ return points; } void Player::addPoint(void) { /* Pre: Player object exists. Post: points of player object is incremented by 1. */ points += 1; } void Player::computeRank(void) { /* Pre: Player object exists and dice have been thrown at least once. Post: Rank of current values of dice is computed and stored in rank. */ int numArray[7]; int numPairs = 0; int i; for (i = 0; i < 7; i++){ // initialize the contents of numArray to 0 numArray[i] = 0; } // step through the five dice values in diceArr and increment the // contents of the corresponding location of numArray for (i = 0; i < 5; i++) { numArray[diceArray[i]]++; } for (i = 1; i < 7; i++) { if (numArray[i] == 2) { numPairs++; } } // determine whether numArray contains a 3, 4 or 5, print the // corresponsing description, and return the rank rank = 0; for (i = 1; i < 7; i++) { if (numArray[i] == 5) { rank = 6; } else if (numArray[i] == 4) { rank = 5; } else if ((numArray[i] == 3) && (numPairs == 1)) { rank = 4; } else if ((numArray[i] == 3) && (numPairs == 0)) { rank = 3; } } if (rank < 3) { // if execution of the for loop ends without setting rank above // zero, then numArray contains only 0's, 1's and 2's, and // execution continues here if (numPairs == 2) { rank = 2; } else if (numPairs == 1) { rank = 1; } else { rank = 0; } } } /**********Randomize***************************** *Get a random number between 0 and range * *************************************************/ int Randomize(int range) { return (int)(((double)rand()/((double)RAND_MAX + 1.0)) * (double)range) + 1; }