// Piece.java -- define a piece class. For now, it's just the rook // and knight. Can you find the bugs? Why is there an infinite loop? public class Piece { private char rank; private int row; private int col; public Piece(char k, int r, int c) { rank = k; row = r; col = c; } // Infinite loop in this function. // ROOK -- go outward in 4 directions, up, down, left, right // for example, if we are at row = 2, col = 3 // 1,3 // 2,1 2,2 *** 2,4 2,5 2,6 2,7 2,8 // 3,3 // 4,3 // 5,3 // 6,3 // 7,3 // 8,3 // Note that (row,col) is the current position of the rook // and that (r,c) is a possible destination. public void findRookMoves() { int r, c; // go up for (r = row - 1; r >= 1; --r) System.out.println(r + "," + col); // go down for (r = row + 1; row <= 8; ++r) System.out.println(r + "," + col); // go left for (c = col - 1; col >= 1; --c) System.out.println(row + "," + c); // go right for (c = col + 1; col <= +8; ++c) System.out.println(row + "," + c); } // Infinite loop in this function. // KNIGHT -- go 2 up and 1 over, or some other combo of 2 and 1 void findKnightMoves() { int i, j; for (i = 1; i <= 8; ++i) for (j = 1; j <= 8; ++i) if (two_and_one (i, j, row, col)) System.out.println(i + "," + j); } // there are no bugs in this function -- it determines if // the square (row2, row2) can be reached by a knight from (row1, col1) public boolean two_and_one (int row1, int col1, int row2, int col2) { int row_diff, col_diff; row_diff = row1 - row2; if (row_diff < 0) row_diff = -row_diff; col_diff = col1 - col2; if (col_diff < 0) col_diff = -col_diff; if (row_diff == 2 && col_diff == 1 || row_diff == 1 && col_diff == 2) return true; else return false; } }