我正在编写一个boggle游戏板解算器。它使用Stack,从.dat / .txt文件读入的游戏板字母的2D阵列网格,以及搜索“States”来存储它的位置,特别是(点坐标,到目前为止的单词)。它被设计为在板上搜索每个可能的字母组合以形成长度为3或更多的字符串,然后检查字典文件以查看该字是否是有效的boggle字解决方案。之后,它存储单词并返回参数中给出的游戏板的解决方案列表。
我的问题:出于某种原因,这个程序没有像我以前写过的任何其他东西那样让我望而却步。我对“国家”这个概念很新,所以这可能是一个潜在的问题。我相信我所拥有的是相当接近工作的,我只是因为它可能出错而感到茫然。当前的问题是它不会在检查相邻字母时存储当前字母和构建字符串。它会正确检查邻居,但不会构建任何字符串。这是代码:
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());
}
}
我很感激任何有关这方面的帮助,因为我现在正在努力解决这个问题。我知道有很多方法可以实现其他数据结构或组织方法,以便在更高效的运行时间内完成此任务。然而,分配的关注不是为了提高效率,而是使用这些数据结构(堆栈等)的基本原则,并理解你如何能够走错方向然后安全地回溯并在没有程序的情况下走向新的方向崩溃。提前感谢您,我会尽快回答任何问题。
答案 0 :(得分:2)
尽管我可以告诉你,WordSearch
中有两个网格,但是你将它们都设置为初始化数组。你永远不会使用你可能在main方法中构建的网格。
但很难说。
你给了我们很多数据,但信息很少。我们不需要有关整个程序的详细信息,即使是这样大小的程序;我们需要知道您的具体问题是什么。没有人有责任为你调试这个,事实上很少有人会像我一样阅读它。
在你修复了初始化问题之后,调试你的程序然后你可以找出它做了哪些事情(1)它不应该,以及(2)你不理解。你需要花足够的时间自己弄清楚你自己的调试教育#1,这样你就更有可能解释你不理解的东西并得到一个特定问题的好答案。 “它不是建筑字符串”并不够远;它在哪里构建,你是什么意思建立字符串等我希望它是因为它没有输入,但我没有分析那么远。