diff --git a/src/main/java/Snake/Controller.java b/src/main/java/Snake/Controller.java index 6fb4a14..23b6cfa 100644 --- a/src/main/java/Snake/Controller.java +++ b/src/main/java/Snake/Controller.java @@ -9,6 +9,9 @@ public class Controller { private enum GameState { Running }; private GameView gameView; private Snake snakeModel; + private SnakeView snakeView; + private Apple appleModel; + private AppleView appleView; private boolean inputHandled; private GameState gameState; @@ -25,6 +28,31 @@ public class Controller { gameView.getActionMap().put("move down", new MoveAction(Snake.SnakeDirection.DOWN)); gameView.getActionMap().put("move right", new MoveAction(Snake.SnakeDirection.RIGHT)); } + + private void selectApplesPosition() + { + do + { + appleModel.selectGridPosition(gameView.getGridSize()); + } while(!isApplePositionIsValid()); + appleView.setPosition(appleModel.getPosition()); + } + private boolean isApplePositionIsValid() + { + var snakeSegments = snakeModel.getBodySegments(); + for(var segmentPosition: snakeSegments) + { + if(segmentPosition.equals(appleModel.getPosition())) + return false; + } + return true; + } + private void updateSnakeViewPosition() + { + snakeView.setPositions(snakeModel.getBodySegments()); + } + + private class MoveAction extends AbstractAction {