比较二维数组和返回的布尔值
问题描述:
我在做一个平铺游戏,最后我使用方法isGameOver()
来检查二维数组tiles
中的每个拼图是否在正确的位置(即如果它匹配了winningTiles
,其中元素从1-15开始依次列出)),然后根据它们是否匹配返回布尔值。所有用户必须要做的是输入15使tiles
匹配winningTiles
,但isGameOver()
不会返回true并打印出“您赢了!”。声明不管。任何想法我的2D阵列比较有什么问题?比较二维数组和返回的布尔值
import java.awt.geom.Point2D;
import java.util.Arrays;
import java.util.Scanner;
public class TileGame
{
public TileGame()
{
GameBoard gameBoard = new GameBoard();
Scanner user_input = new Scanner(System.in);
int tileNumber = 0; //User-entered tile number
while (tileNumber != -1)
{
System.out.print("\nEnter tile number: ");
tileNumber = user_input.nextInt();
if (tileNumber < 0) //So user can enter -1 to quit the game
System.exit(0);
Point2D point = gameBoard.getTilePosition(tileNumber);
if (gameBoard.isEmptySpotANeighborOfTile(tileNumber, point))
gameBoard.moveTile(tileNumber, point);
if (gameBoard.isGameOver())
{
System.out.print("\nYou won!");
System.exit(0);
}
}
}
public static void main(String[] args)
{
new TileGame();
}
}
class GameBoard
{
int maxRows = 6;
int [][] tiles = { {-1, -1, -1, -1, -1, -1},
{-1, 1, 2, 3, 4, -1},
{-1, 5, 6, 7, 8, -1},
{-1, 9, 10, 11, 12, -1},
{-1, 13, 14, 0, 15, -1},
{-1, -1, -1, -1, -1, -1} };
public GameBoard()
{
printGameBoard();
}
public void printGameBoard() //Prints current location of tiles to terminal window
{
for(int i = 1; i < (maxRows - 1); i++)
{
for(int j = 1; j < (maxRows - 1); j++)
{
System.out.printf("%5s ", tiles[i][j]);
}
System.out.println();
}
}
public Point2D getTilePosition(int tileNumber) //Tells us where tile is currently located on the board
{
for(int i = 1; i < (maxRows -1); i++)
{
for(int j = 1; j < (maxRows - 1); j++)
{
if(tileNumber == tiles[i][j])
{
System.out.print("Tile number: " + tileNumber + " Row: " + i + " Column: " + j);
Point2D.Double searchedTile = new Point2D.Double(i,j); //Stores tile's location in Point2D object
return searchedTile;
}
}
}
return null;
}
public boolean isEmptySpotANeighborOfTile(int tileNumber, Point2D point) //Checks if empty spot(0) is neighboring the tile
{
int i = (int)point.getX();
int j = (int)point.getY();
if(tiles[i -1][j] == 0)
{
System.out.print("\nEmpty spot as neighbor?: true");
return true;
}
else if(tiles[i + 1][j] == 0)
{
System.out.print("\nEmpty spot as neighbor?: true");
return true;
}
else if(tiles[i][j - 1] == 0)
{
System.out.print("\nEmpty spot as neighbor?: true");
return true;
}
else if(tiles[i][j + 1] == 0)
{
System.out.print("\nEmpty spot as neighbor?: true");
return true;
}
else
{
System.out.print("\nEmpty spot as neighbor?: false");
return false;
}
}
public void moveTile(int tileNumber, Point2D point) //Switches empty spot and tile locations on the board
{
int i = (int)point.getX();
int j = (int)point.getY();
if(tiles[i -1][j] == 0)
tiles[i][j] = 0;
else if(tiles[i + 1][j] == 0)
tiles[i][j] = 0;
else if(tiles[i][j - 1] == 0)
tiles[i][j] = 0;
else if(tiles[i][j + 1] == 0)
tiles[i][j] = 0;
}
public boolean isGameOver() //Checks if each tile's in the right location & prints if the user has won
{
int[][] winningTiles = { {-1, -1, -1, -1, -1, -1},
{-1, 1, 2, 3, 4, -1},
{-1, 5, 6, 7, 8, -1},
{-1, 9, 10, 11, 12, -1},
{-1, 13, 14, 15, 0, -1},
{-1, -1, -1, -1, -1, -1} };
for(int i = 1; i < tiles.length; i++)
{
for(int j = 1; j < tiles.length; j++)
{
if (tiles == winningTiles)
return true;
}
}
return false;
}
}
答
if(tiles == winningTiles)
检查是否tiles
和winningTiles
指代相同的数组,他们没有。要检查单个元素(说一个在i
,j
),使用
if(tiles[i][j] == winningTiles[i][j])
这是不是与你的代码是唯一的问题,但是这是isGameOver
总是返回false的原因。
答
你为什么比较if (tiles == winningTiles)
??? 它比较对象的引用或地址。您必须通过数组的元素检查每个元素。
您可以使用索引号(例如,使用i和j)像if (tiles[i][j] == winningTiles[j][j])
或
可以使用Arrays.deepEquals(arr1, arr2)
方法。这样你就不必考虑循环。
,而不是下面的代码只是使用Arrays.deepEquals(tiles, winningTiles)
for(int i = 1; i < tiles.length; i++)
{
for(int j = 1; j < tiles.length; j++)
{
if (tiles == winningTiles)
return true;
}
}
您的比较也许应该是'砖[i] [j] == winningTiles [i] [j]'因为这是它的检查,如果他们是相同的数组,如果它们具有相同的元素则不是。在将来。您并不总是需要包含*所有代码,只是足以证明问题。 – genisage 2015-02-10 05:37:10