***I'm starting this new series which will highlight code that I think is pretty/gorgeous/mind-blowing or all of the above. Generally it will be simple cases, and most of it will be Java only because of my limited knowledge of coding languages. Feel free to submit code to me (Using Pastie works best I believe) or to correct code highlighted in these segments.***
findPairs()
I'm making a dice game for android with a close friend of mine. The game is called Farkle and its highly addictive. A player needs to roll 6 die and choose the best combinations to score the most points. This method I just created will return the positions of other dice with the same value. The method came out very clean and I'm extremely proud of it. Even if it's extremely simple
public int[] findPairs(int index, int value) {
int[] dirtyArray = { 0, 0, 0, 0, 0, 0 };
int count = 0;
for (int j = 0; j < dieValues.length; j++) {
if (j != index && dieValues[j] == value) {
dirtyArray[count++] = j;
}
}
if (count == -1) return null;
int[] cleanArray = new int[count];
System.arraycopy(dirtyArray, 0, cleanArray, 0, count);
return cleanArray;
}
Edit: Teehee made a mistake and just found it. Guess that's what happens when you post "Beautiful" code before testing. Note to self: variable++ returns the variable before it got incremented
Double Edit: And another one! It's perfect now (I promise)
Edit: Teehee made a mistake and just found it. Guess that's what happens when you post "Beautiful" code before testing. Note to self: variable++ returns the variable before it got incremented
Double Edit: And another one! It's perfect now (I promise)
No comments:
Post a Comment
Hey There! Please leave a comment, suggestion, or the occasional flame (but only if its informational!)