The value cannot be resolved or is not a field
The method public boolean mergesWith(Tile moving)
returns true
if tags this
and moving
have the same meaning. But when I check if they are the same do the following:
if(this.value == temp.value){
return true;
}
then it shows me an error on temp.value
, saying that the value cannot be resolved or is not a field .
How can I fix this?
TwoNTile class :
package Game2048;
// Concrete implementation of a Tile. TwoNTiles merge with each other
// but only if they have the same value.
public class TwoNTile extends Tile {
private int value;
// Create a tile with the given value of n; should be a power of 2
// though no error checking is done
public TwoNTile(int n){
value = n;
}
// Returns true if this tile merges with the given tile. "this"
// (calling tile) is assumed to be the stationary tile while moving
// is presumed to be the moving tile. TwoNTiles only merge with
// other TwoNTiles with the same internal value.
public boolean mergesWith(Tile moving){
Tile temp = moving;
if(this.value == temp.value){
return true;
}
else{
return false;
}
}
// Produce a new tile which is the result of merging this tile with
// the other. For TwoNTiles, the new Tile will be another TwoNTile
// and will have the sum of the two merged tiles for its value.
// Throw a runtime exception with a useful error message if this
// tile and other cannot be merged.
public Tile merge(Tile moving){
return null;
}
// Get the score for this tile. The score for TwoNTiles are its face
// value.
public int getScore(){
return -1;
}
// Return a string representation of the tile
public String toString(){
return "";
}
}
Class Tile :
package Game2048;
// Abstract notion of a game tile.
public abstract class Tile{
// Returns true if this tile merges with the given tile.
public abstract boolean mergesWith(Tile other);
// Produce a new tile which is the result of merging this tile with
// the other. May throw an exception if merging is illegal
public abstract Tile merge(Tile other);
// Get the score for this tile.
public abstract int getScore();
// Return a string representation of the tile
public abstract String toString();
}
First . You can do it:
public boolean mergesWith(Tile moving){
return this.value == temp.value;
}
to get a more elegant solution.
Second , you need to add a variable value
to the class Tile
.
public abstract class Tile{
// ...
// add a value to Tile
protected int value;
// ...
}
You have expanded Tile
and added a new field. This field is not a field Tile
, it Tile
does not contain (does not see).
Based on the comment below :
When you declare a value in Tile
, you don't need to declare it in again TwoNTile
. You can make Tile objects just by not using constructors.
Tile t = new TwoNTile(...);
is a valid object Tile
. This way you can implement the logic you already tried using.
Have a look at static and dynamic binding in Java on SO, or google it .
source to share
You are trying to access a property from an abstract class that does not have it.
You need to cut it at a higher level, for example.
TwoNTile temp = (TwoNTile) moving;
I understand there can be more than one extension of this abstract class, but you need to carve it to the highest level in the class hierarchy for cast classes or its detectors to respond to.
Method updated :
public boolean mergesWith(Tile moving){
TwoNTile temp = (TwoNTile) moving;
if(this.value == temp.value){
return true;
}
else{
return false;
}
}
source to share
In your class, Tile
you need to add a class variable value
like:
package Game2048;
// Abstract notion of a game tile.
public abstract class Tile{
//need to add instance variable
int value;
// Returns true if this tile merges with the given tile.
public abstract boolean mergesWith(Tile other);
// Produce a new tile which is the result of merging this tile with
// the other. May throw an exception if merging is illegal
public abstract Tile merge(Tile other);
// Get the score for this tile.
public abstract int getScore();
// Return a string representation of the tile
public abstract String toString();
}
source to share