working with arrays

Associate
Joined
12 Jun 2003
Posts
898
Location
Wicklow, Ireland
i'm trying to develop this very rudimentary dice game in java that gives the player five values (out of 6 from a dice) at random and then do scoring based on what values their dice have.

i'm using standard poker hands like pair/three of a kind/two pair/full house (pair and a three of a kind)/ double (two pairs) / four of a kind / five of a kind / straight (1,2,3,4,5/2,3,4,5,6).

i'm having problems trying to figure out algorithms for these except for five of a kind as that was easy.

Code:
if(face_values[0] == face_values[1] && face_values[1] == face_values[2] && face_values[2] == face_values[3] &&
			face_values[3] == face_values[4]){
		  	System.out.println("FIVE OF A KIND!!!!!");
}

for the others i was thinking of sorting the array first by using sort(face_values); then trying to figure out if there's a score there.

what i want to know is when i sort the array using the sort method does it actually move the values around.

ie if i have an array like this

Code:
a[0] a[1] a[2] a[3] a[4]
  3    4    6    2    4

will sort make it like this?

Code:
a[0] a[1] a[2] a[3] a[4]
  2    3    4    4    6

what would be the best way to check for these combinations?
 
Last edited:
Yes

Code:
Unsorted: 
[ 9 7 8 6 5 ]
Sorted: 
[ 5 6 7 8 9 ]

Produced by the code:
Code:
int[] face_values = new int[] {9,7,8,6,5};
System.out.println("Unsorted: ");
System.out.println(arrayToString(face_values));
java.util.Arrays.sort(face_values);
System.out.println("Sorted: ");
System.out.println(arrayToString(face_values));

The method arrayToString(int[]) simply iterates over an array of integers and returns a string representation.
 
The method arrayToString(int[]) simply iterates over an array of integers and returns a string representation.
If you are using Java 1.5 you can use java.util.Arrays.toString(array).

/edit - Oops just notice you weren't asking for help with the algorithms :o

/edit2 - just noticed you were so I'll put it back in again :o :o


First I'd check for a flush. You could do this with a loop that iterates through your sorted array of dice rolls and checks if each successive element is 1 larger than the previous.

what pops to mind for the others is:

1) count the number of occurrences of each number.
For the roll 2,1,4,2,2 we would get
1,2,3,4,5,6
1,3,0,1,0,0

an array indexed by the roll would work for this
array[dice roll]++; //same as doing array[2] = array[2] +1;

Don't forget to initialise all the elements of your array to 0 first or you'll get NullPointerExceptions :)

2) search this for our hands
Code:
if numberOfOccurences(5) == 1 {
    //five of a kind!
}

if numberOfOccurences(3) == 1 {
    if (numberOfOccurences(2) == 1 {
        //full house
    } else {
        //3 of a kind
    }
}
and so on.

numberOfOccurences() just searches through the datastructure from 1) counting the number of occurrences of the number you give it. Using the above example 3 would return 1, 1 would return 2 and all the other numbers 0.
 
Last edited:
sorry the first code example i posted was checking for five of a kind, just noticed now, and have edit the original post.

i have this method for doing a straight.

Code:
public void straight(){
		face_values();
		java.util.Arrays.sort(face_value);
		for(int i=0 ; i < face_value.length ; i++){
			if(face_value[i] < face_value[i+1])
				System.out.println("STRAIGHT");

		}

	}

face_values() is a method that inserts the values into the array that i'm working with as the die values are contained in another class.

i'm getting a ArrayIndexOutOfBoundsException though when i call the straight method.


for the other you're suggesting i...
1. set up an array of 7 elements and initialise it to zero.
2. count the occurences
3. use a method called numberOfOccurences()

i'm not fully sure how that numberOfOccurences() works what's the parameter in relation to and why do you check that it's == to one?
 
Last edited:
Here is a sample implementation of the method numberOfOccurences()
Code:
public int numberOfOccurences(int num){
	int count = 0;
	for (int i : face_values){
		if (i == num){
			count++;
		}
	}
	return count;
}
 
Hi,

Another approach you could take would be to sort the dice numbers and then form them into a string. You would then use regular expressions to find combinations.

For example, if the dice were assembled into the string "122345" then the reg exp:

/(\d)\1/

would be used to locate a pair. The \d detects a number and the brackets put it into the \1 backreference. So, the expression looks for a number followed immediately by the same number.

Following on, the regexp:

/(\d)\1{2}/

could be used to find three of a kind and so on. By using different regexps you can detect complex sequences without doing lots of coding for each specific situation.

Hope that helps.

Jim
 
I was hoping someone would post about regular expressions :) They may as well be double dutch to me but they are so useful and you do look like a god when literate in them. That or an unwashed perl monger :p

Yep, RobH's implementation was what I was envisioning. It will answer the question "How many 5 of a kinds have I got?" or "How many 2 of a kinds have I got?". I check "==1" as I know there will either be 1 or 0 (we've not rolled enough dice for two 5 of a kinds).

If numberOfOccurences(2) == 2 //we have no pairs
If numberOfOccurences(2) == 1 // we have no pair
If numberOfOccurences(2) == 0 // we rolled no pairs

It doesn't say anything about what number we actually rolled a pair of.

I was trying to balance saying too little/too in my first post so I hope it's clearer now.

i'm getting a ArrayIndexOutOfBoundsException though when i call the straight method.
The reason for this is that your loop iterates through to your last element then checks it against the next element which does not exist. Stop the loop at (length-1) and remember to write comments as you write your code otherwise no one will know the reasoning for this limit :)

Also, your loop checks that face_value < face_value[i+1]. This would allow "12456". You need to check (face_value+1) == face_value[i+1] but that looks rather confusing.

An alternative could be to check the face value against a variable that increases by one for each time round the loop.

Code:
private Boolean isStraight() {
	// This method checks for a straight. It returns true 
	// if all values in face_values are consecutive.
	// For example 12345 or 23456
	//note: this assumes you have sorted face_values somewhere else.
	
	//starts at the value of our first roll
	int predictedValue = face_values[0];
	
	for (int i : face_values) {
		if (i != predictedValue) {
			return false;
		}
		predictedValue++;
	}
	
	return true;
}

Please ask if you have any questions. I'm rather enjoying refreshing my memory :) I was last a Java programmer in 2001.
 
Back
Top Bottom