Author | SHA1 | Message | Date |
---|---|---|---|
Rob Hallam | 7a182cb7ea | feat: remove GameActor reference from GameState once assigned to AI | 9 months ago |
Rob Hallam | 7ac60058fa |
refactor: use setter - give GameState a *temporary* ref to GameActor
This reference in GameState can be removed once it is given to AIPlayer in game initialisation (setupGameState) |
9 months ago |
Rob Hallam | d123fa9810 | feat: call GameState::setupGameState() from initialise event handler | 9 months ago |
Rob Hallam | 87ed62cc7a |
feat: add setupGameState method for initialising game state
This will be called from the Initialise class, making the game state ready to be played- create player instances, board, tiles, cards etc |
9 months ago |
Rob Hallam | 7686cdd652 |
feat: move GameActor reference into AIPlayer
This avoid the ugly hack of keeping a referenec in GameState |
9 months ago |
@@ -65,7 +65,7 @@ public class GameActor extends AbstractActor { | |||||
// Initalize a new game state object | // Initalize a new game state object | ||||
gameState = new GameState(); | gameState = new GameState(); | ||||
// #hack: give the game state a reference to the game actor so AIPlayer can send messages | // #hack: give the game state a reference to the game actor so AIPlayer can send messages | ||||
gameState.gameActor = this; | |||||
gameState.setGameActor(this); | |||||
// Get the list of image files to pre-load the UI with | // Get the list of image files to pre-load the UI with | ||||
Set<String> images = ImageListForPreLoad.getImageListForPreLoad(); | Set<String> images = ImageListForPreLoad.getImageListForPreLoad(); | ||||
@@ -23,15 +23,7 @@ public class Initalize implements EventProcessor{ | |||||
@Override | @Override | ||||
public void processEvent(ActorRef out, GameState gameState, JsonNode message) { | public void processEvent(ActorRef out, GameState gameState, JsonNode message) { | ||||
// hello this is a change | |||||
gameState.gameInitalised = true; | |||||
gameState.something = true; | |||||
// User 1 makes a change | |||||
//CommandDemo.executeDemo(out); // this executes the command demo, comment out this when implementing your solution | |||||
//Loaders_2024_Check.test(out); | |||||
gameState.setupGameState(); | |||||
// start the turn demo | // start the turn demo | ||||
TurnDemo.executeDemo(out); | TurnDemo.executeDemo(out); | ||||
@@ -11,17 +11,36 @@ import structures.basic.AIPlayer; | |||||
* | * | ||||
*/ | */ | ||||
public class GameState { | public class GameState { | ||||
public boolean gameInitalised = false; | public boolean gameInitalised = false; | ||||
public boolean something = false; | public boolean something = false; | ||||
public int currentPlayer = 1; | public int currentPlayer = 1; | ||||
public AIPlayer aiPlayer = new AIPlayer(); | |||||
private GameActor gameActor; | |||||
// note: still a bit of a hack but we remove this from GameState | |||||
// and give it to the AIPlayer on initialisation, and it can then | |||||
// be removed from here | |||||
public AIPlayer aiPlayer; | |||||
public GameState() { | |||||
super(); | |||||
} | |||||
public void setGameActor(GameActor gameActor) { | |||||
this.gameActor = gameActor; | |||||
} | |||||
// bit of a hack but include a reference to the GameActor so AIPlayer can send messages to it | |||||
public GameActor gameActor; | |||||
/** Set up everything the game needs to be played (called via initialise game event) | |||||
* | |||||
*/ | |||||
public void setupGameState() { | |||||
// do game setup | |||||
this.gameInitalised = true; | |||||
// set initial player | |||||
this.currentPlayer = 1; | |||||
// create the AI player: | |||||
this.aiPlayer = new AIPlayer(this.gameActor); | |||||
this.gameActor = null; | |||||
} | |||||
} | } |
@@ -8,13 +8,20 @@ import com.fasterxml.jackson.databind.JsonNode; | |||||
import com.fasterxml.jackson.databind.ObjectMapper; | import com.fasterxml.jackson.databind.ObjectMapper; | ||||
import com.fasterxml.jackson.databind.node.ObjectNode; | import com.fasterxml.jackson.databind.node.ObjectNode; | ||||
import actors.GameActor; | |||||
import akka.actor.ActorRef; | import akka.actor.ActorRef; | ||||
public class AIPlayer extends Player { | public class AIPlayer extends Player { | ||||
private GameActor gameActor; | |||||
public AIPlayer() { | public AIPlayer() { | ||||
super(); | super(); | ||||
} | } | ||||
public AIPlayer(GameActor gameActor) { | |||||
super(); | |||||
this.gameActor = gameActor; | |||||
} | |||||
public void takeTurn(ActorRef out, GameState gameState) { | public void takeTurn(ActorRef out, GameState gameState) { | ||||
// addPlayer2Notification | // addPlayer2Notification | ||||
BasicCommands.addPlayer2Notification(out, "My turn!", 2); | BasicCommands.addPlayer2Notification(out, "My turn!", 2); | ||||
@@ -30,11 +37,10 @@ public class AIPlayer extends Player { | |||||
((ObjectNode) message).put("player", "AI"); | ((ObjectNode) message).put("player", "AI"); | ||||
// then send it to the GameActor: | // then send it to the GameActor: | ||||
try { | try { | ||||
gameState.gameActor.processMessage("endturnclicked", message); | |||||
this.gameActor.processMessage("endturnclicked", message); | |||||
} catch (Exception e) { | } catch (Exception e) { | ||||
e.printStackTrace(); | e.printStackTrace(); | ||||
} | } | ||||
// gameState.gameActor.receive(message); | |||||
} | } | ||||