adamjw / SnakeWS

0 stars 1 forks source link

4] The snake doesn't move #4

Closed adamjw closed 9 years ago

adamjw commented 9 years ago

Press space to start the game.

Implement the two functions in snake.game.controller.SnakeController: updateMovingDirection and move. These two calls will need to be added to updateLogic in snake.game.SnakeGame.

Acceptance Criteria:

  1. The snake moves according to the latest keyboard input UNLESS that movement is impossible (the snake can't move up if it was just moving down, for example).
adamjw commented 9 years ago

So, lists. If you want to read a lot about generics: http://docs.oracle.com/javase/tutorial/java/generics/why.html

Here are some brief notes:

  1. You can declare a variably of type list by using this syntax: List<MyClass> myList = ... where MyClass would be the name of a class like String or Integer. (Integer is a class and int is a primitive. Hah!)
  2. You have a couple options for iterating over collections.
for(MyClass myObject : myList) {
  System.out.println(myObject);
}
for(int i = 0; i < myList.size(); i++) {
  MyClass myObject = myList.get(i);
  System.out.println(myObject);
}

The first is generally more elegant.

adamjw commented 9 years ago

For updateMovingDirection:

The keyboard object of type BufferedKeyboard holds a list of keys pressed since the last time the snake was moved. The order of the keys is from oldest to newest. That is, the key at index 0 was pressed before the key at index n > 0. Keyboard keys are represented as integer values. To map the integer value to a direction, use the helper method getDirectionForKey.

It would also be elegant to write a helper method that decides if that direction is valid. My suggestion would be to write a private method that returns true if the direction is valid, and false otherwise.

adamjw commented 9 years ago

For move:

Note that the Position class is immutable, too. Look through the Snake class's methods to see what will help you (F3 to navigate to the definition).