简体   繁体   中英

Having difficulties printing out my board in my Snake Game

I am not an expert on java and have run into an issue on my Snake Game. I have created a class called 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;
}

}

Now the issue I'm having is that whenever I try to print a string version of this board on my cmd, the program just hangs and I have to hard close the cmd. I have been messing around with some of the code and I have been able to fix the program just crashing, but I haven't been able to figure why its all not printing out.

Here is my Snake Class ( Note : I also have some other methods in this class that I am not using at the moment, so I don't think they are the issue):

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);
    }

and my 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;
    }

Here's my Food Class:

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";
    }
}

and here is my GameObject Class:

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;
    }

}

I have a good feeling that my toString method in Snake is completely wrong, but I don't necessarily understand how to fix it and why my other toString methods don't work. I have looked around the forums to see if I could find and answer, but I couldn't find anything for this.

The problem is that you're trying to print an array of type gameObject, but that object does not have a .toString operator, so it's always going to return null.

I'm guessing that you want to represent the cells as either empty or occupied, or perhaps even further by having food, but you'd need a .toString in that class to define what you want returned in whatever given scenario.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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