# 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 &gt;= ONES &amp;&amp; category &lt;= SIXES || category == CHANCE) {
categoryMatch = true;
}
else {

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

/*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 &lt; N_DICE; i++) {
if(dice[i] == 1) {
}
else if(dice[i] == 2) {
}
else if(dice[i] == 3) {
}
else if(dice[i] == 4) {
}
else if(dice[i] == 5) {
}
else if(dice[i] == 6) {
}
}
if(category == THREE_OF_A_KIND) {
if(ones.size() &gt;= 3 || twos.size() &gt;= 3 || threes.size() &gt;= 3 || fours.size() &gt;= 3 || fives.size() &gt;= 3 || sixes.size() &gt;= 3) {
categoryMatch = true;
}
}
else if(category == FOUR_OF_A_KIND) {
if(ones.size() &gt;= 4 || twos.size() &gt;= 4 || threes.size() &gt;= 4 || fours.size() &gt;= 4 || fives.size() &gt;= 4 || sixes.size() &gt;= 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 &amp;&amp; twos.size() == 1 &amp;&amp; threes.size() == 1 &amp;&amp; fours.size() == 1 &amp;&amp; fives.size() == 1){
categoryMatch = true;
}
else if(twos.size() == 1 &amp;&amp; threes.size() == 1 &amp;&amp; fours.size() == 1 &amp;&amp; fives.size() == 1 &amp;&amp; sixes.size() == 1) {
categoryMatch = true;
}
}
else if(category == SMALL_STRAIGHT) {
if(ones.size() &gt;= 1 &amp;&amp; twos.size() &gt;= 1 &amp;&amp; threes.size() &gt;= 1 &amp;&amp; fours.size() &gt;= 1) {
categoryMatch = true;
}
else if(twos.size() &gt;= 1 &amp;&amp; threes.size() &gt;= 1 &amp;&amp; fours.size() &gt;= 1 &amp;&amp; fives.size() &gt;= 1) {
categoryMatch = true;
}
else if(threes.size() &gt;= 1 &amp;&amp; fours.size() &gt;= 1 &amp;&amp; fives.size() &gt;= 1 &amp;&amp; sixes.size() &gt;= 1) {
categoryMatch = true;
}
}
}
return categoryMatch;
}
```

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