2015-02-10 94 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; 
      } 
     }