Example #1
0
// Returns the roll's value for a given scoring choice
const int DiceRoll::rollValue(const ChoiceAction choice) const
{
	switch(choice) {
	case ACES:
		return sumIfEqual(1);
	case TWOS:
		return sumIfEqual(2);
	case THREES:
		return sumIfEqual(3);
	case FOURS:
		return sumIfEqual(4);
	case FIVES:
		return sumIfEqual(5);
	case SIXES:
		return sumIfEqual(6);
	case THREE_OF_A_KIND:
		if (isThreeOfAKind())
			return sumAll();
		return 0;
	case FOUR_OF_A_KIND:
		if (isFourOfAKind())
			return sumAll();
		return 0;
	case FULL_HOUSE:
		if (isFullHouse())
			return 25;
		return 0;
	case SMALL_STRAIGHT:
		if (isSmallStraight())
			return 30;
		return 0;
	case LARGE_STRAIGHT:
		if (isLargeStraight())
			return 40;
		return 0;
	case YAHTZEE:
		if (isYahtzee())
			return 50;
		return 0;
	case CHANCE:
		return sumAll();
	case ROLL:
		assert(false); // Unscorable!
	}
}
Example #2
0
int main() {


	
	char toReroll[DICE];
	int sectionSelection;
	int categorySelection;
	int counter= 1;

	int upperUsed1 = 0;
	int upperUsed2 = 0;
	int upperUsed3 = 0;
	int upperUsed4 = 0;
	int upperUsed5 = 0;
	int upperUsed6 = 0;

	int lowerUsed1 = 0;
	int lowerUsed2 = 0;
	int lowerUsed3 = 0;
	int lowerUsed4 = 0;
	int lowerUsed5 = 0;
	int lowerUsed6 = 0;
	int lowerUsed7 = 0;

	srand(time(NULL));
	int i;

	while (counter < 13) {	//Must go 13 times to fill all categories
		printf("\: %i\n", counter);

		int rolls=1;		//Variable to hold how many re-rolls a user gets

		for(i = 0; i < DICE; i++) {
			dice[i] = generateDice();		//Generates dice
		}

		printf("Your Roll: ");

		for(i = 0; i < DICE; i++){ 
			printf("%i ", dice[i]);			//Prints dice
		}

		printf("\n");



		while(rolls < 3 ) {					//Loop to re-roll dice if necessary
			printf("Which dice to reroll?\n");
			scanf("%s", &toReroll);

			if(toReroll[0] == '0') {	//Do not reroll any more dice if user enters '0'
				break;
			} else {
				for(i = 0; i < DICE; i++) {
					//printf("%c\n", toReroll[i]);
					if(toReroll[i] == NULL) {
						break;
					}
					int num = toReroll[i] - '0';	//Converts from char to int
					num -= 1;
					dice[num] = generateDice();
				
					
				}

				int x;
					
				for(x = 0; x < DICE; x++){ 
					printf("%i ", dice[x]);			//Prints dice
				}
				printf("\n");


			}



			rolls++;

		}

		printf("Place dice into:\n");
		printf("1) Upper Section\n");
		printf("2) Lower Section\n");

		printf("Selection? ");
		scanf("%i", &sectionSelection);
		printf("\n");

		if(sectionSelection == 1) {		//Place into Upper Section

			while(1) {
				printf("Place dice into:\n");
				printf("1) Ones\n");
				printf("2) Twos\n");
				printf("3) Threes\n");
				printf("4) Fours\n");
				printf("5) Fives\n");
				printf("6) Sixes\n");
				scanf("%i", &categorySelection);
				printf("\n");


				if(categorySelection == 1) {
					if(upperUsed1) {
						printf("Already used. Please pick again\n\n");
						continue;
					}
					upper.ones = computeScore(categorySelection);
					score+= upper.ones;
					upperUsed1 = 1;
					break;
				} else if (categorySelection == 2) {
					if(upperUsed2) {
						printf("Already used. Please pick again\n\n");
						continue;
					}
					upper.twos = computeScore(categorySelection);
					score+= upper.twos;
					upperUsed2 = 1;
					break;
				} else if (categorySelection == 3) {
					if(upperUsed3) {
						printf("Already used. Please pick again\n\n");
						continue;
					}
					upper.threes = computeScore(categorySelection);
					score+= upper.threes;
					upperUsed3 = 1;
					break;
				} else if (categorySelection == 4) {
					if(upperUsed4) {
						printf("Already used. Please pick again\n\n");
						continue;
					}
					upper.fours = computeScore(categorySelection);
					score+= upper.fours;
					upperUsed4 = 1;
					break;
				} else if (categorySelection == 5) {
					if(upperUsed5) {
						printf("Already used. Please pick again\n\n");
						continue;
					}
					upper.fives = computeScore(categorySelection);
					score+= upper.fives;
					upperUsed5 = 1;
					break;
				} else if (categorySelection == 6) {
					if(upperUsed6) {
						printf("Already used. Please pick again\n\n");
						continue;
					}
					upper.sixes = computeScore(categorySelection);
					score+= upper.sixes;
					upperUsed6 = 1;
					break;
				}
			}


			printScore();
			printf("\n\n");

		} else {	//Place into Lower Section

			while(1) {
				char takeZero;
				printf("Place dice into:\n");
				printf("1) Three of a Kind\n");
				printf("2) Four of a Kind\n");
				printf("3) Full House\n");
				printf("4) Small Straight\n");
				printf("5) Large Straight\n");
				printf("6) Yahtzee\n");
				printf("7) Chance\n");
				scanf("%i", &categorySelection);
				printf("\n");

				if(categorySelection == 1) {
					if(lowerUsed1) {
						printf("Already used. Please pick again\n\n");
						continue;
					}

					if(!isThreeOfAKind()){
						printf("You do not have three of a kind. Would you like to take a 0 for this category? [y/n]\n\n");
						scanf("%c", &takeZero);
						if(takeZero == 'y') {
							lower.threeOfAKind = 0;
						} else{
							continue;
						}
					} else{
						int sum = 0;
						for(i = 0; i < DICE; i++) {
							sum += dice[i];
						}

						lower.threeOfAKind = sum;
						score+= lower.threeOfAKind;
						lowerUsed1 = 1;
					}
					
				} else if (categorySelection == 2) {
					if(lowerUsed2) {
						printf("Already used. Please pick again\n\n");
						continue;
					}

					if(!isFourOfAKind()){
						printf("You do not have four of a kind. Would you like to take a 0 for this category? [y/n]\n\n");
						scanf("%c", &takeZero);
						if(takeZero == 'y') {
							lower.fourOfAKind = 0;
						} else {
							continue;
						}
					} else{
						int sum = 0;
						for(i = 0; i < DICE; i++) {
							sum += dice[i];
						}

						lower.fourOfAKind = sum;
						score+= lower.fourOfAKind;
						lowerUsed2 = 1;
					}
					
				} else if (categorySelection == 3) {
					if(lowerUsed3) {
						printf("Already used. Please pick again\n\n");
						continue;
					}
					if(!isFullHouse()) {
						printf("You do not have a full house. Would you like to take a 0 for this category? [y/n]\n\n");
						scanf("%c", &takeZero);
						if(takeZero == 'y') {
							lower.fullHouse = 0;
						} else {
							continue;
						}
					} else {
						lower.fullHouse = FULL_HOUSE;
						score += FULL_HOUSE;
						lowerUsed3 = 1;
					}
				} else if (categorySelection == 4) {
					if(lowerUsed4) {
						printf("Already used. Please pick again\n\n");
						continue;
					}
					if(!isSmallStraight()) {
						printf("You do not have small straight. Would you like to take a 0 for this category? [y/n]\n\n");
						scanf("%c", &takeZero);
						if(takeZero == 'y') {
							lower.smallStraight = 0;
						} else {
							continue;
						}
					} else {
						lower.smallStraight = SMALL_STRAIGHT;
						score += SMALL_STRAIGHT;
						lowerUsed4 = 1;
					}
				} else if (categorySelection == 5) {
					if(lowerUsed5) {
						printf("Already used. Please pick again\n\n");
						continue;
					}
					if(!isLargeStraight()) {
						printf("You do not have large straight. Would you like to take a 0 for this category? [y/n]\n\n");
						scanf("%c", &takeZero);
						if(takeZero == 'y') {
							lower.largeStraight = 0;
						} else {
							continue;
						}
					} else {
						lower.largeStraight = LARGE_STRAIGHT;
						score += LARGE_STRAIGHT;
						lowerUsed5 = 1;
					}
				} else if (categorySelection == 6) {
					if(lowerUsed6) {
						printf("Already used. Please pick again\n\n");
						continue;
					}
					if(!isYahtzee()){
						printf("You do not have yahtzee. Would you like to take a 0 for this category? [y/n]\n\n");
						scanf("%c", &takeZero);
						if(takeZero == 'y') {
							lower.threeOfAKind = 0;
						} else {
							continue;
						}
					} else {
						lower.yahtzee = YAHTZEE;
						score += YAHTZEE;
						lowerUsed6 = 1;
					}
				} else if(categorySelection == 7) {
					if(lowerUsed7) {
						printf("Already used. Please pick again\n\n");
						continue;
					}
					int i;
					int tempScore = 0;
					for (i = 0; i < DICE; i++) {
						tempScore += dice[i];
					}

					lower.chance = tempScore;
					score += tempScore;
					lowerUsed7 = 1;
				}


				printScore();
				printf("\n");
				break;
			} //End while
		} //End else


		counter++;
	} //End while

	if( (upper.ones + upper.twos + upper.threes + upper.fours + upper.fives + upper.sixes) > 63) {
		printf("35 Bonus points because your upper section totalled more than 63\n");
		score+= 35;
	}
	addInZeros();
	printf("Final Score\n");
	printf("------------------------------------------\n");
	printScore();
	printf("------------------------------------------\n");
	 


	return 0;
}