我正在编写一个令人难以置信的游戏板求解器。它使用堆栈,一个 2D 数组网格,其中包含从 .dat/.txt 文件中读入的游戏板字母,并搜索"状态"来存储它的位置,特别是(点坐标,到目前为止的单词)。它旨在搜索板上每个可能的字母组合以形成长度为 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);
}
}
单词搜索:包含所有算法,这些算法在给定游戏板上找到每个可能的字母组合,并与字典类交叉检查它们。
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());
}
}
我将不胜感激对此的任何帮助,因为此时我真的很挣扎。我知道有许多方法可以实现其他数据结构或组织方法,以在更高效的运行时完成此任务。然而,分配的关注点不是为了效率,而是使用这些数据结构(堆栈等)的基本原则,并理解你如何能够走错方向,然后安全地回溯并走向一个新的方向,而不会崩溃程序。提前感谢您,任何问题我都会尽快回答。
我所知,您有两个网格WordSearch
,但是您将它们都设置为初始化数组。 您永远不会使用看起来像是在 main 方法中构建的网格。
但很难说。
你给了我们很多数据,但信息很少。 我们不需要有关整个程序的详细信息,即使是这种大小的程序;我们需要知道您的具体问题是什么。 没有人愿意为你调试它,事实上,很少有人会像我一样阅读那么多的内容。
修复初始化问题后,尽可能调试程序,并找出它在哪里执行(1)不应该执行的操作,以及(2)您不理解的操作。 您需要花足够的时间尝试自己找出#1以进行自己的调试教育,这使您更有可能解释您不理解的内容并获得特定问题的良好答案。 "这不是在构建字符串"还远远不够;它在哪里不是在构建,你所说的构建字符串是什么意思,等等。 我希望这是因为它没有输入,但我没有分析那么远。