1-The final program should accept a the file name of a \"grid file\" from the co
ID: 3605259 • Letter: 1
Question
1-The final program should accept a the file name of a "grid file" from the command line.
1.1 The input file will be a space delimited stream of digits (allowing for easy use the input Scanner).
1.2 Since the grid may be of arbitrary size, the first two integers of the file will indicate the number of ROWS and COLS respectively. (See the example code that I provided). (Using a randomly generated grid with a fixed seed is probably the most convenient way to test during development, however.)
public class BruteForceBoundedDepthExample {
/**
* Construct and return a game using a random number generator..
*
* @return A new instance of a GreedGame object.
*/
static GreedGame createNewGame(int seed, int size) {
// Seed this with any integer to always generate the same board.
final Random rnd = new Random(seed);
// Get a new game object.
return new GreedGame(() -> rnd.nextInt(9) + 1, size, size);
}
/**
* Construct a new game by reading from a file. Assumes that the first two
* integers in the file represent the number of ROWS and COLS respectively.
*
* @param fileName
* @return A new instance of a GreedGame object.
* @throws FileNotFoundException
*/
public static GreedGame createNewGame(String fileName) throws FileNotFoundException {
// "Try with resource" to auto close input file.
try (java.util.Scanner gridFile = new java.util.Scanner(new java.io.File(fileName))) {
int rows = gridFile.nextInt();
int cols = gridFile.nextInt();
return new GreedGame(() -> gridFile.nextInt(), rows, cols);
}
}
/**
* Recursively Explore all children of the parent node DEPTH-FIRST until
* MAX_DEPTH is reached.
*
* @param parentNode the node to explore
* @param depth the current depth.
* @return The result of the best child.
*/
static StateNode tryMoves(final StateNode parentNode, final int depth, final int maxDepth) {
if (depth >= maxDepth)
return parentNode;
StateNode bestSoFar = parentNode;
Direction bestDir = NONE;
// Recursivly Evaluate each child node to MAX_DEPTH.
for (Direction direction : getAvailableDirections(parentNode)) {
System.out.printf("Trying %d:%n%s%n", depth, testMove(parentNode, direction));
StateNode childNode = tryMoves(testMove(parentNode, direction), depth + 1, maxDepth);
if (childNode.getScore() > bestSoFar.getScore()) {
bestSoFar = childNode;
bestDir = direction;
}
bestSoFar.setDirection(bestDir);
}
return bestSoFar;
}
/**
* Test "AI".
*
* @param args
*/
public static void main(String[] args) {
final int MAX_DEPTH = 5;
final int INITIAL_DEPTH = 0;
GreedGame greed = BruteForceBoundedDepthExample.createNewGame(0, 21);
StateNode testMove = testMove(greed.asNode(), N);
greed.makeMove(N);
System.out.println(testMove);
StateNode bestMove = tryMoves(greed.asNode(), INITIAL_DEPTH, MAX_DEPTH);
greed.makeMove(bestMove.getDirection());
System.out.println(greed);
System.out.printf("Potential Score: %d Current Score: %d%n", bestMove.getScore(), greed.getScore());
}
}
Explanation / Answer
public class BruteForceBoundedDepthExample {
static GreedGame createNewGame(int seed, int size) {
// Seed this with any integer to always generate the same board.
final Random rnd = new Random(seed);
// Get a new game object.
return new GreedGame(() -> rnd.nextInt(9) + 1, size, size);
}
public static GreedGame createNewGame(String fileName) throws FileNotFoundException {
// "Try with resource" to auto close input file.
try (java.util.Scanner gridFile = new java.util.Scanner(new java.io.File(fileName))) {
int rows = gridFile.nextInt();
int cols = gridFile.nextInt();
return new GreedGame(() -> gridFile.nextInt(), rows, cols);
}
}
static StateNode tryMoves(final StateNode parentNode, final int depth, final int maxDepth) {
if (depth >= maxDepth)
return parentNode;
StateNode bestSoFar = parentNode;
Direction bestDir = NONE;
// Recursivly Evaluate each child node to MAX_DEPTH.
for (Direction direction : getAvailableDirections(parentNode)) {
System.out.printf("Trying %d:%n%s%n", depth, testMove(parentNode, direction));
StateNode childNode = tryMoves(testMove(parentNode, direction), depth + 1, maxDepth);
if (childNode.getScore() > bestSoFar.getScore()) {
bestSoFar = childNode;
bestDir = direction;
}
bestSoFar.setDirection(bestDir);
}
return bestSoFar;
}
public static void main(String[] args) {
final int MAX_DEPTH = 5;
final int INITIAL_DEPTH = 0;
GreedGame greed = BruteForceBoundedDepthExample.createNewGame(0, 21);
StateNode testMove = testMove(greed.asNode(), N);
greed.makeMove(N);
System.out.println(testMove);
StateNode bestMove = tryMoves(greed.asNode(), INITIAL_DEPTH, MAX_DEPTH);
greed.makeMove(bestMove.getDirection());
System.out.println(greed);
System.out.printf("Potential Score: %d Current Score: %d%n", bestMove.getScore(), greed.getScore());
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.