Skip to content

Instantly share code, notes, and snippets.

@Nick-Gabe
Last active October 15, 2022 09:59
Show Gist options
  • Save Nick-Gabe/460df842032cae3ba7b88f4c62a0c465 to your computer and use it in GitHub Desktop.
Save Nick-Gabe/460df842032cae3ba7b88f4c62a0c465 to your computer and use it in GitHub Desktop.
Code used to answer the Rabbit Hole challenge.
// tamanho do tabuleiro ao todo e quantas colunas tem em uma fileira
const boardSize = 500;
const holesPerRow = 50;
// tempo em ms de delay do intervalo do game loop
const loopDelay = 25;
// emojis, só pra deixar bonitinho kkkk
const emojis = {
blank: '🟫',
bunnyTile: '🟫',
playerTile: '🟪',
bunny: '🐇',
player: '🤖',
}
// cria um array com o tamanho do tabuleiro e preenche com emojis de terreno
const holes = new Array(boardSize).fill(emojis.blank);
const initialPositions = {
player: 0,
bunny: Math.floor(Math.random() * boardSize)
}
let bunnyPosition, playerPosition, found, attempts = 1, temp;
// algoritmos que podem ser usados para achar o coelho (WIP)
const algorithms = {
random(target) {
target = Math.floor(Math.random() * boardSize)
return target
},
linear(target, initialPosition) {
if (target === undefined || target === boardSize - 1) {
target = initialPosition;
} else target += 1;
return target
},
alternate(target = 0) {
if (!temp) target += 1
else target += 2
if (target === undefined || target === boardSize - 1) {
target = 0;
}
if(target >= boardSize) temp = !temp
return target
}
}
// função para mostrar o tabuleiro no console formatado com informações
const logHoles = () => {
console.clear();
console.log('\n');
for (let i = 0; i < holes.length; i += holesPerRow) {
let logString = holes.slice(i, i + holesPerRow).join('');
if (i === 0) {
logString += ` Board size: ${holesPerRow}x${boardSize / holesPerRow} (${boardSize})`
} else if (i === holesPerRow) {
logString += ` Attempts: ${attempts}`;
} else if (i === holesPerRow * 2) {
logString += ` Status: ${found ? 'Found!' : 'Not found :('}`;
} else if (i === holesPerRow * 3) {
logString += ` Bunny Position: ${bunnyPosition}`
} else if (i === holesPerRow * 4) {
logString += ` Player Position: ${playerPosition}`
}
console.log(logString);
}
console.log('\n');
}
// faz com que o jogador olhe nos buracos e mostra seu ícone no tabuleiro
// (descomente o algoritmo que for usar / crie o seu)
const playerJump = () => {
holes.splice(playerPosition, 1, emojis.playerTile);
// // LINEAR
// playerPosition = algorithms.linear(playerPosition, initialPositions.player)
// // RANDOM
// playerPosition = algorithms.random()
// // ALTERNATE
playerPosition = algorithms.alternate(playerPosition)
holes.splice(playerPosition, 1, emojis.player);
}
// faz com que o coelho mude de posição e mostra no tabuleiro
const bunnyJump = () => {
holes.splice(bunnyPosition, 1, emojis.bunnyTile);
bunnyPosition = changeBunnyPosition(bunnyPosition);
holes.splice(bunnyPosition, 1, emojis.bunny);
}
// roda em loop enquanto o coelho não for encontrado
const gameLoop = () => {
bunnyJump();
playerJump();
logHoles();
if (playerPosition === bunnyPosition) {
found = true;
logHoles();
clearInterval(interval);
}
}
// lógica pro pulo adjacente do coelho
const changeBunnyPosition = (target) => {
if (target === undefined) target = initialPositions.bunny;
else {
if (target >= boardSize - 1) target -= 1;
else if (target <= 0) target += 1;
else {
const direction = Math.random() > 0.5 ? 1 : -1;
target += direction;
}
}
return target;
}
// intervalo que roda o jogo e aumenta as tentativas
var interval = setInterval(() => {
attempts++
gameLoop();
}, loopDelay)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment