Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 | 1x 1x 1x 1x 1x 1x 11x 1x 1x 483x 483x 483x 483x 483x 5587x 5587x 5587x 142x 5587x 483x 1x 31x 186x 1152x 1152x 31x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 3x 34x 2x 32x 1x 31x 31x 31x 744x 31x 31x 31x 31x 31x 1x 1x 31x 1x 31x 31x 31x 31x 31x 31x 31x 3x 28x 28x | /**
* @author Timo Lehnertz
*/
import { Board } from "./Board";
import { BoardPosition } from "./BoardPosition";
import { GameState } from "./GameState";
import { Move } from "./Move";
import { ShiftPosition } from "./ShiftPosition";
import { Treasure } from "./Treasure";
const BEST_MOVE_WEIGHT = 10000;
/**
* Returns a number between 0 and BEST_MOVE_WEIGHT (inclusive) based on the standing
*/
export type EvaluatorFunction = (
gameState: GameState,
playerIndex: number
) => number;
export type MoveGenerator = (gameState: GameState) => Move;
export function generateShiftPositions(gameState: GameState): ShiftPosition[] {
return gameState.board.generateValidShiftPositions();
}
export function generateRandomMove(gameState: GameState): Move {
const shiftPositions = generateShiftPositions(gameState);
const shiftPositionIndex = Math.floor(Math.random() * shiftPositions.length);
const moves = generateMoves(
gameState,
shiftPositions[shiftPositionIndex],
Math.floor(Math.random() * 4)
);
const index = Math.floor(Math.random() * moves.length);
return moves[index];
}
export function generateMoves(
gameState: GameState,
toShiftPosition: ShiftPosition,
rotation: number
): Move[] {
const moves: Move[] = [];
const movedGameState = gameState
.setShiftPosition(toShiftPosition)
.rotateLooseTile(rotation)
.insertLooseTile();
const playerState = movedGameState.allPlayerStates.getPlayerToMoveState();
const reachableFields = movedGameState.board.getReachablePositions(
playerState.position
);
for (const reachableField of reachableFields) {
const treasureAtTile = movedGameState.board.getTreasureAt(reachableField);
let collectedTreasure: Treasure | null = null;
if (Treasure.compare(treasureAtTile, playerState.currentTreasure)) {
collectedTreasure = playerState.currentTreasure;
}
moves.push(
new Move(
movedGameState.allPlayerStates.playerIndexToMove,
rotation,
gameState.board.shiftPosition,
toShiftPosition,
playerState.position,
reachableField,
collectedTreasure
)
);
}
return moves;
}
export function positionByTreasure(
board: Board,
treasure: Treasure
): BoardPosition | null {
for (let x = 0; x < board.width; x++) {
for (let y = 0; y < board.height; y++) {
const tile = board.getTile(new BoardPosition(x, y));
if (Treasure.compare(tile.treasure, treasure)) {
return new BoardPosition(x, y);
}
}
}
return null;
}
function shuffle(array: any[]) {
let currentIndex = array.length;
// While there remain elements to shuffle...
while (currentIndex != 0) {
// Pick a remaining element...
let randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex--;
// And swap it with the current element.
[array[currentIndex], array[randomIndex]] = [
array[randomIndex],
array[currentIndex],
];
}
}
/**
*
* @param evaluate Eval function
* @param strength number between 0 and 1 0 => random move, 1 => strongest move
* @returns The move generator
*/
export function buildMoveGenerator(
evaluate: EvaluatorFunction,
strength: number
): MoveGenerator {
return (gameState: GameState): Move => {
let bestMove: Move | null = null;
let bestStanding = Number.MIN_SAFE_INTEGER;
for (let rotation = 0; rotation < 4; rotation++) {
const shiftPositions = generateShiftPositions(gameState);
for (const shiftPosition of shiftPositions) {
const moves = generateMoves(gameState, shiftPosition, rotation);
Iif (strength < 1) {
shuffle(moves);
}
const lookupAmount = Math.max(
moves.length - 1,
Math.min(1, Math.floor(moves.length * strength))
);
let i = 0;
for (const move of moves) {
if (i >= lookupAmount) {
break;
}
if (move.collectedTreasure !== null) {
return move;
}
let moveFound = false;
let standing = 0;
for (const historyMove of gameState.historyMoves) {
Iif (historyMove.equals(move)) {
moveFound = true;
break;
}
}
Iif (moveFound) {
// never prefer a repeated moved move to avoid infinite games between bots
standing = 0;
} else {
const potentialState = gameState.move(move);
standing = evaluate(
potentialState,
gameState.allPlayerStates.playerIndexToMove
);
}
Iif (standing === BEST_MOVE_WEIGHT) {
return move;
}
if (standing > bestStanding) {
bestStanding = standing;
bestMove = move;
}
i++;
}
}
}
Iif (bestMove === null) {
throw new Error("could not find any move");
}
return bestMove;
};
}
export function manhattanEvaluator(gameState: GameState, playerIndex: number) {
Iif (gameState.getWinnerIndex() === playerIndex) {
return BEST_MOVE_WEIGHT;
}
const playerState = gameState.allPlayerStates.getPlayerState(playerIndex);
Iif (playerState.currentTreasure === null) {
const home = Board.getPlayerHomePosition(
playerIndex,
gameState.board.width,
gameState.board.height
);
// playerState.position.distanceFrom(home) + Math.random() * 5;
const distance = playerState.position.manhattanDistanceFrom(home);
return BEST_MOVE_WEIGHT - distance;
}
// return Math.random();
const treasureLocation = positionByTreasure(
gameState.board,
playerState.currentTreasure
);
Iif (treasureLocation === null) {
return 0; // treasure is on loose tile
}
// const distance = playerState.position.distanceFrom(treasureLocation);
const diff = playerState.position.subtract(treasureLocation);
if (Math.abs(diff.x) === 1 && Math.abs(diff.y) === 1) {
return BEST_MOVE_WEIGHT - 1;
} else {
const distance =
playerState.position.manhattanDistanceFrom(treasureLocation);
return BEST_MOVE_WEIGHT - distance;
}
}
|