Reputation: 1
I'm making a tile game, and at the end, I'm using the method isGameOver()
to check that each tile in the 2D array tiles
is in the right location (i.e. if it matches up winningTiles
, where the elements are listed in order from 1-15)), and then return a boolean based on whether or not they match. All the user has to really do is enter 15 to make tiles
match winningTiles
, but isGameOver()
won't return true and print the "you won!" statement regardless. Any idea what's wrong with my 2D array comparison?
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;
}
}
Upvotes: 0
Views: 80
Reputation: 1761
Why did you compare if (tiles == winningTiles)
???
Its compare the reference or address of the object. You have to check each element by element of array.
You may use index number (e.g. using i and j) like if (tiles[i][j] == winningTiles[j][j])
or
You can use Arrays.deepEquals(arr1, arr2)
method. So that you don't have to think about looping.
instead of below code just use 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;
}
}
Upvotes: 0
Reputation: 58888
if(tiles == winningTiles)
checks whether tiles
and winningTiles
refer to the same array, which they don't. To check a single element (say the one at i
,j
), use
if(tiles[i][j] == winningTiles[i][j])
This is not the only problem with your code, but this is the reason that isGameOver
always returns false.
Upvotes: 1