Question: java program snake game -- complete all the todos for the code import bridges.base.NamedColor; import bridges.base.NamedSymbol; import bridges.games.NonBlockingGame; import java.util.*; public class SnakeScaffold extends NonBlockingGame{

java program snake game -- complete all the todos for the code

import bridges.base.NamedColor;

import bridges.base.NamedSymbol;

import bridges.games.NonBlockingGame;

import java.util.*;

public class SnakeScaffold extends NonBlockingGame{

java.util.Random random = new java.util.Random();

static int gridColumns = 30;

static int gridRows = 30;

final long FRAMERATE = 1000000000 / 15;

int startX = gridColumns / 3;

int startY = gridRows / 2;

int startLength = 3;

long frameTime;

long nextFrameTime;

// define the snake and apple with the Block object type

Block head;

Block tail;

Direction dir;

Block apple;

Block bomb;

Direction lastDir;

NamedColor bg = NamedColor.forestgreen;

NamedColor bc = NamedColor.green;

NamedColor fg = NamedColor.silver;

NamedColor hc = NamedColor.white;

NamedColor ac = NamedColor.red;

// constructor, bridges authentication

public SnakeScaffold(int assid, String login, String apiKey, int c, int r) {

super(assid, login, apiKey, c, r);

}

// keep track of user interaction to move the snake

public void handleInput() {

if (keyLeft() && dir != Direction.EAST && lastDir != Direction.EAST) {

dir = Direction.WEST;

}

else if (keyUp() && dir != Direction.SOUTH && lastDir != Direction.SOUTH) {

dir = Direction.NORTH;

}

else if (keyDown() && dir != Direction.NORTH && lastDir != Direction.NORTH) {

dir = Direction.SOUTH;

}

else if (keyRight() && dir != Direction.WEST && lastDir != Direction.WEST) {

dir = Direction.EAST;

}

}

// update snake position

public void updatePosition() {

Block current = head.next;

int nextX = head.x;

int nextY = head.y;

Block nextPos = head;

while (current != null) {

int tempX = current.x;

int tempY = current.y;

current.x = nextX;

current.y = nextY;

nextX = tempX;

nextY = tempY;

current = current.next;

}

switch (dir) {

case NORTH:

head.y--;

if (head.y < 0)

head.y = gridRows - 1;

break;

case SOUTH:

head.y++;

if (head.y == gridRows)

head.y = 0;

break;

case EAST:

head.x++;

if (head.x == gridColumns)

head.x = 0;

break;

case WEST:

head.x--;

if (head.x < 0)

head.x = gridColumns - 1;

break;

}

}

public void plantApple() {

int x;

int y;

while (true) {

x = Math.abs(random.nextInt() % 29);

y = Math.abs(random.nextInt() % 29);

boolean collision = false;

Block current = head;

while (current != null) {

if (current.x == x && current.y == y) {

collision = true;

break;

}

current = current.next;

}

if (!collision && (bomb == null || bomb.x != x || bomb.y != y)) {

break;

}

}

apple.x = x;

apple.y = y;

}

public void plantBomb() {

int x;

int y;

while (true) {

x = Math.abs(random.nextInt() % 29);

y = Math.abs(random.nextInt() % 29);

boolean collision = false;

Block current = head;

while (current != null) {

if (current.x == x && current.y == y) {

collision = true;

break;

}

current = current.next;

}

if (!collision && (apple == null || apple.x != x || apple.y != y)) {

break;

}

}

bomb.x = x;

bomb.y = y;

}

public void detectApple() {

if (head.x == apple.x && head.y == apple.y) {

Block newBlock = new Block(tail.x, tail.y);

tail.next = newBlock;

tail = newBlock;

drawSymbol(tail.y, tail.x, NamedSymbol.none, fg);

drawSymbol(apple.y, apple.x, NamedSymbol.none, ac);

plantApple();

}

}

public void detectBomb() {

if (head.x == bomb.x && head.y == bomb.y) {

head = head.next;

drawSymbol(head.y, head.x, NamedSymbol.none, bg);

drawSymbol(bomb.y, bomb.x, NamedSymbol.none, ac);

if (head == null) {

System.exit(0);

}

plantBomb();

}

}

// did the snake eat itself?

// end the game if the snake collides with itself

public void detectDeath() {

Block current = head.next;

while (current != null) {

if (head.x == current.x &&

head.y == current.y)

System.exit(0);

current = current.next;

}

}

// redraw the board with snake and apple updated positions

public void paint() {

for (int i = 0; i < gridColumns; ++i) {

for (int j = 0; j < gridRows; ++j) {

if (i % 2 == j % 2)

setBGColor(j, i, bg);

else

setBGColor(j, i, bc);

}

}

setBGColor(head.y, head.x, hc);

drawSymbol(apple.y, apple.x, NamedSymbol.apple, ac);

drawSymbol(bomb.y, bomb.x, NamedSymbol.bomb, ac);

Block current = head.next;

while (current != null) {

setBGColor(current.y, current.x, fg);

current = current.next;

}

}

public void initialize() {

System.out.println("initialize");

for (int i = 0; i < gridColumns; ++i) {

for (int j = 0; j < gridRows; ++j) {

if (i % 2 == j % 2)

setBGColor(j, i, bg);

else

setBGColor(j, i, bc);

}

}

head = new Block(startX, startY);

tail = head;

for (int i = 0; i < startLength; ++i) {

setBGColor(startY, startX - i, fg);

if (i > 0) {

tail = enqueue(tail, new Block(startX - i, startY));

}

}

frameTime = System.nanoTime();

nextFrameTime = frameTime + FRAMERATE;

dir = Direction.EAST;

lastDir = dir;

apple = new Block();

bomb = new Block();

plantApple();

plantBomb();

}

// Game loop will run many times per second.

// handle input, check if apple was detected, update position, redraw,

// detect if snake ate itself

public void gameLoop() {

handleInput();

if (System.nanoTime() > nextFrameTime) {

frameTime = System.nanoTime();

nextFrameTime = frameTime + FRAMERATE;

lastDir = dir;

detectApple();

detectBomb();

updatePosition();

paint();

detectDeath();

}

}

public static void main(String args[]) {

SnakeScaffold game = new SnakeScaffold(22, "", "", gridColumns, gridRows);

game.setTitle("snake");

game.setDescription("Snake: Eat the food, not yourself!");

game.start();

}

//TODO: Handle enqueue of a new block to the linked list, the tail is the last position in the linked list,

//next is the new block to enqueue.

//Return the tail of the list.

public static Block enqueue(Block tail, Block next) {

if ()

return tail;

}

//TODO: Handle dequeue of a new block to the linked list, the head is the first position in the linked list,

//Return the head of the list.

public static Block dequeue(Block head) {

return head;

}

}

enum Direction {

NORTH,

SOUTH,

EAST,

WEST

}

class Block {

public Block next;

public int x;

public int y;

public Block() {

this(-1, -1, null);

}

public Block(int x, int y) {

this(x, y, null);

}

public Block(int x, int y, Block next) {

this.x = x;

this.y = y;

this.next = next;

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!