# Help with making this algorithm better

• 01-15-2008, 07:59 PM
RLRExtra
Help with making this algorithm better
Hello! I've created a small game where a human player and the computer both roll 5 dice each. The player that gets the best hand wins.

To work out the winning hands, the rolls get taken to a class called referee.

Winning hands would be who ever gets three, four or five of the same number.

At the moment my referee class works but seems quite complicated and was wondering if there was a better way of doing it.

Thanks!

Code:

class Referee
{
static int judgeHand(int[] humanHand, int[] cpuHand)// compares the 2 players dice rolls
{
int a = 0;
int b = 0;
int c = 0;
int d = 0;
int e = 0;
int f = 0;
int g = 0;
int h = 0;
int i = 0;
int j = 0;
int k = 0;
int l = 0;
int score = 0;
int scorer = 0;

for(int ray = 0; ray!=humanHand.length ; ray++)// will find the frequency of the numbers from the rolls.
{
if (humanHand[ray] == 1)
{
a = a + 1;
}
else if (humanHand[ray] == 2)
{
b = b + 1;
}
else if (humanHand[ray] == 3)
{
c = c + 1;
}
else if (humanHand[ray] == 4)
{
d = d + 1;
}
else if (humanHand[ray] == 5)
{
e = e + 1;
}
else if (humanHand[ray] == 6)
{
f = f + 1;
}
}

for(int ray = 0; ray!=cpuHand.length ; ray++)
{
if (cpuHand[ray] == 1)
{
g = g + 1;
}
else if (cpuHand[ray] == 2)
{
h = h + 1;
}
else if (cpuHand[ray] == 3)
{
i = i + 1;
}
else if (cpuHand[ray] == 4)
{
j = j + 1;
}
else if (cpuHand[ray] == 5)
{
k = k + 1;
}
else if (cpuHand[ray] == 6)
{
l = l + 1;
}
}

if (a == 3 || b == 3 || c == 3 || d == 3 || e == 3 || f == 3)// will give the scoring method a way of ranking the rolls
{
score = score + 1;
}
else if (a == 4 || b == 4 || c == 4 || d == 4 || e == 4 || f == 4)
{
score = score + 2;
}
else if (a == 5 || b == 5 || c == 5 || d == 5 || e == 5 || f == 5)
{
score = score + 3;
}

if (g == 3 || h == 3 || i == 3 || j == 3 || k == 3 || l == 3)
{
scorer = scorer + 1;
}
else if (g == 4 || h == 4 || i == 4 || j == 4 || k == 4 || l == 4)
{
scorer = scorer + 2;
}
else if (g == 5 || h == 5 || i == 5 || j == 5 || k == 5 || l == 5)
{
scorer = scorer + 3;
}

if(score > scorer) // compares the rolls rank to check who wins the round
{
score = score - score + 1;
System.out.println("you won that round");
}
else if(scorer > score)
{
scorer = scorer - scorer + 1;
System.out.println("you lost that round");
}
else
{
score = score - score;
scorer = scorer - scorer;
System.out.println("unlucky roll again");
}

return score;

}
}

• 01-16-2008, 10:37 AM
tim
Hello RLRExtra

In stead of having an integer for each number for each player, why not have an integer arrays for each player?
Code:

int[] humanCount = new int[6];
int[] cpuCount = new int[6];

The arrays will then serve as counters. Now you can iterate through the two arrays like this:
Code:

// Will find the frequency of the numbers from the rolls.
for (int ray = 0; ray < humanHand.length ; ray++){
int index = humanHand[ray] - 1;
humanCount[index]++;
}
for (int ray = 0; ray < cpuHand.length ; ray++){
int index = cpuHand[ray] - 1;
cpuCount[index]++;
}
// Now look for the winner
int humanScore = 0;
int cpuScore = 0;
for (int i = 0; i < humanCount.length; i++){
switch (humanCount[i]){
case 3 :
humanScore += 1;
break;
case 4 :
humanScore += 2;
break;
case 5 :
humanScore += 3;
break;
}
}
for (int i = 0; i < cpuCount.length; i++){
switch (cpuCount[i]){
case 3 :
cpuScore += 1;
break;
case 4 :
cpuScore += 2;
break;
case 5 :
cpuScore += 3;
break;
}
}
if (humanScore  > cpuScore ){
// player wins
} else {
// player did not win
}

Just check this code by a compiler. I hope this helped. :D
• 01-16-2008, 12:57 PM
RLRExtra
Yes, that works great after a few tweaks ::D . Thanks a lot! I'm not to good at working out logic :>
• 01-16-2008, 01:20 PM
tim
I'm glad that helped. Welcome to Java Forums! ;)
• 01-16-2008, 01:37 PM
RLRExtra
Hehe, nice place to be. Trying to make sense of what you've posted now. Was just wondering what this part does:

int index = humanHand[ray] - 1;

i don't get what the -1 is? Could you explain?
• 01-16-2008, 02:46 PM
tim
Arrays in Java work with zero based indexing, meaning that elements are indexed 0, 1, 2, 3, 4, 5. In your dice program, your dice are numbered using one based indexing: 1, 2, 3, 4, 5, 6. The "-1" is necessary to convert from one based indexing to zero based indexing. For example:
If the human player played a six (6), then one must be added to the sixth element in the humanCount array. If you use six as an index you will see that an Exception is thrown that says that you are trying to access an element outside the bounds of an array. So, one is subtracted to give 5, which is the last element of the array.

Anything else? :p
• 01-17-2008, 05:11 PM
RLRExtra
Haha, nah you been big help thanks! Think I'll try some GUI now.