简体   繁体   English

Java IO,从文件读取并打印到2D字符数组

[英]Java IO, reading from a file and printing to a 2d character array

So I'm working on an assignment for my game design class to build a pacman clone, it'll be throughout the semester. 因此,我正在为我的游戏设计课分配作业,以构建一个pacman克隆,这将贯穿整个学期。

Currently I've got a text file that is the pacman maze 目前,我有一个文本文件,是吃豆子迷宫

see below : 见下文 :

WWWWWWWWWWWWWWWWWWWWWWWWWWWW
W............WW............W
W.WWWW.WWWWW.WW.WWWWW.WWWW.W
W*WWWW.WWWWW.WW.WWWWW.WWWW*W
W.WWWW.WWWWW.WW.WWWWW.WWWW.W
W..........................W
W.WWWW.WW.WWWWWWWW.WW.WWWW.W
W.WWWW.WW.WWWWWWWW.WW.WWWW.W
W......WW....WW....WW......W
WWWWWW.WWWWW.WW.WWWWW.WWWWWW
WWWWWW.WWWWW.WW.WWWWW.WWWWWW
WWWWWW.WW..........WW.WWWWWW
WWWWWW.WW.WWWWWWWW.WW.WWWWWW
WWWWWW.WW.WWWWWWWW.WW.WWWWWW
..........WWWWWWWW..........
WWWWWW.WW.WWWWWWWW.WW.WWWWWW
WWWWWW.WW.WWWWWWWW.WW.WWWWWW
WWWWWW.WW..........WW.WWWWWW
WWWWWW.WW.WWWWWWWW.WW.WWWWWW
WWWWWW.WW.WWWWWWWW.WW.WWWWWW
W............WW............W
W.WWWW.WWWWW.WW.WWWWW.WWWW.W
W*WWWW.WWWWW.WW.WWWWW.WWWW*W
W...WW................WW...W
WWW.WW.WW.WWWWWWWW.WW.WW.WWW
WWW.WW.WW.WWWWWWWW.WW.WW.WWW
W......WW....WW....WW......W
W.WWWWWWWWWW.WW.WWWWWWWWWW.W
W.WWWWWWWWWW.WW.WWWWWWWWWW.W
W..........................W
WWWWWWWWWWWWWWWWWWWWWWWWWWWW 

the idea is that this is read in, line by line by a reader from the java io package and then used to populate a 2d array, I think then I can use a loop to specify where to print images using the paint class with the data in the array. 想法是,这是由读者从java io包逐行读取的,然后用于填充2d数组,我想然后我可以使用循环指定使用paint类和数据来打印图像的位置在数组中。

My problem currently is the paint method, it doesnt seem to be working at all but I cant find what's wrong with it at the moment. 我目前的问题是绘画方法,它似乎根本不起作用,但是我现在无法找到问题所在。 Can anyone point me in the right direction? 谁能指出我正确的方向?

(My codes formatting has been messed up a tad by indentation required here, I'm also new to the java IO package, first time I've seen exception handling!) (我的代码格式已被此处所需的缩进弄乱了,我还是java IO包的新手,这是我第一次看到异常处理!)

Thanks in advance for any help! 在此先感谢您的帮助!

//imports
import java.awt.*;
import java.io.*;
import javax.swing.*;


public class Maze extends JFrame
{

//V.Decleration
private static final Dimension WindowSize = new Dimension (600,600);
static char[][] Amaze = new char[28][31];


//default constructor
public Maze()
{
this.setTitle("Pacman");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Dimension screensize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
int x = screensize.width/2 - WindowSize.width/2;
int y = screensize.height/2 - WindowSize.height/2;
setBounds(x, y, WindowSize.width, WindowSize.height);
setVisible(true);
}

public void paint (Graphics g)
{

String line = null;

try
  { 
     BufferedReader reader = new BufferedReader(new FileReader("G:\\Game       Design\\Pacman\\src\\Maze.txt"));


  for (int i=0; i<=31; i++)
 {   
do
 {
    try
    {

    line=reader.readLine();
     for (int y=0; y<=28; y++)
     {

      Amaze[y][i]=line.charAt(y); 
     }

   }catch (IOException e) { }
 }

while (line!= null);
    try
      {
       reader.close();
      } catch (IOException e) { }
    }
 } catch (FileNotFoundException e) {}

 }

//main
public static void main (String [] args)
{
 Maze maze = new Maze();

 for (int i=0;i<=28;i++)
 System.out.print(Amaze[i][31]);
}

}

You are creating an array that is too small for the loop. 您正在创建的数组对于循环而言太小。 Namely: new char[28][31]; 即: new char[28][31]; will only allow for a maximum index of 27 and 30. Your for loops are: 将只允许最大索引27和30。for循环是:

for (int i=0; i<=31; i++)
for (int y=0; y<=28; y++)

Use i<31 and y<28 , or increase your array to be [29][32] . 使用i<31y<28 ,或将数组增加为[29][32] Either one of these should solve your current problem. 这些中的任何一个都可以解决您当前的问题。

Three suggestions: 三点建议:

  1. It may be less misleading to separate your functions to do just one job at a time. 将功能分开仅一次完成一项工作可能会减少误导。 So there should be a function to read the file and then another to paint the results. 因此,应该有一个读取文件的功能,然后是另一个绘制结果的功能。
  2. Put more System.out.println() statements in your code when you're trying to debug. 尝试调试时,在代码中放置更多System.out.println()语句。 That's a good way to check whether each part did what you intended. 这是检查每个零件是否符合您的期望的好方法。 For example, printing the line variable after reading it in would at least let you know whether you were reading the file correctly. 例如,在读入line变量后打印它至少会让您知道您是否正确地读取了文件。
  3. Always print out your exceptions. 始终打印出您的例外情况。 They will tell you what went wrong and where. 他们会告诉您哪里出了问题以及在哪里。

That said, this code will load and print your maze: 也就是说,此代码将加载并打印您的迷宫:

import java.io.*;

public class Read2DArray {
    private final int WIDTH = 28;
    private final int HEIGHT = 31;

    private char[][] maze = new char[WIDTH][HEIGHT];

    public static void main(String[] args) {
        Read2DArray array = new Read2DArray();
        array.loadFile("maze.txt");
        array.printArray();
    }

    public void loadFile(String fname) {
        try {
            BufferedReader reader = new BufferedReader(new FileReader(fname));

            String line;
            int col = 0, row = 0;
            while((line = reader.readLine()) != null && row < HEIGHT) {
                for(col = 0; col < line.length() && col < WIDTH; col++) {
                    maze[col][row] = line.charAt(col);
                }
                row++;
            }
            reader.close();
        } catch(IOException e) {
            e.printStackTrace();
        }
    }

    public void printArray() {
        for(int row = 0; row < HEIGHT; row++) {
            for(int col = 0; col < WIDTH; col++) {
                System.out.print(maze[col][row]);
            }
            System.out.println();
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM