From 6594ea59d624b694cc8cbbab9b772054caf7a3e3 Mon Sep 17 00:00:00 2001 From: Justin Senn Date: Mon, 6 Feb 2023 21:59:56 +0100 Subject: [PATCH] update --- src/main/java/Snake/Controller.java | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) 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 {