I'm posting the code of a game that I wrote recently. Game name is FifteenPuzzle or N-Puzzle.
You can download the file here.
File Name: FifteenPuzzle.java
import java.awt.*; |
Comments and suggestions will be great.
-Anil Madamala
computer programming
Please feel free to mail me suggestions.
import java.awt.*; |
// Example of normal loop i=0 while ( i < count ) { a[ i ] = i; i++; } |
// Loop unrolled once i = 0; while ( i < count -1 ) { a[ i ] = i; a[ i + 1 ] = i + 1; // Unrolling once i = i + 2; } if ( i == count) { // These lines pick up the case a[ i - 1 ] = i - 1;// that might fall through the } // cracks if the loop went by // twos instead of by ones |
// Loop unrolled twice i = 0; while ( i < count - 2 ) { a[ i ] = i; a[ i + 1 ] = i+1; a[ i + 2 ] = i+2; i = i + 3; } if ( i <= count - 1 ) { a[ count - 1 ] = count - 1; } if ( i == count - 2 ) { a[ count -2 ] = count - 2; } |
for ( Iterator i = c.iterator(); i.hasNext(); i++ ) { doSomething ( i.next() ) ; } |
Iterator i = c.iterator(); while (i.hasNext()) { doSomething(i.next()); } ... Iterator i2 = c2.iterator(); while (i.hasNext()) { // BUG! doSomethingElse(i2.next()); } |
for (Iterator i = c.iterator(); i.hasNext(); ) { doSomething(i.next()); } ... // Compile-time error - the symbol i cannot be resolved for (Iterator i2 = c2.iterator(); i.hasNext(); ) { doSomething(i2.next()); } |
//Common Practice for (int i = 0; i < list.size(); i++) { doSomething(list.get(i)); } // High-performance idiom for iterating over random access lists for (int i = 0, list.size(); i < n; i++) { doSomething(list.get(i)); } |
// Common practice of printing the array in Java for( int i = 0; i < array.length; i++) { System.out.println(array[i]); } // Best way of printing the array in Java System.out.println( Arrays.asList(array) ) ; |
// Loop through an Array using for each for( int intVal : array ) { doSomething(intVal); } |
The bug is in this line: int mid =(low + high) / 2;
1: public static int binarySearch(int[] a, int key) {
2: int low = 0;
3: int high = a.length - 1;
4:
5: while (low <= high) {
6: int mid = (low + high) / 2;
7: int midVal = a[mid];
8:
9: if (midVal < key)
10: low = mid + 1
11: else if (midVal > key)
12: high = mid - 1;
13: else
14: return mid; // key found
15: }
16: return -(low + 1); // key not found.
17: }