Commit 9c205567 authored by Joseph Walton-Rivers's avatar Joseph Walton-Rivers 🐦

handle calling tick for the agents

parent 47be88aa
Pipeline #2150 passed with stages
in 2 minutes and 33 seconds
......@@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changes
- Agents no longer need to manage history when forwarding the state, this will be handled by the action
- The copy constructor is no longer visible for BasicState, instead the getCopy method should be used
- The game will handle calling tick for the agents, calling tick will now result in a warning message being printed.
### Fixes
- Fix spelling of information in state interface
......
......@@ -206,7 +206,6 @@ public class GameRunner {
while (!state.isGameOver()) {
try {
state.tick();
writeState(state);
nextMove();
} catch (RulesViolation rv) {
......
......@@ -42,6 +42,17 @@ public class AgentPlayer implements Player {
return policy.doMove(playerID, state);
}
/**
* Resolve a turn.
*
* We don't know enouph to apply the action (ie, the deck order and our own cards).
* Instead, we use the effects of the action (Events) to update our state.
* Because the action is never executed on our side, our tick counter won't update, so do that to.
*
* @param actor the player who just made a move
* @param action the move the player made
* @param what we saw happen when the move was made
*/
@Override
public void resolveTurn(int actor, Action action, List<GameEvent> events) {
Objects.requireNonNull(state);
......@@ -55,8 +66,8 @@ public class AgentPlayer implements Player {
event.apply(state, this.playerID);
}
// tick the game state
state.tick();
// tick the state
state.actionTick();
}
@Override
......
......@@ -76,7 +76,6 @@ public class Individual {
Action myAction = possible.get(random.nextInt(possible.size()));
myAction.apply(playerId, forward);
}
forward.tick();
playerId = (playerId + 1) % state.getPlayerCount();
}
......
......@@ -165,7 +165,6 @@ public class MCTS implements Agent {
Action action = current.getAction();
if (action != null) {
action.apply(agent, state);
state.tick();
}
if (iterationObject.isMyGo(agent)) {
......@@ -243,7 +242,6 @@ public class MCTS implements Agent {
while (!state.isGameOver() && moves < rolloutDepth) {
Action action = selectActionForRollout(state, playerID);
action.apply(playerID, state);
state.tick();
playerID = (playerID + 1) % state.getPlayerCount();
moves++;
}
......
......@@ -107,7 +107,6 @@ public class MCTSPredictor extends MCTS {
Action action = current.getAction();
if (action != null) {
action.apply(agent, state);
state.tick();
}
if (iterationObject.isMyGo(agent)) {
......
......@@ -150,7 +150,6 @@ public class MCTSExpConst implements Agent {
Action action = current.getAction();
if (action != null) {
action.apply(agent, state);
state.tick();
}
if (iterationObject.isMyGo(agent)) {
......@@ -228,7 +227,6 @@ public class MCTSExpConst implements Agent {
while (!state.isGameOver() && moves < rolloutDepth) {
Action action = selectActionForRollout(state, playerID);
action.apply(playerID, state);
state.tick();
playerID = (playerID + 1) % state.getPlayerCount();
moves++;
}
......
......@@ -104,7 +104,6 @@ public class MCTSPredictorExpConst extends MCTSExpConst {
Action action = current.getAction();
if (action != null) {
action.apply(agent, state);
state.tick();
}
if (iterationObject.isMyGo(agent)) {
......
......@@ -40,7 +40,7 @@ class FirstMoveGameRunner extends GameRunner {
@Override
public GameStats playGame(Long seed) {
init(seed);
state.tick();
//state.tick();
nextMove();
return null;
......
......@@ -524,6 +524,11 @@ public class BasicState implements GameState {
public void setTableValue(CardColour colour, int value) {
table.put(colour, value);
}
@Deprecated
public void tick() {
LOG.warn("Tick is handled automaticlly, if you are applying events call actionTick instead");
}
/**
* Countdown clock used to keep track of remaining moves in the end game.
......@@ -531,7 +536,7 @@ public class BasicState implements GameState {
* This should be called any time the state is advanced.
*/
@Override
public void tick() {
public void actionTick() {
turnNumber++;
if (!deck.hasCardsLeft()) {
movesLeft--;
......
......@@ -212,12 +212,16 @@ public interface GameState extends Serializable {
*/
void setTableValue(CardColour c, int nextValue);
@Deprecated
void tick();
/**
* Update state turn information.
*
* This should be called when moves are made to ensure the game turn count and cards remaining remains in sync.
* This is called when an action is applied.
* If you are applying events manually, then call this when applying them.
*/
void tick();
void actionTick();
/**
* Return the game event history.
......
......@@ -7,8 +7,28 @@ import java.util.List;
import java.util.Objects;
public class HistoryEntry {
/**
* The player that performed this action.
*
* If the player ID is negative, the events occoured without a player making an action.
* This should only happen at the start of the game to deal cards.
*/
public final int playerID;
/**
* The action the player made.
*
* If this is null, the events are from the game rather than another player's action
* (ie, dealing cards at the start of the game).
*
* JWR: yes, it's a little hacky - I couldn't think of a better way of doing it, I'm sorry.
*/
public final Action action;
/**
* Our observations of the action.
*/
public final List<GameEvent> history;
public HistoryEntry(int playerID, Action action, List<GameEvent> history){
......
......@@ -52,6 +52,7 @@ public class DiscardCard implements Action {
// update state history
game.addAction(playerID, this, events);
game.actionTick();
return events;
}
......
......@@ -69,6 +69,7 @@ public class PlayCard implements Action {
//update state history
game.addAction(playerID, this, events);
game.actionTick();
return events;
}
......
......@@ -50,6 +50,7 @@ public class TellColour implements Action {
//handle history management for the state
List<GameEvent> effects = Collections.singletonList(cardInformation);
game.addAction(playerID, this, effects);
game.actionTick();
return effects;
}
......
......@@ -56,6 +56,7 @@ public class TellValue implements Action {
//update state history
List<GameEvent> effects = Collections.singletonList(cardInformation);
game.addAction(playerID, this, effects);
game.actionTick();
return effects;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment