PDA

View Full Version : Code is doing something funky.



MetKiller Joe
February 2nd, 2009, 08:34 AM
I'm writing a video poker game for a casino program for my 1st semester programming project. I am currently debugging a function which is supposed to sort the poker hand from the greatest rank to the lowest and assign suits to the same card in the process (so there is not information loss).

The function works as follows:

-beforeAfter[n][0] (where n is 0-4) is assigned the original value of rank[n] (in another function)

-using the Array.sort(rank) function the program sorts the array in a ascending order

-the program stores the values at their new index using beforeAfter[n][1]

Comparing beforeAfter[n][0] & [1] the program should assign the value tempSuite[n] (it being the value in its original index) to suite[y] (assigning the value to its new index y). So, you could think of tempSuite as using [0] and suite using [1].

The problem is that when suite[y] is being assigned the value at tempSuite[i], like suite[y] = tempSuite[i], the program is doing another operation, tempSuite[y] = tempSuite[i].





public static void sortHand(){
int[] tempSuite = new int[5];
tempSuite = suite;
java.util.Arrays.sort(rank);
for(int i = 0; i < 5; i++) beforeAfter[i][1] = rank[i];
for(int i = 0; i < 5; i++){
for(int y = 0; y < 5; y++)
if(beforeAfter[i][0] == beforeAfter[y][1]) suite[y] = tempSuite[i];
//why does the program do tempSuite[y] = tempSuite[i]?
}
}

RickYng
February 2nd, 2009, 01:23 PM
tempSuite = suite

Pointer is being passed but the whole thing does not get copied (not 100% sure though).

EDIT: Try this to copy the array. http://www.java-tips.org/java-se-tips/java.lang/how-to-copy-elements-from-one-array-to-another-3.html

MetKiller Joe
February 2nd, 2009, 02:55 PM
That's weird, but it works. Thanks for the help!