Results 1 to 7 of 7
  1. #1
    RLRExtra is offline Member
    Join Date
    Jan 2008
    Posts
    7
    Rep Power
    0

    Default 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;
    	
    	}
    }

  2. #2
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Default

    Hello RLRExtra

    In stead of having an integer for each number for each player, why not have an integer arrays for each player?
    Java 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:
    Java 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
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  3. #3
    RLRExtra is offline Member
    Join Date
    Jan 2008
    Posts
    7
    Rep Power
    0

    Default

    Yes, that works great after a few tweaks ::D . Thanks a lot! I'm not to good at working out logic :>

  4. #4
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Default

    I'm glad that helped. Welcome to Java Forums! ;)
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  5. #5
    RLRExtra is offline Member
    Join Date
    Jan 2008
    Posts
    7
    Rep Power
    0

    Default

    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?

  6. #6
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Default

    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
    Last 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.

  7. #7
    RLRExtra is offline Member
    Join Date
    Jan 2008
    Posts
    7
    Rep Power
    0

    Default

    Haha, nah you been big help thanks! Think I'll try some GUI now.

Similar Threads

  1. Making a stack from a LinkedList
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-16-2008, 10:28 PM
  2. Making triangle
    By banie in forum New To Java
    Replies: 4
    Last Post: 02-02-2008, 11:23 AM
  3. Making a frame not re sizable
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-21-2007, 08:39 AM
  4. Making Plugins for Eclipse
    By javaplus in forum New To Java
    Replies: 0
    Last Post: 12-17-2007, 08:31 AM
  5. Making a session in swing
    By sandor in forum AWT / Swing
    Replies: 3
    Last Post: 04-22-2007, 10:58 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •