From 67a9721bb5a4ca03d896a7cf4de6efee554d036c Mon Sep 17 00:00:00 2001 From: Brian Tripp Date: Sat, 23 May 2026 15:56:17 -0500 Subject: [PATCH] refactor: decouple engine from world/processor creation --- .../hauntedhouse/engine/CliRunner.java | 8 ++++++- .../hauntedhouse/engine/GameEngine.java | 21 ++++++++++++++----- .../hauntedhouse/service/GameService.java | 10 ++++++++- 3 files changed, 32 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/bdtripp/hauntedhouse/engine/CliRunner.java b/src/main/java/com/bdtripp/hauntedhouse/engine/CliRunner.java index ba55eb3..823bf87 100644 --- a/src/main/java/com/bdtripp/hauntedhouse/engine/CliRunner.java +++ b/src/main/java/com/bdtripp/hauntedhouse/engine/CliRunner.java @@ -1,6 +1,7 @@ package com.bdtripp.hauntedhouse.engine; import com.bdtripp.hauntedhouse.model.Command; +import com.bdtripp.hauntedhouse.model.World; /** * Runs the game in a command-line environment. @@ -12,12 +13,17 @@ */ public class CliRunner { private Parser parser = new Parser(System.in); - private GameEngine gameEngine = new GameEngine(); + private GameEngine gameEngine; /** * Initializes the parser and game engine for cli play. */ public CliRunner() { + World world = new WorldBuilder().createWorld(); + GameEngine engine = new GameEngine(world); + CommandProcessor processor = new CommandProcessor(world, engine); + engine.setCommandProcessor(processor); + this.gameEngine = engine; } /** diff --git a/src/main/java/com/bdtripp/hauntedhouse/engine/GameEngine.java b/src/main/java/com/bdtripp/hauntedhouse/engine/GameEngine.java index 161cfac..713a70c 100644 --- a/src/main/java/com/bdtripp/hauntedhouse/engine/GameEngine.java +++ b/src/main/java/com/bdtripp/hauntedhouse/engine/GameEngine.java @@ -19,15 +19,26 @@ */ public class GameEngine { private final World world; - private final CommandProcessor commandProcessor; + private CommandProcessor commandProcessor; private boolean gameOver; /** - * Creates the GameEngine and the game world. + * Creates the GameEngine. */ - public GameEngine() { - world = new WorldBuilder().createWorld(); - commandProcessor = new CommandProcessor(world, this); + public GameEngine(World world) { + this.world = world; + } + + /** + * Injects the CommandProcessor used by the engine to interpret and execute player commands. + * + * This setter is required because the CommandProcessor also depends on the GameEngine, and + * therefore cannot be constructed until after the engine itself is created. + * + * @param processor the CommandProcessor responsible for handling commands + */ + public void setCommandProcessor(CommandProcessor processor) { + this.commandProcessor = processor; } /** diff --git a/src/main/java/com/bdtripp/hauntedhouse/service/GameService.java b/src/main/java/com/bdtripp/hauntedhouse/service/GameService.java index c04486c..d796694 100644 --- a/src/main/java/com/bdtripp/hauntedhouse/service/GameService.java +++ b/src/main/java/com/bdtripp/hauntedhouse/service/GameService.java @@ -1,11 +1,15 @@ package com.bdtripp.hauntedhouse.service; import com.bdtripp.hauntedhouse.engine.Parser; +import com.bdtripp.hauntedhouse.engine.WorldBuilder; import com.bdtripp.hauntedhouse.model.Command; import com.bdtripp.hauntedhouse.api.GameRequest; import com.bdtripp.hauntedhouse.api.GameResponse; +import com.bdtripp.hauntedhouse.engine.CommandProcessor; import com.bdtripp.hauntedhouse.engine.GameEngine; import com.bdtripp.hauntedhouse.model.GameStatus; +import com.bdtripp.hauntedhouse.model.World; + import org.springframework.stereotype.Service; import org.springframework.web.context.annotation.SessionScope; @@ -32,7 +36,11 @@ public GameService() { * @return a response containing a welcome message and status of the game */ public GameResponse startGame() { - gameEngine = new GameEngine(); + World world = new WorldBuilder().createWorld(); + GameEngine engine = new GameEngine(world); + CommandProcessor processor = new CommandProcessor(world, engine); + engine.setCommandProcessor(processor); + this.gameEngine = engine; gameEngine.startGame(); String output = gameEngine.buildWelcomeMessage() + "\n"; return new GameResponse(output, GameStatus.RUNNING);