import java.util.ArrayList; import java.util.Scanner; public class Knight { int board[][]; private class Loc { int x; int y; public Loc(int xx, int yy) { this.x=xx; this.y=yy; } public Loc newLoc(Loc add) { Loc nn = new Loc(x, y); nn.x+=add.x; nn.y+=add.y; return nn; } public boolean onboard() { return x>=0 && x<8 && y>=0 && y<8; } public String toString() { return "<"+x+","+y+">"; } public boolean equals(Loc l) { return l.x==x && l.y==y; } } private ArrayList kmoves; public Knight() { board = new int[8][8]; // Changed here from that showin in class, the inclass version set all board positions to 99999. for (int i=0; i<8; i++) for (int j=0; j<8; j++) board[i][j]=0; kmoves = new ArrayList<>(); kmoves.add(new Loc(2,1)); kmoves.add(new Loc(2,-1)); kmoves.add(new Loc(1,2)); kmoves.add(new Loc(1,-2)); kmoves.add(new Loc(-2,1)); kmoves.add(new Loc(-2,-1)); kmoves.add(new Loc(-1,2)); kmoves.add(new Loc(-1,-2)); } public boolean moveknight(Loc c, Loc target, int remaining) { System.out.println("Considering " + c); if (remaining<=0) return false; for (Loc km : kmoves) { Loc nn = c.newLoc(km); if (nn.equals(target)) { System.out.println("Got to " + target + " via " + c + " with " + (remaining-1) + " move remaining"); return true; } // Note I fixed this from what I dscussed in class where I mumbled about possibly needing to subtract if (nn.onboard() && board[nn.x][nn.y]