I'm having a hard time implementing the method below for Project 0: 2048 Game:
public static boolean maxTileExists(Board b)
This method should return true if any of the tiles in the board are equal to the winning tile value 2048. Note that rather than hard coding the constant 2048 into your code, you should use MAX_PIECE, which is a constant that is part of the Model class. In other words, you shouldn’t do if (x == 2048) but rather if (x == MAX_PIECE). Leaving in hard coded numbers like 2048 is a bad programming practice sometimes referred to as a “magic number”. The danger of such magic numbers is that if you change them in one part of your code but not another, you might get unexpected results. By using a variable like MAX_PIECE you can ensure they all get changed together. After you’ve written the method, the tests in TestMaxTileExists.java should pass.
Here is my code below:
/**
* Returns true if any tile is equal to the maximum valid value.
* Maximum valid value is given by MAX_PIECE. Note that
* given a Tile object t, we get its value with t.value().
*/
public static boolean maxTileExists(Board b) {
// TODO: Fill in this function.
for (int i = 0; i < b.size(); i++) {
for (int j = 0; j < b.size(); j++) {
if (b.tile(i, j).value() == MAX_PIECE) {
return true;
}
}
}
return false;
}
I keep getting this error when testing:
java.lang.NullPointerException: Cannot invoke "game2048.Tile.value()" because the return value of "game2048.Board.tile(int, int)" is null
The first method that I needed to implement is below which worked perfectly and passed all test cases:
public static boolean emptySpaceExists(Board b)
This method should return true if any of the tiles in the given board are null. You should NOT modify the Board.java file in any way for this project. For this method, you’ll want to use the tile(int col, int row) and size() methods of the Board class. No other methods are necessary. Note: We’ve designed the Board class using a special keyword private that disallows you from using the instance variables of Board directly. For example, if you try to access b.values[0][0], this will not work. This is a good thing! It forces you to learn to use the tile method, which you’ll use throughout the rest of the project. Try opening the TestEmptySpace.java folder. Run the tests. You should see that 6 of the tests fail and 2 of them pass. After you’ve correctly written the emptySpaceExists method, all 8 tests in TestEmptySpace should pass.
Here is that code below:
/**
* Returns true if at least one space on the Board is empty.
* Empty spaces are stored as null.
*/
public static boolean emptySpaceExists(Board b) {
// TODO: Fill in this function.
for (int i = 0; i < b.size(); i++) {
for (int j = 0; j < b.size(); j++) {
if (b.tile(i, j) == null) {
return true;
}
}
}
return false;
}