Topic 1 - Variables and Data Types
Topic 2 - Conditionals and Strings
Topic 3 - Loops
Topic 4 - Arrays
Topic 5 - File Handling
Semester 1 Projects
Topic 6 - Classes/Objects and Methods
Topic 7 - ArrayLists
Semester Projects

Part 1c: Move the Snake

In SnakeProBrain.java -> updateSnake():

updateSnake() calls the private method advanceTheSnake, which moves the snake to the cell (nextCell) provided as an argument. Fill in the rest of the method advanceTheSnake so that the snake moves, keeping in mind the following requirements and implementation details:

  • You can’t modify the snakeCells in SnakeProData.java to be public. They have to stay private. 
  • You must therefore write a helper method or two in SnakeProData.java to modify the data for your game (e.g. snakeCells) as desired. snakeCells is of type LinkedList<BoardCell>, which is the Java-library version of a double-ended queue implemented via a linked list. This sounds complicated but its just an ArrayList, except it has a few more methods you can use. You will thus have access to the methods listed and described at: http://docs.oracle.com/javase/6/docs/api/index.html?java/util/LinkedList.html
  • You should NOT create any new BoardCells or modify the row or column of any existing BoardCells. Instead, you should use the BoardCell methods becomeHead(), becomeBody(), and becomeOpen(). 
  • If nextCell is part of the body or a wall, the game is over (this code is already written).
  • If nextCell is NOT food, the snake should move (i.e., nextCell will be the new head and the last piece of the tail will no longer be part of the snake.)
  • If nextCell IS food, the head should just be moved forward (and the tail stays the same spot). This causes the snake to move forward AND grow by one square.

In SnakeProData:

  • You’ll need to add certain helper methods, which will then be called in advanceTheSnake().
  • Add the following, add or remove cells from the snakeCells arrayList
public void appendHead(BoardCell newHead)

and

public void removeTail()

Tests

When you complete this part, the test cases in SnakeProBrainTest_AdvanceSnake.java should pass.