Results 1 to 7 of 7
- 01-15-2008, 06:59 PM #1
Member
- Join Date
- Jan 2008
- Posts
- 7
- Rep Power
- 0
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!
Java 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, 09:37 AM #2
Hello RLRExtra
In stead of having an integer for each number for each player, why not have an integer arrays for each player?
The arrays will then serve as counters. Now you can iterate through the two arrays like this:Java Code:int[] humanCount = new int[6]; int[] cpuCount = new int[6];
Just check this code by a compiler. I hope this helped. :DJava 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 }Eyes dwelling into the past are blind to what lies in the future. Step carefully.
- 01-16-2008, 11:57 AM #3
Member
- Join Date
- Jan 2008
- Posts
- 7
- Rep Power
- 0
Yes, that works great after a few tweaks ::D . Thanks a lot! I'm not to good at working out logic :>
- 01-16-2008, 12:20 PM #4
I'm glad that helped. Welcome to Java Forums! ;)
Eyes dwelling into the past are blind to what lies in the future. Step carefully.
- 01-16-2008, 12:37 PM #5
Member
- Join Date
- Jan 2008
- Posts
- 7
- Rep Power
- 0
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, 01:46 PM #6
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? :pLast edited by tim; 01-16-2008 at 01:48 PM.
Eyes dwelling into the past are blind to what lies in the future. Step carefully.
- 01-17-2008, 04:11 PM #7
Member
- Join Date
- Jan 2008
- Posts
- 7
- Rep Power
- 0
Similar Threads
-
Making a stack from a LinkedList
By Java Tip in forum java.langReplies: 0Last Post: 04-16-2008, 10:28 PM -
Making triangle
By banie in forum New To JavaReplies: 4Last Post: 02-02-2008, 11:23 AM -
Making a frame not re sizable
By Java Tip in forum Java TipReplies: 0Last Post: 12-21-2007, 08:39 AM -
Making Plugins for Eclipse
By javaplus in forum New To JavaReplies: 0Last Post: 12-17-2007, 08:31 AM -
Making a session in swing
By sandor in forum AWT / SwingReplies: 3Last Post: 04-22-2007, 10:58 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks