Anda di halaman 1dari 6

#include <iostream> #include <math.

h> using namespace std; const int SIZE = 6; const const const const const const int int int int int int //Array size for dice

straightScore = 1000; threeAlike = 100; fourAlike = threeAlike*2; fiveAlike = fourAlike*2; sixAlike = fiveAlike*2; threePair = 500;

int rollDie(); //rolls a die between 1 and 6 void rollArray(int[], int size); //Rolls a random array of of SIZE entri es void convertInput(int,int[],int&); void convertOccurence(const int[]); //boolean values to check for in main function bool hasStraight(int[]); bool hasThreePair(int[]); bool scoreable(int[]); bool validity(int[],int[]); //Checks to see if the dice the user inputs match up with the dice given int scoreDice(int[]);

//calls: convertINput, scoreable, scoreDice int main() { //The following two variables refer to the dice being rolled int rolledDice[SIZE]; int rolledDiceOccurence[SIZE]; int rolledDiceSize = SIZE; //Variables for dice user inputs int diceInputArray[SIZE]; int diceInputOccurence[SIZE]; int diceSize; int diceInput; //Score variables int totalScore; int turnScore; //The TURN for(char answer = 'y'; answer == 'y'; cin >> answer) { rollArray(rolledDice,rolledDiceSize); //The loop is in case the user uses an invalid selection of dice //validity tests whether or not the selection is valid do{ cout << "Please enter the dice you wish to score, with NO spaces : ";

cin >> diceInput; convertInput(diceInput,diceInputArray,diceSize); convertOccurence(diceInputArray,diceInputOccurence); if(validity(diceInputArray,rolledDice) == false) cout << "You can't use those dice. Please input a valid selection of dice." << endl; } while(validity(diceInputArray,rolledDice) == false); only works while false rolledDiceSize = rolledDiceSize - diceSize; eration if(scoreable(diceInputArray) == true) turnScore = scoreDice(diceInputArray); else if(scoreable(diceInputArray) == false) { cout << "There is no way to score. Turn ends!" << endl; turnScore = 0; } cout << "You earned " << turnScore << " points on that turn. Tot al = " << totalScore << endl; //Loop

//For the next it

cout << endl << "Player another turn (y/n)? "; } return 0; } //Rolls a die between 1 and 6 int rollDie() { return (rand()%6 + 1); } //Makes an array filled with random void rollArray(int dice[], int arraySize) { for(int i = 0; i < arraySize; i++) dice[i] = rollDie(); cout << "Rolling " << arraySize << ":"; for(int i = 0; i < arraySize; i++) cout << dice[i] << " "; cout << endl; } //convertInput: //counts the individual digits in an integer value

//used to allow a single input value to represent a set of dice to keep //Hint: repeatedly dividing the input parameter by 10 gives useful remainders void convertInput(int inputValue, int dice[], int& digits) { //Turns the inputValue into single digits to input into dice[] for(int i = 0; i < SIZE; i++) { dice[i] = inputValue%10; //Assigns last digit to array inputValue = inputValue/10; //Truncates the last digit } digits = 0; //Initialize the digits //Not sure what this is for yet for(int i = 0; i < SIZE && dice[i] != 0;i++) digits++; } //hasStraight: //returns a boolean indicating that the set of dice is a straight //(one each of all the values from 1 to 6) //Parameter: // dice (input int array) : the dice to examine bool hasStraight(int dice[]) { bool straight = true;

//Defaults to true

for(int i = 1; i < SIZE; i++) { if(dice[0] == dice[i] || dice[i] == 0) straight = false; //But if it's false, it's false. d ice[i] can't be zero for straight } return straight; }

//hasThreePair: //returns a boolean indicating that the set of dice has three pair //(two each of three different values) //Parameter: // dice (input int array) the dice to examine bool hasThreePair(int dice[]) { int check = 0; bool threePair = false; r true-ness

//Initialized for the for loop //Initialized to false. Checking fo

//Starts with the first entry and tests it against the other entries //If the first entry has no match with at least two of the the other ent ries, //It will continue to the next entry.

//i.e. if dice[0] has only one match in the array, it will continue to d ice[1] //If dice[1] has a match with two other entries, that means there is a t hree pair //This can only happen when check == 3, because that's when the loop end s for(int m = 0; (m < SIZE) && (check < 3); m++) { check = 0; for(int i = 1; i < SIZE; i++) { if(dice[m] == dice[i]) check++; } } if(check == 3) threePair = true; return threePair; } //scoreable: //returns a boolean indicating whether it is possible to score with any dice //if this returns false ( no score possible ), the current turn will end //Parameter: // dice (input int array) : the dice to examine //Pre-condition: // the total number of dice represented is between 1 and 6, inclusive //Calls: hasStraight, hasThreePair bool scoreable(int dice[]) { bool valid = false; //Runs until it goes through the array or it finds a five or a one dice for(int i = 0; i < SIZE && valid == false; i++) { if (dice[i] == 1 || dice[i] == 5) valid = true; } if(hasStraight(dice) == true) valid = true; else if(hasThreePair(dice) == true) ir valid = true; //Four alike, five alike, and six alike, all need three pair return valid; } int scoreDice(int dice[]) { int score;

//Can't have straight and three pa

if(hasStraight(dice) == true) score = straightScore; return score; } bool validity(int chosenDice[],int rolledDice[]) { int chosenDiceTest[SIZE]; int rolledDiceTest[SIZE]; for(int i = 0; i < SIZE; i++) chosenDiceTest[i] = chosenDice[i]; for(int i = 0; i < SIZE; i++) rolledDiceTest[i] = rolledDice[i]; bool valid = true; for(int i = 0; i < SIZE; i++) { for(int j = 0; j < SIZE; j++) { if(chosenDiceTest[i] == rolledDiceTest[j] && chosenDiceT est[i] != 0) { chosenDiceTest[i] = 0; rolledDiceTest[j] = 0; } } } for(int i = 0; i < SIZE; i++) { if(chosenDiceTest[i] != 0) valid = false; } return valid; } //Takes a given array and creates a occurence array from it //This will be useful for scoring void convertOccurence(const int dice[], int occurenceDice[]) { //Initializes the for (int i = 0; i < SIZE; i++) { occurenceDice[i] = 0; } for (int i = 0; i < SIZE; i++) { if(dice[i] != 0) occurenceDice[dice[i]]++; }

Anda mungkin juga menyukai