Skip to content

Instantly share code, notes, and snippets.

@ElectricCoffee
Last active December 20, 2022 12:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ElectricCoffee/45ce37d179fcb62142418a51bfdfca5c to your computer and use it in GitHub Desktop.
Save ElectricCoffee/45ce37d179fcb62142418a51bfdfca5c to your computer and use it in GitHub Desktop.
A layout generator for Fiesta Caldera. Simulates dice rolls to see if setting up with dice is viable. (it is).
const MIN_X = 0;
const MAX_X = 4;
const MIN_Y = 0;
const MAX_Y = 4;
const MAX_VAL = 8;
const mat = Array.from(Array(5), () => Array(5).fill(MAX_VAL));
const supply = Array(8).fill(3);
// rolls values between 0 and 7.
// note that 8 isn't included on purpose
const rollD8 = () => Math.floor(Math.random() * MAX_VAL);
// checks if there are no values left in the supply
const noneLeft = (x) => supply[x] <= 0;
const matchesLeft = (x, y, colour) => mat[y][x - 1] === colour;
const matchesRight = (x, y, colour) => mat[y][x + 1] === colour;
const matchesAbove = (x, y, colour) => mat[y - 1][x] === colour;
const matchesBelow = (x, y, colour) => mat[y + 1][x] === colour;
function matchesAdjacent(x, y, colour) {
// number conversion required because indices are normally strings
y = Number(y);
x = Number(x);
let checked = false;
if (x > MIN_X) {
checked |= matchesLeft(x, y, colour);
}
if (x < MAX_X) {
checked |= matchesRight(x, y, colour);
}
if (y > MIN_Y) {
checked |= matchesAbove(x, y, colour);
}
if (y < MAX_Y) {
checked |= matchesBelow(x, y, colour);
}
return checked;
}
for (const y in mat) {
for (const x in mat[y]) {
// leave centre empty
if (y == 2 && x == 2) continue;
let i = 0;
let colour = rollD8();
while ((matchesAdjacent(x, y, colour) || noneLeft(colour)) && i <= 1000) {
colour = (colour + 1) % MAX_VAL;
i++;
}
// if it's tried 1000 times, something's probably wrong...
// due to the random nature of randomness, we can't be sure if we just got 1000 bad rolls in a row or not
// that said, the likelihood is pretty small, so we just assume something went to shit here and take over here.
// this being said, this course correction is fully naiive, and doesn't check anything else surrounding it,
// so it could very well happen that the correction itself would require correction, which the program doesn't consider.
if (i >= 1000) {
console.log("course correcting", x, y);
// find the colour left over in the supply
colour = supply.map((x, i) => [i, x]).find((x) => x[1] > 0)[0];
// if both the one above and the one to the left clashes, swap with the top left corner.
if (matchesAbove(x, y, colour) && matchesLeft(x, y, colour)) {
console.log("swapping top left");
mat[y][x] = mat[0][0];
mat[0][0] = colour;
// if it matches above, swap with the left
} else if (matchesAbove(x, y, colour)) {
console.log("swapping left");
mat[y][x] = mat[y][x - 1];
mat[y][x - 1] = colour;
// if it matches left, swap with above
} else if (matchesLeft(x, y, colour)) {
console.log("swapping above");
mat[y][x] = mat[y - 1][x];
mat[y - 1][x] = colour;
}
} else {
mat[y][x] = colour;
}
supply[colour]--;
}
}
// let's make the print pretty
const colours = [
"R", // 0: red
"O", // 1: orange
"Y", // 2: yellow
"G", // 3: green
"C", // 4: cyan
"B", // 5: blue
"P", // 6: purple
"T", // 7: transparent,
"_", // 8: empty
];
console.log(mat.map((y) => y.map((x) => colours[x])));
// console.log(mat.map((y) => y.map((x) => colours[x]).join("")).join("\n"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment