当前位置: 首页 > 知识库问答 >
问题:

Boggle游戏板搜索程序问题

谷出野
2023-03-14
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;
}
}
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.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()); 
}

}

我会很感激在这方面的任何帮助,因为我真的在这一点上挣扎。我知道有许多方法可以实现其他数据结构或组织方法,以便在更高效的运行时完成这项任务。然而,对分配的关注并不是为了效率,而是为了使用这些数据结构(堆栈等)的基本原则,以及理解如何能够走错方向,然后安全地返回轨道,并在程序不崩溃的情况下走新的方向。提前谢谢你,任何问题我都会尽可能快地回答。

共有1个答案

周宏胜
2023-03-14

据我所知,在WordSearch中有两个网格,但您将它们都设置为初始化数组。您永远不会使用看起来像在main方法中构建的网格。

但这很难说。

你给了我们很多数据,但信息很少。我们不需要关于整个程序的细节,即使是这么大的程序;我们需要知道你的具体问题是什么。没有人能够为您调试它,事实上,很少有人能像我这样阅读它。

 类似资料:
  • rollDice( )应使用兰德( )随机生成介于1-6之间的数字 返回兰德( )生成的数字 3)实现一个功能,功能原型为Int ;Playgame( Void ;);  根据用户的赢或输的次数给用户一个适当的消息 返回值为EXIT_SUCCESS 这里是我现在拥有的,但它告诉我有错误。有谁能帮我完成这个任务吗? ^ X.C:10:1:错误:程序中杂散“\240” X.C:12:1:错误:程序中杂

  • 每当我打开我的android应用程序,它就会触发三星galaxy S8上的游戏模式/游戏启动器。但这个应用程序不是一个游戏。在以前的版本中,该应用程序使用opengl,如果使用opengl,三星似乎将应用程序视为游戏。我已经删除了opengl代码,但三星仍然将该应用程序视为游戏。奇怪的是,当我更改应用程序ID时,应用程序会正常启动。所以,我认为三星将application-id作为游戏存储在某个地

  • 代码是一个简单的二分搜索程序。我试着追踪程序,但它只会让我更加困惑。我不明白为什么嵌套的if has data,min,midpoint-1,&target和底部的else if语句has data,midpoint+1,max,target。

  • 我添加了依赖项 我已经有了Entitymanager,它工作得很好。 然后我添加了Hibernate搜索属性:

  • 我正在尝试编写一个程序来玩游戏猪,我两次得到这个错误,但我不知道为什么:“错误:无法找到符号扫描器sc=new Scanner(system.in); 非常感谢任何帮助。我对写代码很陌生,所以请你好心。:)另外,忽略奇怪的间距...在我的程序中它做得很正确,但我有困难让它正确地适合这里。

  • 我想使用极小极大搜索(带有alpha-beta修剪),或者更确切地说是内极大搜索,来使计算机程序玩纸牌游戏。 纸牌游戏实际上由4个玩家组成。所以为了能够使用极小极大等等。,我把游戏简化为“我”对抗“别人”。每次“走位”后,你都可以从游戏本身客观地读出当前状态的评价。当所有4个玩家都放好牌后,最高的玩家赢得所有人,并且牌的价值也算在内。 由于您不知道其他 3 名玩家之间的卡牌分布情况,我认为您必须使