Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-15-2008, 07:59 PM
Member
 
Join Date: Jan 2008
Posts: 7
RLRExtra is on a distinguished road
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; } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 01-16-2008, 10:37 AM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 334
tim is on a distinguished road
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.
__________________
If your ship has not come in yet then build a lighthouse.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 01-16-2008, 12:57 PM
Member
 
Join Date: Jan 2008
Posts: 7
RLRExtra is on a distinguished road
Yes, that works great after a few tweaks : . Thanks a lot! I'm not to good at working out logic :>
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 01-16-2008, 01:20 PM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 334
tim is on a distinguished road
I'm glad that helped. Welcome to Java Forums!
__________________
If your ship has not come in yet then build a lighthouse.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 01-16-2008, 01:37 PM
Member
 
Join Date: Jan 2008
Posts: 7
RLRExtra is on a distinguished road
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?
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 01-16-2008, 02:46 PM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 334
tim is on a distinguished road
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?
__________________
If your ship has not come in yet then build a lighthouse.

Last edited by tim : 01-16-2008 at 02:48 PM.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 01-17-2008, 05:11 PM
Member
 
Join Date: Jan 2008
Posts: 7
RLRExtra is on a distinguished road
Haha, nah you been big help thanks! Think I'll try some GUI now.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Making a stack from a LinkedList Java Tip java.lang 0 04-16-2008 11:28 PM
Making triangle banie New To Java 4 02-02-2008 12:23 PM
Making a frame not re sizable Java Tip Java Tips 0 12-21-2007 09:39 AM
Making Plugins for Eclipse javaplus New To Java 0 12-17-2007 09:31 AM
Making a session in swing sandor AWT / Swing 3 04-22-2007 11:58 PM


All times are GMT +3. The time now is 12:23 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org