比较2D数组和返回布尔值

时间:2015-02-10 04:57:56

标签: java

我正在制作拼图游戏,最后,我使用isGameOver()方法检查2D数组tiles中的每个拼贴是否在正确的位置(即,如果它匹配winningTiles,其中元素按照1-15))的顺序列出,然后根据它们是否匹配返回一个布尔值。所有用户必须真正做的是输入15以使tiles匹配winningTiles,但isGameOver()不会返回true并打印"您赢了!&#34 ;声明不管。知道我的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;
    }
} 

2 个答案:

答案 0 :(得分:1)

if(tiles == winningTiles)

检查tileswinningTiles是否引用相同的数组,而不是它们。要检查单个元素(例如ij),请使用

if(tiles[i][j] == winningTiles[i][j])

这不是代码的唯一问题,但这就是isGameOver始终返回false的原因。

答案 1 :(得分:0)

你为什么比较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;
           }
        }