Question: please help with the Knight class segment of this assignment. Here are the additional files necessary to complete the Knight class BackTrack.java: import java.util.*; public
please help with the Knight class segment of this assignment.




Here are the additional files necessary to complete the Knight class
BackTrack.java:
import java.util.*; public class BackTrack { protected Application app; /** * Initializes this BackTrack object from an application. * * @param app the application */ public BackTrack (Application app) { this.app = app; } // constructor /** * Attempts to reach a goal through a given position. * * @param pos the given position. * * @return true if the attempt succeeds; otherwise, false. */ public boolean tryToReachGoal (Position pos) { Iterator itr = app.iterator (pos); while (itr.hasNext()) { pos = itr.next(); if (app.isOK (pos)) { app.markAsPossible (pos); if (app.isGoal (pos) || tryToReachGoal (pos)) return true; app.markAsDeadEnd (pos); } // pos may be on a path to a goal } // while return false; } // method tryToReachGoal } // class BackTrack
Position.java:
public class Position { protected int row, column; /** * Initializes this Position object to (0, 0). */ public Position () { row = 0; column = 0; } // default constructor /** * Initializes this Position object to (row, column). * * @param row the row this Position object has been initialized to. * @param column the column this Position object has been initialized to. */ public Position (int row, int column) { this.row = row; this.column = column; } // constructor /** * Determines the row of this Position object. * * @return the row of this Position object. */ public int getRow () { return row; } // method getRow /** * Determines the column of this Position object. * * @return the column of this Position object. */ public int getColumn () { return column; } // method getColumn } // class Position Application interface:
import java.util.*; public interface Application { /** * Determines if a given position is legal and not a dead end. * * @param pos - the given position. * * @return true if pos is a legal position and not a dead end. */ boolean isOK (Position pos); /** * Indicates that a given position is possibly on a path to a goal. * * @param pos the position that has been marked as possibly being on a * path to a goal. */ void markAsPossible (Position pos); /** * Indicates whether a given position is a goal position. * * @param pos the position that may or may not be a goal position. * * @return true if pos is a goal position; false otherwise. */ boolean isGoal (Position pos); /** * Indicates that a given position is not on any path to a goal position. * * @param pos the position that has been marked as not being on any path to * a goal position. */ void markAsDeadEnd (Position pos); /** * Converts this Application object into a String object. * * @return the String representation of this Application object. */ String toString(); /** * Produces an Iterator object that starts at a given position. * * @param pos the position the Iterator object starts at. * * @return an Iterator object that accesses the positions directly * available from pos. */ Iterator iterator (Position pos); } // interface Application
Objective: This assignment will help you understand the backtracking design pattern and the Iterator interface by implementing the knight's tour game. This game requires a knight to be moved from any starting square of a chessboard to each of the other squares without landing on the same square more than once. See http://en.wikipedia.org/wiki/Knight's_tour for historical references on this problem. Program assignment Develop and test a program to show the moves of a knight in traversing differently sized chess boards (Programming Project 5.3 in textbook). Create a Java project project 3 and a package package3. From the Maze Problem source files, use without modifications: class Backtrack, class Position, and interface Application. You may download these source files from the shared directory, or from the book's website. Design and implement the following classes: Knight (implementing interface Application and with inner class KnightIterator). KnightUser: This will be your driver class and should have the following header: o O c. 1. Defined Knight class a. Class implements interface Application... b. Has constructor with 3 parameters: x, y (grid size) & starting cell. Has method getStart: returns start position. d. Has method getGrid: returns state of tour. Has method isok: postion is legal... f. Has method is Goal: reached end of tour. g Has method markAsPossible: possible move toward goal. h. Has method markAs DeadEnd: retract move... i. Has method toString..... e. su u un ir u u uru Objective: This assignment will help you understand the backtracking design pattern and the Iterator interface by implementing the knight's tour game. This game requires a knight to be moved from any starting square of a chessboard to each of the other squares without landing on the same square more than once. See http://en.wikipedia.org/wiki/Knight's_tour for historical references on this problem. Program assignment Develop and test a program to show the moves of a knight in traversing differently sized chess boards (Programming Project 5.3 in textbook). Create a Java project project 3 and a package package3. From the Maze Problem source files, use without modifications: class Backtrack, class Position, and interface Application. You may download these source files from the shared directory, or from the book's website. Design and implement the following classes: Knight (implementing interface Application and with inner class KnightIterator). KnightUser: This will be your driver class and should have the following header: o O c. 1. Defined Knight class a. Class implements interface Application... b. Has constructor with 3 parameters: x, y (grid size) & starting cell. Has method getStart: returns start position. d. Has method getGrid: returns state of tour. Has method isok: postion is legal... f. Has method is Goal: reached end of tour. g Has method markAsPossible: possible move toward goal. h. Has method markAs DeadEnd: retract move... i. Has method toString..... e. su u un ir u u uru
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
