Question
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
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;
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;
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; } }
// TODO: choose a new location for the apple // given a randomly generated x and y location, loop over the block linked list, // and make sure the generated x and y location isnt on the snakes position. //Alsp be sure that the apple isnt placed at the position of the bomb. // once you have determined that the x and y position is unoccupied, place the new apple. public void plantApple() { int x; int y; while (true) { x = Math.abs(random.nextInt() % 29); y = Math.abs(random.nextInt() % 29);
//TODO check the apple location is good break; } apple.x = x; apple.y = y; } // TODO: choose a new location for the bomb // given a randomly generated x and y location, loop over the block linked list, // and make sure the generated x and y location isnt on the snakes position. // Also be sure that the bomb isn't placed at the position of the apple // once you have determined that the x and y position is unoccupied, place the new bomb. public void plantBomb() { int x; int y; while (true) { x = Math.abs(random.nextInt() % 29); y = Math.abs(random.nextInt() % 29); //TODO check the bomb location is good break; } bomb.x = x; bomb.y = y; }
// did the snake find the apple? //TODO: if the head location of the snake queue is at the same position of the apple, //enqueue a new block at the position of the tail reference of the queue, clear the cell symbol where the apple was, //and draw a new apple by calling its method. // You can draw a blank cell by calling drawSymbol(apple.y, apple.x, NamedSymbol.none, ac); public void detectApple() { } // did the snake find the bomb? //TODO: if the head location of the snake queue is at the same position of the bomb, //dequeue the head of the snake, clear the cell symbol where the bomb was, //and draw a new bomb by calling its method. // You can draw a blank cell by calling drawSymbol(bomb.y, bomb.x, NamedSymbol.none, ac); public void detectBomb() { }
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; } }
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; } }
// Set up the first state of the game grid //TODO: Init a queue of blocks that represent the snake. //the head and tail pointers are currently not initialized. //The head variable can represent the first block in the linked list queue and the tail can represent the last block 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); } } //TODO: START LINKNED LIST QUEUE HERE BY ASSIGNING HEAD AND TAIL VARIABLES
for (int i = 0; i < startLength; ++i) { setBGColor(startY, startX - i, fg); if (i > 0) { //TODO: BASED ON THE SIZE OF THE STARTING SNAKE, ENQUEUE A NEW BLOCK TO THE LINKED LIST } }
frameTime = System.nanoTime(); nextFrameTime = frameTime + FRAMERATE; dir = Direction.EAST; lastDir = dir; apple = new Block(); bomb = new Block(); plantApple(); plantBomb(); } public void gameLoop() { handleInput(); if (System.nanoTime() > nextFrameTime) { frameTime = System.nanoTime(); nextFrameTime = frameTime + FRAMERATE;
lastDir = dir;
detectApple(); detectBomb();
updatePosition();
paint();
detectDeath();
} }
//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) { 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; } }
TODOs please
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started