I recently started learning Java for fun but got stuck at the following problem: I have 2 arrays int[] that holds a representation of a poker hand. Position 0 in the array tells if it is a straight flush and the number in there tells the value of the high card. Position 1 in the array tells if its for of a kind and the value of the card so 5 here means you have fourofakind in 5:s and so on.
So I want to compare all player arrays first position 0 (for straight flush) and the player that has highest number wins.. if all have 0 or 2 or more player have equal numbers step to position 1 in the array and compare and so on.
To simplify the code I tried to do a compare function that compares the hands of 2 players and returns if hand1 (in code array1) is higher than hand 2 (array 2)
I did do the following code (that doesn't work):
int isBetterHand(int[] array1, int[]array2){ //private
if(array1.length > array2.length){ throw new RuntimeException("arrayCompare got arrays of different length.");}
int arrayLength = array1.length;
for(int i = 0; i < arrayLength; i++){
if(array1[i] > array2[i]){
return 1;
} else if(array1[i] < array2[i]){
return -1;
} else {
return 0;
}
}
return 0;
}
I figure in Java you cant write if array1[0] (comparator like <, >, ==) array2[0]
How do I fix the code above? Also is there a faster, better, smarter way to do this? The arrays are 26 numbers long. I need co compare max 8 arrays.
Say I have 5 players left in game I could get a matrix like (I only type in first 5 positions:
P1 0, 0, 0, 0, 0, ... P2 0, 0, 0, 0, 0, ... P3 0, 2, 0, 0, 0, ... P4 0, 0, 13, 3, 0, ... P5 0, 0, 0, 0, 14, ...
In the ex above pos[0] means highcard in straightflush noone has that in pos[1] it shows P3 is the winner with four of a kind in 2:s ((pos[2]&[3] shows P4 has full house K over 3 (but P3 4 of a kind is better...)))
Edit: Just wow! I didn't expect answers this fast. Now its working like a charm. A BIG! thank you to all who helped me.
I have to go more indepth on the suggestions for comparing the matrix of arrays but you guys pointed me in the right direction there.
It looks like you basically have the right idea. The only problem is the early return of 0
from the loop. While you're looping you should only return 1
or -1
if the corresponding array elements aren't equal. Only once you've exhausted the entire array can you conclude that they are equal, and return 0
:
for (int i = 0; i < arrayLength; i++) {
if (array1[i] > array2[i]) {
return 1;
} else if (array1[i] < array2[i]) {
return -1;
}
// else claus removed here
}
return 0;
Edit:
As Andy Turner mentioned in the comment, you should also check that the two arrays are exactly the same length, not just that the first one isn't longer than the second. Putting this all together:
int isBetterHand(int[] array1, int[]array2) {
i f(array1.length != array2.length) {
// Use != ----^
throw new RuntimeException("isBetterHandgot arrays of different length.");
}
int arrayLength = array1.length;
for (int i = 0; i < arrayLength; i++) {
if (array1[i] > array2[i]) {
return 1;
} else if (array1[i] < array2[i]) {
return -1;
}
// else clause removed here
}
return 0;
}