Walls placement algorithm overhaul.

master
Ben Burlingham 5 years ago
parent 9b62ec7336
commit 5fbe9b29ff
  1. 7
      README.md
  2. 94
      server/ricochet.js

@ -13,6 +13,9 @@ A victory state can be stored by taking a snapshot of the current stack.
Icons from [https://game-icons.net](https://game-icons.net) Icons from [https://game-icons.net](https://game-icons.net)
## TODO ## TODO
- walls and winstate algorithm
- tutorial - tutorial
- restore name prompt1 - restore name prompt
- home button
- reset robots? on server reset...avoid multiple shadows
- Fix 9 things

@ -134,63 +134,81 @@ Ricochet.prototype.freshRobots = function() {
}; };
Ricochet.prototype.freshWalls = function() { Ricochet.prototype.freshWalls = function() {
// Edge IDs are of the form [i1-j1-i2-j2]. Top left is 0, 0. // Corner count relates quadratically to squares per side.
const numberOfCorners = Math.ceil(Math.pow((this.squaresPerSide / 4), 2));
// Leave here for testing. const corners = [];
// return [ let shortCircuitCounter = 0;
// "1-9-1-10",
// "9-1-10-1",
// "9-19-10-19",
// "19-9-19-10"
// ];
// console.log("Generating walls."); while (corners.length < numberOfCorners && shortCircuitCounter < numberOfCorners * 5) {
const { i, j } = this.randomSquare();
// Squares per side has quadratic relationship with wall/corner requirements. const isTooClose = corners.reduce((acc, v) => {
const numberOfCorners = Math.ceil(Math.pow((this.squaresPerSide / 10), 2)); const delta = Math.abs(v.i - i) + Math.abs(v.j - j);
const numberOfWalls = Math.ceil(Math.pow((this.squaresPerSide / 5), 2));
const edges = []; return acc || (delta < 3);
}, false);
// DO NUMBER OF CORNERS FIRST AFTER TESTING if (isTooClose === false) {
for (let n = 0; n < numberOfWalls; n++) { corners.push({ i, j });
const { i: ri, j: rj } = this.randomSquare(); }
}
const isHorizontal = Math.random() < 0.5; // Edge IDs are of the form [i1-j1-i2-j2]. Top left is 0, 0.
const isBackward = Math.random() < 0.5; const horizontalEdges = corners.map(({ i, j }) => {
const isLeftward = i > 0 && i < this.squaresPerSide && (Math.random() < 0.5);
const i1 = isLeftward ? i - 1 : i;
const i2 = isLeftward ? i : i + 1;
let i1, j1, i2, j2; return { i1, j1: j, i2, j2: j};
});
if (isHorizontal) { const verticalEdges = corners.map(({ i, j }) => {
i1 = isBackward ? ri - 1 : ri; const isUpward = j > 0 && j < this.squaresPerSide && (Math.random() < 0.5);
i2 = isBackward ? ri : ri + 1; const j1 = isUpward ? j - 1 : j;
const j2 = isUpward ? j : j + 1;
j1 = rj; return { i1: i, j1, i2: i, j2 };
j2 = rj; });
} else {
i1 = ri;
i2 = ri;
j1 = isBackward ? rj - 1 : rj; const edges = horizontalEdges.concat(verticalEdges);
j2 = isBackward ? rj : rj + 1;
}
const edge = `${i1}-${j1}-${i2}-${j2}`; return edges.reduce((acc, { i1, j1, i2, j2 }) => {
// Remove walls along all edges
const onLeft = (i1 === 0 && i2 === 0);
const onRight = (i1 === this.squaresPerSide && i2 === this.squaresPerSide);
const onTop = (j1 === 0 && j2 === 0);
const onBottom = (j1 === this.squaresPerSide && j2 === this.squaresPerSide);
if (edges.includes(edge)) { if (onLeft || onRight || onTop || onBottom) {
n--; return acc;
} else {
edges.push(edge);
} }
// Remove some walls randomly
if (Math.random() < 0.3) {
return acc;
} }
return edges; return acc.concat(`${i1}-${j1}-${i2}-${j2}`);
}, []);
}; };
Ricochet.prototype.freshObjective = function() { Ricochet.prototype.freshObjective = function() {
const getRandomWall = () => {
if (!this.walls.length) {
this.randomUnoccupiedSquare();
}
const wall = this.walls[Math.floor((Math.random() * this.walls.length))];
const [i1, j1, i2, j2] = wall.split('-');
return (Math.random() < 0.5) ? { i: i1, j: j1 } : { i: i2, j: j2 };
};
const rand = Math.floor(Math.random() * this.robotIds.length); const rand = Math.floor(Math.random() * this.robotIds.length);
const id = this.robotIds[rand]; const id = this.robotIds[rand];
const { i, j } = this.randomUnoccupiedSquare();
const { i, j } = getRandomWall();
return { return {
i, i,

Loading…
Cancel
Save