japan free jump
This commit is contained in:
parent
a80cf7ab6f
commit
fc4db3e01e
4 changed files with 47 additions and 26 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -4,3 +4,4 @@ node_modules
|
|||
viewer/public/*.jpg
|
||||
viewer/public/*.jpeg
|
||||
viewer/public/*.png
|
||||
.pnpm-store
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ import { range } from 'lodash';
|
|||
import { GameState, Phase, Player, PowerPlantType, ResourceType } from './gamestate';
|
||||
import { MoveName } from './move';
|
||||
import prices from './prices';
|
||||
import { minBy } from './utils';
|
||||
|
||||
export interface AvailableMoves {
|
||||
[MoveName.ChoosePowerPlant]?: number[];
|
||||
|
|
@ -690,14 +691,15 @@ function dijkstra(G: GameState, player: Player): { name: string; price: number }
|
|||
visited: false,
|
||||
}));
|
||||
|
||||
let currentNode = nodes.find((n) => n.name == player.cities[0].name);
|
||||
if (!currentNode) {
|
||||
const start = nodes.find((n) => n.name == player.cities[0].name);
|
||||
if (!start) {
|
||||
// Player's first city is not in the current map — return all cities as unreachable.
|
||||
return nodes;
|
||||
}
|
||||
currentNode.price = 0;
|
||||
start.price = 0;
|
||||
|
||||
while (nodes.some((n) => !n.visited)) {
|
||||
let currentNode: typeof start = start;
|
||||
while (true) {
|
||||
const currentConnections = G.map.connections.filter((c) => c.nodes.includes(currentNode.name));
|
||||
currentConnections.forEach((connection) => {
|
||||
const otherName = connection.nodes.filter((n) => n != currentNode.name)[0];
|
||||
|
|
@ -710,29 +712,14 @@ function dijkstra(G: GameState, player: Player): { name: string; price: number }
|
|||
|
||||
currentNode.visited = true;
|
||||
|
||||
if (!nodes.some((n) => !n.visited)) {
|
||||
break;
|
||||
}
|
||||
|
||||
currentNode = nodes.reduce((a, b) => {
|
||||
if (a.visited) {
|
||||
return b;
|
||||
}
|
||||
|
||||
if (b.visited) {
|
||||
return a;
|
||||
}
|
||||
|
||||
if (a.price <= b.price) {
|
||||
return a;
|
||||
}
|
||||
|
||||
return b;
|
||||
});
|
||||
|
||||
if (currentNode.price > player.money) {
|
||||
// Pick the cheapest still-reachable unvisited node; stop if there is
|
||||
// none, or if it would already cost more than the player can afford.
|
||||
const unvisited = nodes.filter((n) => !n.visited);
|
||||
const next = minBy(unvisited, (n) => n.price);
|
||||
if (!next || next.price > player.money) {
|
||||
break;
|
||||
}
|
||||
currentNode = next;
|
||||
}
|
||||
|
||||
return nodes;
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
import { expect } from 'chai';
|
||||
import { shuffle } from './utils';
|
||||
import { minBy, shuffle } from './utils';
|
||||
|
||||
describe('utils', () => {
|
||||
describe('shuffle', () => {
|
||||
|
|
@ -10,4 +10,20 @@ describe('utils', () => {
|
|||
expect(shuffle([1, 2, 3, 4], 'a')).to.have.ordered.members([4, 2, 3, 1]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('minBy', () => {
|
||||
it('returns the element with the smallest key', () => {
|
||||
expect(minBy([{ p: 3 }, { p: 1 }, { p: 2 }], (x) => x.p)).to.deep.equal({ p: 1 });
|
||||
});
|
||||
|
||||
it('breaks ties by returning the earlier element', () => {
|
||||
const a = { id: 'a', p: 1 };
|
||||
const b = { id: 'b', p: 1 };
|
||||
expect(minBy([a, b], (x) => x.p)).to.equal(a);
|
||||
});
|
||||
|
||||
it('returns undefined for an empty array', () => {
|
||||
expect(minBy([], (x: number) => x)).to.equal(undefined);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -5,6 +5,23 @@ export function asserts<T>(move: any): asserts move is T {}
|
|||
|
||||
/* eslint-enable */
|
||||
|
||||
/**
|
||||
* Return the element of `array` with the smallest `key(element)`. Ties go to
|
||||
* the earlier element. Returns `undefined` if the array is empty.
|
||||
*/
|
||||
export function minBy<T>(array: readonly T[], key: (item: T) => number): T | undefined {
|
||||
let best: T | undefined;
|
||||
let bestKey = Infinity;
|
||||
for (const item of array) {
|
||||
const k = key(item);
|
||||
if (k < bestKey) {
|
||||
best = item;
|
||||
bestKey = k;
|
||||
}
|
||||
}
|
||||
return best;
|
||||
}
|
||||
|
||||
export function shuffle<T>(array: T[], seed: string): T[] {
|
||||
const rng = seedrandom.alea(seed);
|
||||
const reverse = new Map<number, number>();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue