Need help implementing this Java class Food shall have a timer (counter) that ge
ID: 3825767 • Letter: N
Question
Need help implementing this Java class
Food shall have a timer (counter) that gets updated each time it handles a cell. When it reaches the MAX_FOOD_TIMER, food shall reset its timer to its initial value, 0. The color of food shall depend on the timer, using the timer as an index into FOOD_COLORS to slowly increase in color. Finally, snakes shall mow through food, so food shall be passable. We shall represent food with an 'F'.
A flashing passable state that uses the default food variables. An "F" in a map file.
Constructor Summary
Method Summary
Get the current color of the state (can be used for drawing).
Updates the cell based off of the state.
Get whether or not the cell is passable.
Get the character representation for this State.
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Constructor Detail
Food
Method Detail
getColor
Description copied from interface: State
Get the current color of the state (can be used for drawing).
Specified by:
getColor in interface State
Returns:
The color of the state
handle
Description copied from interface: State
Updates the cell based off of the state. This method can update the cell's state, or potentially another cell's state depending on the implementation.
Specified by:
handle in interface State
Parameters:
cell - The cell that this state belongs to
isPassable
Description copied from interface: State
Get whether or not the cell is passable. Affects whether or not a state can move through another state via random movement or moving closer to the mouse.
Specified by:
isPassable in interface State
Returns:
true iff the state is passable
See Also:
Cell.getRandomOpen(), Cell.getRandomCloser()
toChar
Description copied from interface: State
Get the character representation for this State. Used for loading map text files.
Specified by:
toChar in interface State
Returns:
character representation for this State
Constructors Constructor and Description Food()Explanation / Answer
Below are the details of class and interface as requested in question:
interface state.java
import java.awt.Color;
/*
* State Interface
*/
public interface State {
public Color getColor();
public boolean isPassable();
public char toChar();
}
Class Food.java
package wall;
import java.awt.Color;
/*
* Wall class that extends Object and implements state
*/
import javafx.scene.control.Cell;
public class Food extends Object implements State {
private Color color;
private boolean passable;
int MAX_FOOD_TIMER=10;
int FOOD_COLOR;
int counter;
public Food(){
color=Color.WHITE;//Walls shall be white
passable=true;//food shall be passable.
}
@Override
public Color getColor() {
return color;
}
@Override
public boolean isPassable() {
return passable;
}
@Override
public char toChar() {
return 'F';//represent food with an 'F'
}
public void handle(Cell cell){
counter++;
FOOD_COLOR=counter;
if(counter==MAX_FOOD_TIMER){
counter=0;
color.brighter();
}
}
}
FoodTest.java
package wall;
public class FoodTest {
public static void main(String[] args) {
Food food=new Food();
System.out.println("FoodColor color...."+food.getColor());
System.out.println("Is Food Passable....."+food.isPassable());
System.out.println("Food char..."+food.toChar());
}
}
Sample output
FoodColor color....java.awt.Color[r=255,g=255,b=255]
Is Food Passable.....true
Food char...F
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.