Need help implemting the last few methods Similar to food, the head of the snake
ID: 3826917 • Letter: N
Question
Need help implemting the last few methods
Similar to food, the head of the snake shall have a timer with initial value 0. Upon reaching MAX_SNAKE_TIMER, the timer resets. SnakeHeads also keep track of the length of a snake (i.e., they inherit from Snake), which should initially be set to 4. When the timer resets, the snake shall try to find a random cell closer to the mouse, using the appropriate method of the Cell class. If no such cell exists, it will then try to find any random open cell. If no such second cell exists, then the player loses the game, and the program shall use a static method in Config to end the game. The score is the total length of the snake at that point. If the snake successfully found a next cell, it shall move to that cell (using the moveState method of Cell), and create a new SnakeSegment inside its current cell. If the next cell contained Food, the snake shall increment its length. We shall represent SnakeHeads with an 'S'. You can use any color you wish for the snake head.
package hw4;
import java.awt.Color;
import graph.Cell;
import state.Snake;
import state.State;
import main.Config;
public class SnakeHead implements Snake,State
{
private int snakeHeadTimer=0;
private int snakeLength=4;
int counter=0;
int MAX_SNAKE_TIMER = 10;
public SnakeHead()
{
}
@Override
public void handle(Cell cell)
{
counter++;
if(counter == MAX_SNAKE_TIMER)
{
counter = 0;
Cell newCell = cell.getRandomCloser();
if(newCell != null)
{
if('D'==newCell.getState().toChar()||'F'==newCell.getState().toChar())
{
snakeLength++;
}
}
else
{
int score = getLength();
Config.endGame(score);
}
}
}
/**
* Get the current color of the state (can be used for drawing).
*/
@Override
public Color getColor()
{
// TODO Auto-generated method stub
return null;
}
/**
* 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.
*/
@Override
public boolean isPassable()
{
// TODO Auto-generated method stub
return false;
}
/**
* Get the character representation
* for this State. Used for loading map text files.
*/
@Override
public char toChar()
{
// TODO Auto-generated method stub
return 0;
}
/**
* Gets the length of the snake.
*/
@Override
public int getLength()
{
// TODO Auto-generated method stub
return 0;
}
}
Explanation / Answer
import java.awt.Color;
import graph.Cell;
import state.Snake;
import state.State;
import main.Config;
public class SnakeHead implements Snake,State
{
private int snakeHeadTimer=0;
private int snakeLength=4;
int counter=0;
int MAX_SNAKE_TIMER = 10;
public SnakeHead()
{
}
@Override
public void handle(Cell cell)
{
counter++;
if(counter == MAX_SNAKE_TIMER)
{
counter = 0;
Cell newCell = cell.getRandomCloser();
if(newCell != null)
{
if('D'==newCell.getState().toChar(newCell.getState())||'F'==newCell.getState().toChar(newCell.getState()))
{
snakeLength++;
}
}
else
{
int score = getLength();
Config.endGame(score);
}
}
}
/**
* Get the current color of the state (can be used for drawing).
*/
@Override
public Color getColor()
{
return Color.red;
}
/**
* 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.
*/
@Override
public boolean isPassable(Cell newCell)
{
if('D'==newCell.getState().toChar(newCell.getState())||'F'==newCell.getState().toChar(newCell.getState()))
{
return true;
}
return false;
}
/**
* Get the character representation
* for this State. Used for loading map text files.
*/
@Override
public char toChar(String state)
{
// TODO Auto-generated method stub
return state.charAt(0);
}
/**
* Gets the length of the snake.
*/
@Override
public int getLength()
{
return snakeLength;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.