Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/main/java/com/bdtripp/hauntedhouse/engine/CliRunner.java
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;
}

/**
Expand Down
21 changes: 16 additions & 5 deletions src/main/java/com/bdtripp/hauntedhouse/engine/GameEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/com/bdtripp/hauntedhouse/service/GameService.java
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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);
Expand Down
Loading