death-games
Version:
Un módulo creado principalmente para juegos en bots de discord.js
273 lines (192 loc) • 7.34 kB
JavaScript
class Buscaminas{
constructor(options){
const { EventEmitter } = require('events');
const util = require("util")
util.inherits(Buscaminas, EventEmitter)
let numeroLargo = 7
let numeroAncho = 7 //Alto
let bombas = 10
let numeroBombas = bombas
let charOculto = 'x'
let defaultChars = [0,1,2,3,4,5,6,7,8,"💥"]
let chars;
if(options && options.largo){
if(isNaN(options.largo)) throw TypeError("Debes de especificar el largo del tablero en la opción largo!")
if(options.largo < 1 || options.largo > 15) throw SyntaxError("El largo del tablero debe ser mayor que 0 y menor o igual que 15!")
numeroLargo = +options.largo
}
if(options && options.ancho){
if(isNaN(options.ancho)) throw TypeError("Debes de especificar el ancho del tablero en la opción ancho!")
if(options.ancho < 1 || options.ancho > 9) throw SyntaxError("El ancho del tablero debe ser mayor que 0 y menor o igual que 9!")
numeroAncho = +options.ancho
}
if(options && options.bombas){
if(isNaN(options.bombas)) throw TypeError("Debes de especificar el número de bombas!")
let maxBombas = numeroLargo * numeroAncho
if(options.bombas < 1 || options.bombas > maxBombas) throw SyntaxError("Debes especificar entre 1 o "+maxBombas+" bombas!")
bombas = +options.bombas
numeroBombas = +options.bombas
}
if(options && options.charOculto){
if(typeof options.charOculto != "string") throw TypeError("Debes proporcionar el carácter que será tomado para las casillas no descubiertas!")
charOculto = options.charOculto
}
if(options && options.chars){
if(!Array.isArray(options.chars) || !options.chars.length) throw TypeError("Debes proporcionar un array de máximo 10 elementos con los emojis o símbolos para cada casilla!")
chars = options.chars
}
if(chars) defaultChars = chars.concat(defaultChars.slice(chars.length)).slice(0, 10)
const largo = new Array(numeroLargo)
const ancho = new Array(numeroAncho)
const coordLetra = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"].slice(0, numeroLargo)
const coordNumero = [1,2,3,4,5,6,7,8,9,10].slice(0, numeroAncho)
for(let i = 0; i < numeroLargo; i++){
largo[i] = i
}
for(let i = 0; i < numeroAncho; i++){
ancho[i] = i
}
const bomb = 9
let row = ancho[Math.floor(Math.random() * ancho.length)]
let column = largo[Math.floor(Math.random() * largo.length)]
var buscaminas=new Array(numeroAncho)
for (let i = 0; i < numeroAncho; i++){
buscaminas[i]=new Array(numeroLargo)
}
for (let i = 0; i< numeroAncho; i++){
for (let j = 0; j< numeroLargo; j++){
buscaminas[i][j] = 0
}
}
while (bombas != 0) {
while(buscaminas[row][column]==9){
row = ancho[Math.floor(Math.random() * ancho.length)];
column = largo[Math.floor(Math.random() * largo.length)];
}
bombas = bombas-1;
buscaminas[row][column] = 9;
let iteri = 3
for (let i = 0; i < iteri; i++) {
let iterj = 3;
if (row == 0 && i == 0){
i++;
}
if (row == numeroAncho - 1 && i == 0){
iteri--
}
for (let j = 0; j < iterj; j++) {
if (column == 0 && j == 0){
j++
}
if (column == numeroLargo - 1 && j == 0){
iterj--
}
if (i != 1 || j != 1){
if (buscaminas[row + i - 1][column - 1 + j] != bomb)
buscaminas[row + i - 1][column - 1 + j]++
}
}
}
}
let cerrado = new Array(numeroAncho)
for (let i = 0; i < numeroAncho; i++){
cerrado[i] = new Array(numeroLargo)
}
for (let i = 0; i< numeroAncho; i++){
for (let j = 0; j< numeroLargo; j++){
cerrado[i][j] = charOculto
}
}
let descubiertos = []
this.find = function(coord){
if(!coord) throw SyntaxError("Debes especificar una letra y un número: (c4)")
coord = coord.toLowerCase()
if(coord.length > 3 || coord.length < 2) throw SyntaxError("Debes especificar una letra y un número: (c4)")
let arr = coord.split("")
let row = coordNumero.indexOf(+(arr[1]))
let column = coordLetra.indexOf(arr[0])
if(row == -1 || column == -1) throw SyntaxError("Debes especificar una letra o un número válidos")
let elegido = buscaminas[row][column]
if(elegido == 9){
for(let i = 0; i < numeroAncho; i++){
for(let j = 0; j < numeroLargo; j++){
if(buscaminas[i][j] == 9) cerrado[i][j] = defaultChars[9]
}
}
return false;
}
if(elegido == 0){
let ceros = []
let iteri = 2
for(let i = -1; i < iteri; i++){
let iterj = 2
if(row == 0 && i == -1) i++
if(row == numeroAncho - 1) iteri--
for(let j = -1; j < iterj; j++){
if(column == 0 && j == -1) j++
if(column == numeroLargo - 1) iterj--
if(i == -1 && (j == -1 || j == 1)) continue;
if(i == 1 && (j == -1 || j == 1)) continue;
if (buscaminas[row + i][column + j] != 0 || descubiertos.some(x => x[0] == [row + i] && x[1] == [column + j])) continue;
cerrado[row + i][column + j] = defaultChars[0]
numerosAlRededor.call(this, row + i, column + j)
ceros.push([row + i, column + j])
descubiertos.push([row + i, column + j])
this.game.descubiertos.push(coordLetra[column + j]+coordNumero[row + i])
}
}
if(ceros.length) for(let x of ceros){
if(buscaminas[x[0]][x[1]]) continue;
this.find(coordLetra[x[1]]+coordNumero[x[0]])
}
if(this.game.descubiertos.length == this.game.tablero.casillas - this.game.bombas){
this.emit("win", this.game)
this.winned = true
return true;
}
return true;
}
this.game.descubiertos.push(coordLetra[column]+coordNumero[row])
cerrado[row][column] = defaultChars[elegido]
if(this.game.descubiertos.length == this.game.tablero.casillas - this.game.bombas){
this.emit("win", this.game)
this.winned = true
return true;
}
return true;
}
this.on = this.on
function numerosAlRededor(row, column){ //Esto iteraría por cada 0, filtrando los que no sean ceros
let pos = buscaminas[row][column]
if(pos != 0) return;
let iteri = 2;
for(let i = -1; i < iteri; i++){
let iterj = 2;
if(row == 0 && i == -1) i++
if(row == numeroAncho - 1 && i == -1) iteri--
for(let j = -1; j < iterj; j++){
if(column == 0 && j == -1) j++
if(column == numeroLargo - 1 && j == -1) iterj--
/*if(i == -1 && (j == -1 || j == 1)) continue;
if(i == 1 && (j == -1 || j == 1)) continue;*/
if(buscaminas[row + i][column + j] == 0 || cerrado[row + i][column + j] != charOculto) continue;
cerrado[row + i][column + j] = defaultChars[buscaminas[row + i][column + j]]
this.game.descubiertos.push(coordLetra[column + j]+coordNumero[row + i])
}
}
}
this.game = {
tablero: {
largo: numeroLargo,
ancho: numeroAncho,
casillas: numeroLargo * numeroAncho
},
bombas: numeroBombas,
open: buscaminas,
ascii: cerrado,
descubiertos: [],
winned: false
}
}
}
module.exports = Buscaminas