java - 基于二维布尔数组的打印
问题描述
我在尝试从二维布尔数组中打印数据时遇到了一些麻烦。这个练习的重点是创建一个迷宫游戏,其中布尔值 true 和 false 分别指示空间是否被阻塞,S 表示开始,G 表示目标。我不明白为什么,但是当迷宫打印时,它似乎认为数组的每个值都是真的,即使我之前已经设置了从文本文件中读取的数组的值。
这是整个班级的代码:
package solution;
import java.util.Scanner;
import java.io.File;
/**
* A maze game.
*
* @author Dominick Pulsone
* @version August 29th 2019
*
*/
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;
/**
* The current location of the player vertically.
*/
// TODO: add field here.
private int userRow;
/**
* The current location of the player horizontally.
*/
// TODO: add field here.
private int userCol;
/**
* The scanner from which each move is read.
*/
// TODO: add field here.
private Scanner moveScanner;
/**
* The row and column of the goal.
*/
// TODO: add fields here.
private int goalRow;
private int goalCol;
/**
* The row and column of the start.
*/
// TODO: add fields here.
private int startRow;
private int startCol;
/**
* Constructor initializes the maze with the data in 'mazeFile'.
* @param mazeFile the input file for the maze
*/
public MazeGame(String mazeFile)
{
// TODO
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)
{
// TODO
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;
}
/**
* getUserRow returns the value in the userRow field.
*
* @return the row the user is in
*/
public int getUserRow()
{
return userRow;
}
/**
* setUserRow sets the row the user is currently in.
*
* @param userRow the row value
*/
public void setUserRow(int userRow)
{
this.userRow = userRow;
}
/**
* getUserCol returns the value in the userCol field.
*
* @return the column the user is in
*/
public int getUserCol()
{
return userCol;
}
/**
* setUserCol sets the column the user is currently in.
*
* @param userCol the column value
*/
public void setUserCol(int userCol)
{
this.userCol = userCol;
}
/**
* getStartCol returns the value in the startCol field.
*
* @return the column the start is at
*/
public int getStartCol()
{
return startCol;
}
/**
* setStartCol sets the column the start is in.
*
* @param startCol the column value
*/
public void setStartCol(int startCol)
{
this.startCol = startCol;
}
/**
* getStartRow returns the value in the startRow field.
*
* @return the row the start is in
*/
public int getStartRow()
{
return startRow;
}
/**
* setStartRow sets the row the start is in.
*
* @param startRow the row value
*/
public void setStartRow(int startRow)
{
this.startRow = startRow;
}
/**
* getGoalCol returns the value in the goalCol field.
*
* @return the column the goal is at
*/
public int getGoalCol()
{
return goalCol;
}
/**
* setgoalCol sets the column the goal is in.
*
* @param goalCol the column value
*/
public void setGoalCol(int goalCol)
{
this.goalCol = goalCol;
}
/**
* getGoalRow returns the value in the goalRow field.
*
* @return the row the goal is in
*/
public int getGoalRow()
{
return goalRow;
}
/**
* setGoalRow sets the row the goal is in.
*
* @param goalRow the row value
*/
public void setGoalRow(int goalRow)
{
this.goalRow = goalRow;
}
/**
* getMoveScanner returns the scanner moveScanner.
*
* @return the scanner moveScanner
*/
public Scanner getMoveScanner()
{
return moveScanner;
}
/**
* setMoveScanner sets the Scanner moveScanner.
*
* @param moveScanner the Scanner
*/
public void setMoveScanner(Scanner moveScanner)
{
this.moveScanner = moveScanner;
}
/**
* 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)
private void loadMaze(String mazeFile)
{
blocked = new boolean[HEIGHT][WIDTH];
try
{
File file = new File(mazeFile);
Scanner fileScanner = new Scanner(file);
int c = 0;
while (fileScanner.hasNext())
{
String character = fileScanner.next();
switch (character)
{
case "S":
startRow = c / WIDTH;
startCol = c % WIDTH;
blocked[c / WIDTH][c % WIDTH] = false;
break;
case "G":
goalRow = c / WIDTH;
goalCol = c % WIDTH;
blocked[c / WIDTH][c % WIDTH] = false;
break;
default:
blocked[c / WIDTH][c % WIDTH] =
Boolean.valueOf(character);
}
c++;
}
fileScanner.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
/**
* 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()
public boolean playerAtGoal()
{
return false;
}
/**
* 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
System.out.println("*---------------------------------------*");
for (int i = 0; i < blocked.length; i++)
{
System.out.print("|");
for (int j = 0; j < blocked[i].length; j++)
{
if (i == startRow && j == startCol)
{
System.out.print("S");
}
else if (i == goalRow && j == goalCol)
{
System.out.print("G");
}
else if (blocked[i][j] = true)
{
System.out.print("X");
}
else if (blocked[i][j] = false)
{
System.out.print(" ");
}
}
System.out.println("|");
}
System.out.println("*---------------------------------------*");
}
/**
* 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();
}
}
迷宫的第一行应该是这样的:
|SX X X X |
但相反,它打印:
|SXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX|
我不明白为什么它认为数组中的每个值都是真的,非常感谢帮助。
解决方案
因为你不是在比较,你是在分配。使用双等号。我是说:
代替
else if (blocked[i][j] = true)
和
else if (blocked[i][j] = false)
尝试
else if (blocked[i][j] == true)
和
else if (blocked[i][j] == false)
或者更好:
else if (blocked[i][j])
和
else if (!blocked[i][j])
推荐阅读
- ruby - 如何在 Mac 上为 Ruby 添加根 CA 证书
- linux-kernel - 具有唯一请求的线程 x 在被阻塞后如何标记为就绪?
- facebook - 在迁移到 https 时保留喜欢 - og:url 被重写
- php - PHP 7.0 下的 PDO FETCH_ASSOC
- python - GTK 对话框边距不起作用
- javascript - Firebase 更新用户个人资料显示名称不工作 Javascript
- c# - WPF 访问 CustomMarkup 中的 DataContext 以进行 DataTrigger 绑定
- javascript - on('request') 对每个请求调用两次,为什么?
- c++ - C++ - 获取 GitHub 项目的最新版本
- python - 从 pyside2 获取根对象