我在做一個平鋪遊戲,最後我使用方法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;
}
}
您的比較也許應該是'磚[i] [j] == winningTiles [i] [j]'因爲這是它的檢查,如果他們是相同的數組,如果它們具有相同的元素則不是。在將來。您並不總是需要包含*所有代碼,只是足以證明問題。 – genisage 2015-02-10 05:37:10