在我的蛇游戏中打印我的主板时遇到困难

时间:2017-12-08 19:28:58

标签: java

我不是java的专家,在我的Snake游戏中遇到了一个问题。我创建了一个名为GameManager的类:

public class GameManager {

  private GameObject board[][];
  private int xR;
  private int yR;
  public Snake snk;
  private Food food;

  public GameManager (String fileName) {
      BufferedReader fileInput = null;

      try {
          fileInput = new BufferedReader(new FileReader(fileName));
          Scanner fileScanner = new Scanner(fileInput);
          int rows = fileScanner.nextInt();
          int cols = fileScanner.nextInt();

          board = new GameObject[rows][cols];
          for (int row = 0; row < rows; row++) {
              for (int col = 0; col < cols; col++) {
                  board[row][col] = new Empty();
              }
          } 

          while(fileScanner.hasNext()) {
              fileScanner.nextLine();
              int xStart = fileScanner.nextInt();
              int yStart = fileScanner.nextInt();
              int xEnd = fileScanner.nextInt();
              int yEnd = fileScanner.nextInt();

            addWall(xStart, yStart, xEnd, yEnd);
        }

        addGameObject(snk);
        addGameObject(food);

        fileScanner.close();

    } catch(IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if(fileInput != null) {fileInput.close();}
        } catch(IOException e) {
            e.printStackTrace();
        }
    }
}

public void newRandomXY() {
    Random r = new Random(0);
    this.xR = r.nextInt(board.length);
    this.yR = r.nextInt(board.length);
}

public void addGameObject(GameObject s) {
    newRandomXY();
    while(board[xR][yR].isOccupied()) {
        newRandomXY();
    }

    if(s instanceof Snake) {
        s = new Snake(xR, yR);
        board[xR][yR] = s;
    } else if(s instanceof Food) {
        s = new Food(xR, yR);
        board[xR][yR] = s;
    }
}

public void addWall(int xStart, int yStart, int xEnd, int yEnd) {        
    for(int x = xStart; x <= xEnd; x++) {
        for(int y = yStart; y <= yEnd; y++) {
            board[x][y] = new Wall();
        }
    }
}


@Override
public String toString() {

    String ret = "";
    for (int row = 0; row < board.length; row++) {
        for (int col = 0; col < board[row].length; col++) {
            ret += board[row][col].toString();
        }
        ret += "\n";
    }
    return ret;
}

}

现在我遇到的问题是,每当我尝试在我的cmd上打印此板的字符串版本时,程序就会挂起,我必须努力关闭cmd。我一直在搞乱一些代码而且我已经能够修复程序崩溃了,但是我还没弄清楚为什么它都没有打印出来。

这是我的Snake类(注意:我此类中还有其他一些方法,我目前还没有使用,所以我不认为它们是问题):

public class Snake extends GameObject {
    private Point head;
    private Deque<Point> snakeBody;
    private int lenght = 0;
    private String direction = "UP";

    public Snake(int x, int y) {
        this.head = super.newCell(x, y);
        this.snakeBody = new ArrayDeque<Point>();
        this.snakeBody.push(head);
    }

和我的toString of Snake:

 public String toString(Deque<Point> s) {
        String str = "";
        for(Point p : s) {
            String snk = p.toString();
            snk = "S";
            str += snk;
        }
        return str;
    }

这是我的食品类:

public class Food extends GameObject {
    private Point foodLoc;

    public Food(int x, int y) {
        this.foodLoc = new Point(x, y);
    }

    public Point getLocation() {
        return foodLoc.getLocation();
    }

    public String toString() {
        return "F";
    }
}

这是我的GameObject类:

import java.awt.Point;

public class GameObject {

    public final int CELL_SIZE = 1;

    public Point newCell(int x, int y) {
        return new Point(x, y);
    }

    public boolean isOccupied() {
        return true;
    }

    public boolean isOccupied(Point p, Point o) {
        boolean flag = false;
        if(p.getLocation().equals(o.getLocation())) {
            flag = true;
        } else {
            flag = false;
        }

        return flag;
    }

}

我很清楚我在Snake中的toString方法是完全错误的,但我不一定了解如何修复它以及为什么我的其他toString方法不起作用。我浏览了论坛,看看能否找到并回答,但我找不到任何相关内容。

1 个答案:

答案 0 :(得分:0)

问题是你正在尝试打印gameObject类型的数组,但是该对象没有.toString运算符,所以它总是返回null。

我猜你想要将单元格表示为空或被占用,或者甚至可能通过食物进一步表示,但是你需要在该类中使用.toString来定义你想要在任何给定场景中返回的内容。