我正在编写一个 boggle 游戏板求解器。它使用堆栈,从 .dat/.txt 文件中读取的游戏板字母的 2D 阵列网格,并搜索“状态”来存储它的具体位置(点坐标,到目前为止的单词)。它旨在搜索板上所有可能的字母组合以形成长度为 3 或更多的字符串,然后检查字典文件以查看该单词是否是有效的单词解决方案。之后,它存储单词并返回参数中给出的游戏板的解决方案列表。
我的问题:由于某种原因,这个程序像我以前写过的其他东西一样让我感到困惑。我对“状态”的概念很陌生,所以这可能是一个潜在的问题。我相信我所拥有的已经非常接近工作了,我只是不知道它可能出了什么问题。当前的问题是它不会在检查相邻字母时存储当前字母并构建字符串。它确实会正确检查邻居,但没有构建任何字符串。这是代码:
BoggleSearch:包含main方法,充当驱动类。
import java.io.*;
import java.util.*;
public class BoggleSearch {
protected static int GRID_SIZE = 4;
public static String[][] grid = new String[GRID_SIZE][GRID_SIZE];
public static void main(String[] args) throws FileNotFoundException {
if (args.length != 1) {
System.err.println("Usage: java BoggleSearch gridFile");
System.exit(1);
}
Scanner scan = new Scanner(new File(args[0]));
String bigString = scan.next();
bigString = bigString+scan.next();
bigString = bigString+scan.next();
bigString = bigString+scan.next();
scan.close();
int count = 0;
for (int i = 0; i < GRID_SIZE; i++) {
for (int j = 0; j < GRID_SIZE; j++) {
grid[i][j] = bigString.substring(count, count);
count++;
}
}
WordSearch ws = new WordSearch(grid);
ArrayList<BoggleSearchState> foundWords = ws.startSearch();
System.out.println(foundWords);
}
}
WordSearch:包含所有算法,这些算法在给定的游戏板上找到每个可能的字母组合,并与字典类进行交叉检查。
import java.awt.Point;
import java.util.*;
public class WordSearch {
public static Stack<BoggleSearchState> stack;
public static ArrayList<BoggleSearchState> foundWords;
private String[][] grid;
private static final int GRID_SIZE = 4;
public BoggleDictionary dictionary;
public WordSearch(String[][] inputGrid) {
grid = new String[GRID_SIZE][GRID_SIZE];
stack = new Stack<BoggleSearchState>();
foundWords = new ArrayList<BoggleSearchState>();
inputGrid = new String[GRID_SIZE][GRID_SIZE];
try {
dictionary = new BoggleDictionary();
} catch (Exception e) {
System.err.println("blew up while making dict object");
e.printStackTrace();
}
}
public ArrayList<BoggleSearchState> startSearch() {
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid.length; j++) {
BoggleSearchState b = new BoggleSearchState(
new ArrayList<Point>(), grid[i][j]);
Point p = new Point(i, j);
b.path.add(p);
stack.push(b);
while (!stack.isEmpty()) {
BoggleSearchState s = stack.pop();
if (s.getWord().length() >=1 && dictionary.contains(s.getWord())) {
foundWords.add(s);
}
Point loc = s.path.get(s.path.size() - 1);
p = new Point(loc.x,loc.y);
// Bottom Neighbor
if (loc.x + 1 >= 0 && loc.x + 1 < grid.length && loc.y >= 0
&& loc.y < grid.length) {
if (s.getVisited(new Point(p.x+1,p.y)) != true) {
BoggleSearchState neo = new BoggleSearchState(new ArrayList<Point>(),s.getWord() + grid[loc.x + 1][loc.y]);
neo.path.add(new Point(p.x+1,p.y));
stack.push(neo);
}
}
// Top Neighbor
if (loc.x - 1 >= 0 && loc.x - 1 < grid.length && loc.y >= 0
&& loc.y < grid.length) {
if (s.getVisited(new Point(p.x-1,p.y)) != true) {
BoggleSearchState neo = new BoggleSearchState(
new ArrayList<Point>(),s.getWord() +
grid[loc.x - 1][loc.y]);
neo.path.add(new Point(p.x-1,p.y));
stack.push(neo);
}
}
// Right Neighbor
if (loc.x >= 0 && loc.x < grid.length && loc.y + 1 >= 0
&& loc.y + 1 < grid.length) {
if (s.getVisited(new Point(p.x,p.y+1)) != true) {
BoggleSearchState neo = new BoggleSearchState(
new ArrayList<Point>(),s.getWord() +
grid[loc.x][loc.y + 1]);
neo.path.add(new Point(p.x,p.y+1));
stack.push(neo);
}
}
// Left Neighbor
if (loc.x >= 0 && loc.x < grid.length && loc.y - 1 >= 0
&& loc.y - 1 < grid.length) {
if (s.getVisited(new Point(p.x,p.y-1)) != true) {
BoggleSearchState neo = new BoggleSearchState(
new ArrayList<Point>(),s.getWord() +
grid[loc.x][loc.y - 1]);
neo.path.add(new Point(p.x,p.y-1));
stack.push(neo);
}
}
// Bottom-Right Neighbor
if (loc.x + 1 >= 0 && loc.x + 1 < grid.length
&& loc.y + 1 >= 0 && loc.y + 1 < grid.length) {
if (s.getVisited(new Point(p.x+1,p.y+1)) != true) {
BoggleSearchState neo = new BoggleSearchState(
new ArrayList<Point>(),s.getWord() +
grid[loc.x + 1][loc.y + 1]);
neo.path.add(new Point(p.x+1,p.y+1));
stack.push(neo);
}
}
// Bottom-Left Neighbor
if (loc.x + 1 >= 0 && loc.x + 1 < grid.length
&& loc.y - 1 >= 0 && loc.y - 1 < grid.length) {
if (s.getVisited(new Point(p.x+1,p.y-1)) != true) {
BoggleSearchState neo = new BoggleSearchState(
new ArrayList<Point>(),s.getWord() +
grid[loc.x + 1][loc.y - 1]);
neo.path.add(new Point(p.x+1,p.y-1));
stack.push(neo);
}
}
// Top-Right Neighbor
if (loc.x - 1 >= 0 && loc.x - 1 < grid.length
&& loc.y + 1 >= 0 && loc.y + 1 < grid.length) {
if (s.getVisited(new Point(p.x-1,p.y+1)) != true) {
BoggleSearchState neo = new BoggleSearchState(
new ArrayList<Point>(),s.getWord() +
grid[loc.x - 1][loc.y + 1]);
neo.path.add(new Point(p.x-1,p.y+1));
stack.push(neo);
}
}
// Top-Left Neighbor
if (loc.x - 1 >= 0 && loc.x - 1 < grid.length
&& loc.y - 1 >= 0 && -1 < grid.length) {
if (s.getVisited(new Point(p.x-1,p.y-1)) != true) {
BoggleSearchState neo = new BoggleSearchState(
new ArrayList<Point>(),s.getWord() +
grid[loc.x - 1][loc.y - 1]);
neo.path.add(new Point(p.x-1,p.y-1));
stack.push(neo);
}
}
}
}
}
return foundWords;
}
}
BoggleSearchState:创建一个状态对象,用于为游戏板上的字符串形成路径的每个实例存储必要的数据。包含其目的所需的方法。
import java.awt.Point;
import java.util.ArrayList;
public class BoggleSearchState {
private String word="";
public ArrayList<Point> path = new ArrayList<Point>();
public BoggleSearchState(ArrayList<Point>path, String word) {
this.path = path;
this.word = word;
}
public String getWord() {
return word;
}
public ArrayList<Point> getLocation() {
return path;
}
public boolean getVisited (Point p) {
ArrayList<Point> newPath = new ArrayList<Point>();
for (Point s: path) {
newPath.add(s);
if (p.equals(s)) {
return true;
}
}
return false;
}
public String toString() {
return this.word;
}
}
BoggleDictionary:为作业提供的写得很糟糕的字典类。然而,它已经过测试并且功能齐全。
// BoggleDictionary.java
import java.io.File;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.io.IOException;
import java.util.Scanner;
import java.util.HashSet;
import java.util.Iterator;
/**
A class that stores a dictionary containing words that can be used in a
Boggle game.
@author Teresa Cole
@version CS221 Fall 2013
*/
public class BoggleDictionary
{
private HashSet<String> dictionary;
/** Create the BoggleDictionary from the file dictionary.dat
*/
@SuppressWarnings("unchecked")
public BoggleDictionary() throws Exception {
ObjectInputStream dictFile = new ObjectInputStream(
new FileInputStream( new File( "dictionary.dat")));
dictionary = (HashSet<String>)dictFile.readObject();
dictFile.close();
}
/** Check to see if a string is in the dictionary to determine whether it
* is a valid word.
* @param word the string to check for
* @return true if word is in the dictionary, false otherwise.
*/
public boolean contains( String word)
{
return dictionary.contains( word);
}
/** Get an iterator that returns all the words in the dictionary, one at a
* time.
* @return an iterator that can be used to get all the words in the
* dictionary.
*/
public Iterator<String> iterator()
{
return dictionary.iterator();
}
/**
Main entry point
*/
static public void main(String[] args)
{
System.out.println( "BoggleDictionary Program ");
Scanner kbd = new Scanner( System.in);
BoggleDictionary theDictionary=null;
try
{
theDictionary = new BoggleDictionary();
}
catch (Exception ioe)
{
System.err.println( "error reading dictionary");
System.exit(1);
}
String word;
/*
while (kbd.hasNext())
{
word = kbd.next();
if (theDictionary.contains( word))
System.out.println( word + " is in the dictionary");
else
System.out.println( word + " is not in the dictionary");
}
*/
Iterator<String> iter = theDictionary.iterator();
while (iter.hasNext())
System.out.println( iter.next());
}
}
我将不胜感激这方面的任何帮助,因为此时我真的很挣扎。我知道有很多方法可以实现其他数据结构或组织方法,以便在更有效的运行时完成这项任务。然而,作业的关注点不在于效率,而是使用这些数据结构(堆栈等)的基本原则,并了解如何在没有程序的情况下安全地回到错误的方向并走向新的方向崩溃。在此先感谢您,任何问题我都会尽快回答。