import info.gridworld.world.World; import info.gridworld.grid.Location; import i
ID: 3554756 • Letter: I
Question
import info.gridworld.world.World;
import info.gridworld.grid.Location;
import info.gridworld.grid.BoundedGrid;
public class Snake
{
private Actor head;
private Actor tail;
private int foodEaten;
private boolean didEat;
private boolean isDead;
public Snake()
{
head = new Section();
head.setDirection(0);
tail = head;
foodEaten = 0;
isDead = false;
}
public void putSelfInGrid(Grid<Actor> grid, Location loc)
{
head.putSelfInGrid(grid, loc);
}
public void move()
{
//make sure snake is not dead and that the grid is not null
//make new Section for this Snake
//get the location of where this new Section might appear
//check for valid location
//check to see if this spot is not null
//check to see if a Snake Section found
//else check to see if Food found
//if spot is null
//move up to this spot
//remove the tail is snake is trying to move off the grid
}
public void removeTail()
{
//if the snake is moving normally
//the tail gets removed as a new section is added to the front
}
public void setDirection(String dir)
{
//change the direction of the head of the snake
}
public int getFoodEaten()
{
return 0;
}
public void setFoodEaten(int amount)
{
}
public boolean isDead()
{
return false;
}
}
Explanation / Answer
Please find below working code for this game. Use this and if it works rate me. Thanks.....
import info.gridworld.grid.Grid;
import info.gridworld.grid.Location;
import info.gridworld.actor.Actor;
import info.gridworld.actor.Bug;
import info.gridworld.actor.Flower;
import info.gridworld.actor.Rock;
import java.util.ArrayList;
import javax.swing.UIManager;
public class SnakeBug extends Bug {
private int grow;
private ArrayList<Location> snake;
public SnakeBug() {
grow = 4;
ArrayList snake = new ArrayList<Location>();
}
public void randomFlower() {
Grid<Actor> gr = getGrid(); // In every method!
int row, column;
Location loc;
do {
row = (int) (Math.random() * gr.getNumRows()); // random from
// 0-NumRows
column = (int) (Math.random() * gr.getNumCols()); // random from
// 0-NumCols
loc = new Location(row, column);
} while (gr.get(loc) != null);
Flower flower = new Flower(getColor());
flower.putSelfInGrid(gr, loc);
}
public void addRockBehindHead(Location loc) {
Grid<Actor> gr = getGrid();
Rock rock = new Rock(getColor());
rock.putSelfInGrid(gr, loc);
snake.add(loc);
snake.add(0, loc);
}
public void eraseTail(Location loc) {
Grid<Actor> gr = getGrid();
Actor r = gr.get(loc);
r.removeSelfFromGrid();
}
public void act() {
Grid<Actor> gr = getGrid();
Actor inFront = null;
if (!canMove()) {
javax.swing.JOptionPane.showMessageDialog(null,
"Score: " + snake.size(), "GAME OVER!", 0);
}else{
Location loc;
ArrayList<Location> getEmptyAdjacentLocations = Location;
inFront = gr.get(loc);
if (inFront instanceof Flower) {
grow += 3;
int randomFlower;
}
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.