2015-02-10 118 views
0

我在做一个平铺游戏,最后我使用方法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; 
    } 
} 
+0

您的比较也许应该是'砖[i] [j] == winningTiles [i] [j]'因为这是它的检查,如果他们是相同的数组,如果它们具有相同的元素则不是。在将来。您并不总是需要包含*所有代码,只是足以证明问题。 – genisage 2015-02-10 05:37:10

回答

1
if(tiles == winningTiles) 

检查是否tileswinningTiles指代相同的数组,他们没有。要检查单个元素(说一个在ij),使用

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

这是不是与你的代码是唯一的问题,但是这是isGameOver总是返回false的原因。

+0

我试过使用这种方法,并使用deepEquals,这只是使isGameOver()返回true,无论游戏是否赢得 – drjacobs 2015-02-10 05:12:34

+0

@drjacobs这是因为在同一代码中的另一个问题。你可以通过用笔和纸自己运行程序来弄清楚问题所在。 – immibis 2015-02-10 05:31:35

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