从文本文件填充布尔二维数组

时间:2018-09-04 17:55:09

标签: java multidimensional-array text-files

尝试将文本文件中的数据输入到二维数组时遇到了一些麻烦。我尝试导入的数据主要是1和0的序列,但也包含几个字符(S和G)。本练习的目的是创建一个迷宫游戏,其中0和1表示是否阻塞了一个空间,S表示开始,G表示目标。我已经尝试了一些方法,但是似乎没有任何东西可以正确地导入我的文本文件来创建初始迷宫布局。我收到的主要错误是数组超出范围,或者是告诉我迷宫的格式不正确。

这是整个类的代码

package solution;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Scanner;

/**
 * A maze game.
 * 
 * @author Nicholas Thomas
 * @version 8/30/2018
 *
 */
public class MazeGame
{
    /**
     * The size of each side of the game map.
     */
    private final static int HEIGHT = 19;
    private final static int WIDTH = 39;

    /**
     * The game map, as a 2D array of ints.
     */
    private boolean[][] blocked = new boolean[HEIGHT][WIDTH];

    /**
     * The current location of the player vertically.
     */
    // TODO: add field here.
    int userCol;
    /**
     * The current location of the player horizontally.
     */
    // TODO: add field here.
    int userRow;
    /**
     * The scanner from which each move is read.
     */
    // TODO: add field here.
    Scanner moveScanner = new Scanner(System.in);
    /**
     * The row and column of the goal.
     */
    // TODO: add fields here.

    /**
     * The row and column of the start.
     * 
     * @return
     */
    // TODO: add fields here.

    /**
     * Accessor method for the Blocked array.
     * 
     * @param Blocked
     * 
     * @return Blocked
     * 
     */
    public boolean[][] getBlocked()
    {
        return blocked;
    }

    /**
     * Accessor method to return the user col.
     * 
     * @return userCol
     */
    public int getUserCol()
    {
        return userCol;
    }

    /**
     * Accessor to get userRow.
     * 
     * @return userRow
     */
    public int getUserRow()
    {
        return userRow;
    }

    /**
     * Accessor method to get scanner contents.
     * 
     */
    public Scanner getMoveScanner()
    {
        return moveScanner;
    }

    /**
     * Mutator method for the blocked array.
     * 
     */
    public void setBlocked(boolean[][] blocked)
    {
        this.blocked = blocked;
    }

    /**
     * Mutator method for the userCol.
     */
    public void setUserCol(int col)
    {
        this.userCol = col;
    }

    public void setUserRow(int row)
    {
        this.userRow = row;

    }

    public void setMoveScanner(Scanner moveScanner)
    {
        this.moveScanner = moveScanner;
    }

    /**
     * Constructor initializes the maze with the data in 'mazeFile'.
     * 
     * @param mazeFile
     *            the input file for the maze
     * @throws FileNotFoundException
     */
    public MazeGame(String mazeFile)
    {
        loadMaze(mazeFile);
        moveScanner = new Scanner(System.in);

    }

    /**
     * Constructor initializes the maze with the 'mazeFile' and the move scanner
     * with 'moveScanner'.
     * 
     * @param mazeFile
     *            the input file for the maze
     * @param moveScanner
     *            the scanner object from which to read user moves
     */
    public MazeGame(String mazeFile, Scanner moveScanner)
    {
        loadMaze(mazeFile);
        this.moveScanner = moveScanner;
    }

    /**
     * getMaze returns a copy of the current maze for testing purposes.
     * 
     * @return the grid
     */
    public boolean[][] getMaze()
    {
        if (blocked == null)
        {
            return null;
        }
        boolean[][] copy = new boolean[HEIGHT][WIDTH];
        for (int i = 0; i < HEIGHT; i++)
        {
            for (int j = 0; j < WIDTH; j++)
            {
                copy[i][j] = blocked[i][j];
            }
        }
        return copy;
    }

    /**
     * setMaze sets the current map for testing purposes.
     * 
     * @param maze
     *            another maze.
     */
    public void setMaze(boolean[][] maze)
    {
        this.blocked = maze;
    }

    /**
     * Function loads the data from the maze file and creates the 'blocked' 2D
     * array.
     * 
     * @param mazeFile
     *            the input maze file.
     */
    // TODO: private void loadMaze(String mazeFile)

    /**
     * Actually plays the game.
     */
    public void playGame()
    {

    }

    /**
     * Checks to see if the player has won the game.
     * 
     * @return true if the player has won.
     */
    // TODO: public boolean playerAtGoal()

    /**
     * Makes a move based on the String.
     * 
     * @param move
     *            the direction to make a move in.
     * @return whether the move was valid.
     */
    public boolean makeMove(String move)
    {
        // TODO
        return false;
    }

    /**
     * Prints the map of the maze.
     */
    public void printMaze()
    {
        // TODO
    }

    /**
     * Creates a new game, using a command line argument file name, if one is
     * provided.
     * 
     * @param args
     *            the command line arguments
     */

    public static void main(String[] args)
    {
        String mapFile = "data/easy.txt";
        Scanner scan = new Scanner(System.in);
        MazeGame game = new MazeGame(mapFile, scan);
        game.playGame();
    }

    /**
     * Method to crate the maze from file.
     * 
     * @param mazeFile
     *
     */
    public void loadMaze(String mazeFile)
    {
        Scanner scanner = null;
        try
        {
            String var;
            scanner = new Scanner(new File(mazeFile));
            boolean[][] blocked = new boolean[HEIGHT][WIDTH];
            for (int i = 0; i < HEIGHT; i++)
            {
                for (int j = 0; j < WIDTH; j++)
                {
                    if (scanner.hasNext())
                    {
                        var =scanner.next();

                        if (var == "1")
                        {
                            blocked[i][j] = true;
                        }
                        if (var == "0")
                        {
                            blocked[i][j] = false;
                        }
                        if (var == "S")
                        {
                            blocked[i][j] = false;
                        }
                        if (var == "G")
                        {
                            blocked[i][j] = false;
                        }
                        System.out.print(blocked[i][j] + ",");
                    }
                }
            }
            scanner.close();
        }
        catch (FileNotFoundException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}

这就是我要导入的文本文件的样子。

S 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 
0 1 1 1 0 1 1 1 0 1 1 1 0 1 0 1 1 1 0 1 0 1 1 1 1 1 1 1 0 1 0 1 1 1 1 1 0 1 0 
0 0 0 1 0 0 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 0 0 1 0 0 0 1 0 1 0 1 0 0 0 0 0 1 0 
1 1 0 1 1 1 1 1 0 1 0 1 0 1 0 1 0 1 0 1 1 1 0 1 0 1 0 1 0 1 0 1 0 1 1 1 1 1 0 
0 1 0 0 0 1 0 0 0 1 0 1 0 0 0 1 0 1 0 0 0 0 0 1 0 1 0 0 0 1 0 1 0 1 0 0 0 1 0 
0 1 1 1 0 1 0 1 1 1 0 1 1 1 1 1 0 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 0 1 0 1 1 1 0 
0 0 0 1 0 1 0 0 0 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,但是这样做并不幸运。

 /**
     * Method to crate the maze from file.
     * 
     * @param mazeFile
     */
    public void loadMaze(String mazeFile)
    {
        String fileName = ("C:/users/thomasns/easy.txt");
        // File file = new File(fileName);
        Scanner scanMan = new Scanner(fileName);
        boolean blocked[][] = new boolean[HEIGHT][WIDTH];
        while (scanMan.hasNext())
        {
            for (int i = 0; i < blocked.length; i++)
            {
                for (int j = 0; j < blocked[i].length; j++)
                {
                    blocked[HEIGHT][WIDTH] = scanMan.nextBoolean();
                }
                scanMan.close();
            }

        }
    }

这是我一直在尝试的另一种实现,到目前为止,我只能使它对所有内容返回false。

  /**
     * Method to crate the maze from file.
     * 
     * @param mazeFile
     *
     */
    public void loadMaze(String mazeFile)
    {
        Scanner scanner = null;
        try
        {
            String var;
            scanner = new Scanner(new File(mazeFile));
            boolean[][] blocked = new boolean[HEIGHT][WIDTH];
            for (int i = 0; i < HEIGHT; i++)
            {
                for (int j = 0; j < WIDTH; j++)
                {
                    if (scanner.hasNext())
                    {
                        var =scanner.next();

                        if (var == "1")
                        {
                            blocked[i][j] = true;
                        }
                        if (var == "0")
                        {
                            blocked[i][j] = false;
                        }
                        if (var == "S")
                        {
                            blocked[i][j] = false;
                        }
                        if (var == "G")
                        {
                            blocked[i][j] = false;
                        }
                        System.out.print(blocked[i][j] + ",");
                    }
                }
            }
            scanner.close();
        }
        catch (FileNotFoundException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

如果有人对我有任何建议,我将不胜感激,在此先感谢您。

1 个答案:

答案 0 :(得分:0)

您的第一个方法可以工作,但是索引错误,因为它们是常量

 blocked[HEIGHT][WIDTH] = scanMan.nextBoolean();

您应该宁愿

 blocked[j][i] = scanMan.nextBoolean(); // or other way around.

第二个可能有效,但是这不是您比较字符串的方式,因此请

 if (var == "1")

应该是

if (var.equals("1"))

对我来说,要么逐字符读取char并将其放入数组中,要么读取整行,拆分,创建数组行,然后重复。

第一种方法可能是这样的:

  boolean blocked[][] = new boolean[HEIGHT][WIDTH];
    int c=0;
    while (scanMan.hasNext()) {
       blocked[c/width][c%width]=scanMan.nextBoolean();
       c++;
    }

我想您必须处理其他字符,然后是1和0,但是您应该有一个大概的主意。

工作示例:

 public static void main(String... args) {
        String input =
                "S 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 \n" +
                "0 1 1 1 0 1 1 1 0 1 1 1 0 1 0 1 1 1 0 1 0 1 1 1 1 1 1 1 0 1 0 1 1 1 1 1 0 1 0 \n" +
                "0 0 0 1 0 0 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 0 0 1 0 0 0 1 0 1 0 1 0 0 0 0 0 1 0 \n" +
                "1 1 0 1 1 1 1 1 0 1 0 1 0 1 0 1 0 1 0 1 1 1 0 1 0 1 0 1 0 1 0 1 0 1 1 1 1 1 0 \n" +
                "0 1 0 0 0 1 0 0 0 1 0 1 0 0 0 1 0 1 0 0 0 0 0 1 0 1 0 0 0 1 0 1 0 1 0 0 0 1 0 \n" +
                "0 1 1 1 0 1 0 1 1 1 0 1 1 1 1 1 0 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 0 1 0 1 1 1 0 \n" +
                "0 0 0 1 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 \n" +
                "1 1 0 1 0 1 0 1 0 1 0 1 0 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 0 1 0 1 1 1 0 1 1 1 1 \n" +
                "0 0 0 1 0 1 0 1 0 1 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 1 0 1 0 1 0 0 0 0 0 0 0 \n" +
                "0 1 1 1 0 1 1 1 0 1 1 1 0 1 1 1 0 1 1 1 1 1 0 1 1 1 0 1 0 1 0 1 1 1 1 1 1 1 0 \n" +
                "0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 1 0 0 0 1 0 0 0 1 G \n" +
                "0 1 0 1 1 1 0 1 1 1 0 1 1 1 0 1 1 1 1 1 0 1 1 1 0 1 1 1 0 1 0 1 0 1 0 1 0 1 1 \n" +
                "0 1 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 1 0 1 0 1 0 0 0 1 0 1 0 0 0 1 0 0 0 \n" +
                "0 1 0 1 0 1 1 1 0 1 1 1 0 1 1 1 1 1 0 1 0 1 0 1 0 1 0 1 1 1 1 1 1 1 1 1 1 1 0 \n" +
                "0 1 0 0 0 1 0 0 0 1 0 1 0 0 0 0 0 0 0 1 0 1 0 0 0 1 0 1 0 0 0 1 0 0 0 0 0 1 0 \n" +
                "0 1 1 1 1 1 0 1 1 1 0 1 1 1 1 1 1 1 0 1 0 1 1 1 0 1 0 1 0 1 0 1 0 1 1 1 0 1 0 \n" +
                "0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 1 0 0 0 1 0 0 0 1 0 1 0 0 0 1 0 1 0 0 0 1 0 0 0 \n" +
                "1 1 1 1 0 1 1 1 0 1 0 1 1 1 0 1 0 1 1 1 1 1 0 1 1 1 1 1 1 1 0 1 1 1 0 1 1 1 0 \n" +
                "0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 ";

        final int width = 39;
        final int heigh = 19;

        Scanner scan = new Scanner(input);
        boolean[][] blocked = new boolean[heigh][width];

        int c = 0;
        while (scan.hasNext()) {
            String character = scan.next();
            switch (character) {
                case "S"://do something; break;
                    System.out.println("Detected S");
                    break;
                case "G"://so something;
                    System.out.println("Detected G");
                    break;
                default:
                    blocked[c / width][c % width] = Boolean.valueOf(character);
            }
            c++;
        }

}
相关问题