Skip to content

Commit

Permalink
add stages
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolodavis committed Sep 10, 2019
1 parent d5e2b55 commit da2f0ea
Show file tree
Hide file tree
Showing 12 changed files with 394 additions and 316 deletions.
27 changes: 18 additions & 9 deletions src/ai/bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ export function Simulate({ game, bots, state, depth }) {

let metadata = null;
let iter = 0;
while (
state.ctx.gameover === undefined &&
state.ctx.actionPlayers.length > 0 &&
iter < depth
) {
const playerID = state.ctx.actionPlayers[0];
while (state.ctx.gameover === undefined && iter < depth) {
let playerID = state.ctx.currentPlayer;
if (state.ctx.stage) {
playerID = Object.keys(state.ctx.stage)[0];
}

const bot = bots instanceof Bot ? bots : bots[playerID];
const t = bot.play(state, playerID);

Expand Down Expand Up @@ -128,11 +128,16 @@ export class MCTSBot extends Bot {
if (playerID !== undefined) {
actions = this.enumerate(G, ctx, playerID);
objectives = this.objectives(G, ctx, playerID);
} else {
for (let playerID of ctx.actionPlayers) {
} else if (ctx.stage) {
for (let playerID in ctx.stage) {
actions = actions.concat(this.enumerate(G, ctx, playerID));
objectives = objectives.concat(this.objectives(G, ctx, playerID));
}
} else {
actions = actions.concat(this.enumerate(G, ctx, ctx.currentPlayer));
objectives = objectives.concat(
this.objectives(G, ctx, ctx.currentPlayer)
);
}

return {
Expand Down Expand Up @@ -212,7 +217,11 @@ export class MCTSBot extends Bot {
i++
) {
const { G, ctx } = state;
const moves = this.enumerate(G, ctx, ctx.actionPlayers[0]);
let playerID = ctx.currentPlayer;
if (ctx.stage) {
playerID = Object.keys(ctx.stage)[0];
}
const moves = this.enumerate(G, ctx, playerID);

// Check if any objectives are met.
const objectives = this.objectives(G, ctx);
Expand Down
9 changes: 7 additions & 2 deletions src/client/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,12 @@ class _ClientImpl {

this.step = async () => {
const state = this.store.getState();
const playerID = state.ctx.actionPlayers[0];

let playerID = state.ctx.currentPlayer;
if (state.ctx.stage) {
playerID = Object.keys(state.ctx.stage)[0];
}

const { action, metadata } = await bot.play(state, playerID);

if (action) {
Expand Down Expand Up @@ -315,7 +320,7 @@ class _ClientImpl {

let isActive = true;

const canPlayerMakeMove = this.game.flow.canPlayerMakeMove(
const canPlayerMakeMove = this.game.flow.canPlayerMakeAnyMove(
state.G,
state.ctx,
this.playerID
Expand Down
6 changes: 3 additions & 3 deletions src/client/client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ describe('event dispatchers', () => {
test('default', () => {
const game = {};
const client = Client({ game });
expect(Object.keys(client.events)).toEqual(['endTurn']);
expect(Object.keys(client.events)).toEqual(['endTurn', 'setStage']);
expect(client.getState().ctx.turn).toBe(1);
client.events.endTurn();
expect(client.getState().ctx.turn).toBe(2);
Expand All @@ -321,15 +321,14 @@ describe('event dispatchers', () => {
events: {
endPhase: true,
endGame: true,
setActionPlayers: true,
},
};
const client = Client({ game });
expect(Object.keys(client.events)).toEqual([
'endTurn',
'endPhase',
'endGame',
'setActionPlayers',
'setStage',
]);
expect(client.getState().ctx.turn).toBe(1);
client.events.endTurn();
Expand All @@ -341,6 +340,7 @@ describe('event dispatchers', () => {
events: {
endPhase: false,
endTurn: false,
setStage: false,
},
};
const client = Client({ game });
Expand Down

0 comments on commit da2f0ea

Please sign in to comment.