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:
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:
Back
Top Bottom