Stanford CS106A YahtzeeMagicStub CheckCategory Method Replacement Code

According to the Stanford CS106A Assignment 5 Yahtzee! game description, one of the hardest parts of building the Yahtzee game is figuring out how to program your own YahtzeeMagicStub CheckCategory method.

Here is my solution to this problem:

I figured out that for the given categories, we need to know how many 1s, 2s, 3s… 6s there are in the rolled die in the game. To do that, I created an ArrayList for each possible die value (from 1 to 6), and added 1 to the appropriate ArrayList if the value was present in the rolled die configuration. I then looked at the array sizes to figure out whether the category should be a match or now. Here is my code, which you can also find on gist:

/* Pre-condition: The player has finished rolling the dice and selects a category.
	 * This method returns true if the selected category matches
	 * to the actual category correctly, and false if it does not match. */
	private boolean checkCategory(int[] dice, int category) {
		boolean categoryMatch = false;
		if(category >= ONES && category <= SIXES || category == CHANCE) {
			categoryMatch = true;
		}
		else {

			//creates an array for each possible dice value (1-6)
			ArrayList <Integer> ones = new ArrayList<Integer>();
			ArrayList <Integer> twos = new ArrayList<Integer>();
			ArrayList <Integer> threes = new ArrayList<Integer>();
			ArrayList <Integer> fours = new ArrayList<Integer>();
			ArrayList <Integer> fives = new ArrayList<Integer>();
			ArrayList <Integer> sixes = new ArrayList<Integer>();

			/*goes through each rolled die and puts 1 as a place-holder into the appropriate ArrayList
			* e.g. if the first die value is 1, then 1 is added to the ones ArrayList or
			* if the second die value is 5, then 1 is added to the fives ArrayList*/
			for(int i = 0; i < N_DICE; i++) {
				if(dice[i] == 1) {
					ones.add(1);
				}
				else if(dice[i] == 2) {
					twos.add(1);
				}
				else if(dice[i] == 3) {
					threes.add(1);
				}
				else if(dice[i] == 4) {
					fours.add(1);
				}
				else if(dice[i] == 5) {
					fives.add(1);
				}
				else if(dice[i] == 6) {
					sixes.add(1);
				}
			}
			if(category == THREE_OF_A_KIND) {
				if(ones.size() >= 3 || twos.size() >= 3 || threes.size() >= 3 || fours.size() >= 3 || fives.size() >= 3 || sixes.size() >= 3) {
					categoryMatch = true;
				}
			}
			else if(category == FOUR_OF_A_KIND) {
				if(ones.size() >= 4 || twos.size() >= 4 || threes.size() >= 4 || fours.size() >= 4 || fives.size() >= 4 || sixes.size() >= 4) {
					categoryMatch = true;
				}
			}
			else if(category == YAHTZEE) {
				if(ones.size() == 5 || twos.size() == 5 || threes.size() == 5 || fours.size() == 5 || fives.size() == 5 || sixes.size() == 5) {
					categoryMatch = true;
				}
			}
			else if(category == FULL_HOUSE) {
				if(ones.size() == 3 || twos.size() == 3 || threes.size() == 3 || fours.size() == 3 || fives.size() == 3 || sixes.size() == 3) {
					if(ones.size() == 2 || twos.size() == 2 || threes.size() == 2 || fours.size() == 2 || fives.size() == 2 || sixes.size() == 2) {
						categoryMatch = true;
					}
				}
			}
			else if(category == LARGE_STRAIGHT) {
				if(ones.size() == 1 && twos.size() == 1 && threes.size() == 1 && fours.size() == 1 && fives.size() == 1){
					categoryMatch = true;
				}
				else if(twos.size() == 1 && threes.size() == 1 && fours.size() == 1 && fives.size() == 1 && sixes.size() == 1) {
					categoryMatch = true;
				}
			}
			else if(category == SMALL_STRAIGHT) {
				if(ones.size() >= 1 && twos.size() >= 1 && threes.size() >= 1 && fours.size() >= 1) {
					categoryMatch = true;
				}
				else if(twos.size() >= 1 && threes.size() >= 1 && fours.size() >= 1 && fives.size() >= 1) {
					categoryMatch = true;
				}
				else if(threes.size() >= 1 && fours.size() >= 1 && fives.size() >= 1 && sixes.size() >= 1) {
					categoryMatch = true;
				}
			}
		}
		return categoryMatch;
	}

Enjoy the article? Join over 17,500+ Swift developers and enthusiasts who get my weekly updates.

  • Trung

    how can you come up with that idea ? I just do it separately for each category, so I need a lot of page to code…

  • Dustin

    Very interesting that you used an ArrayList also. I coded my own YahtzeeMagicStub at the end of my Yahtzee program as well, but I checked all categories without importing extra features. That is until I got hopelessly stuck on a small straight. I ended up importing ArrayLists for that category alone. I wonder if that would have been accepted if turned in for credit in class.