tresenraya
Version:
Módulo para facilitar la creación del popular juego llamado tres en raya en tus proyectos
34 lines (23 loc) • 2.42 kB
JavaScript
//const selecciones = require('../jsons/selecciones.json').mejorPos;
/*module.exports = (pista, fichas) => {
let pos = 5;
const { x, o } = { 'x': fichas[0], 'o': fichas[1] };
if(selecciones.some(s => !s.ya.some(f => !o.includes(pista[f])) && !fichas.includes(pista[s.seleccion-1])) || selecciones/*.filter(x => !x.ya.some(f => ['❌', '⭕'].includes(pista[f]))).some(s => !s.ya.some(f => !x.includes(pista[f])) && !fichas.includes(pista[s.seleccion-1])) && !['❌', '⭕'].includes(pista[(selecciones.find(s => !s.ya.some(f => !['⭕'].includes(pista[f]))) || selecciones.filter(x => !x.ya.some(f => ['❌', '⭕'].includes(pista[f]))).find(s => !s.ya.some(f => !['❌'].includes(pista[f]))))-1])) {
pos = (selecciones.filter(x => !x.ya.some(f => ['❌', '⭕'].includes(pista[f]))).find(s => !s.ya.some(f => !['⭕'].includes(pista[f])) && !fichas.includes(pista[s.seleccion-1])) || selecciones.filter(x => !x.ya.some(f => ['❌', '⭕'].includes(pista[f]))).find(s => !s.ya.some(f => !['❌'].includes(pista[f])) && !fichas.includes(pista[s.seleccion-1]))).seleccion;
} else {
pos = (pista.map((x, i) => fichas.includes(x) ? null : (i+1)).filter(x => x !== null))[Math.floor(Math.random() * (pista.map((x, i) => fichas.includes(x) ? null : i).filter(x => x !== null)).length)];
}
while(fichas.includes(pista[pos-1])) {
pos = (selecciones.filter(x => !x.ya.some(f => fichas.includes(pista[f]))).some(s => !s.ya.some(f => !o.includes(pista[f]))) || selecciones.filter(x => !x.ya.some(f => fichas.includes(pista[f]))).some(s => !s.ya.some(f => !o.includes(pista[f])))) ? (selecciones.filter(x => !x.ya.some(f => fichas.includes(pista[f]))).find(s => !s.ya.some(f => !o.includes(pista[f]))) || selecciones.filter(x => !x.ya.some(f => fichas.includes(pista[f]))).find(s => !s.ya.some(f => !x.includes(pista[f])))).seleccion : (pista.map((x, i) => fichas.includes(x) ? null : (i+1)).filter(x => x !== null))[Math.floor(Math.random() * (pista.map((x, i) => fichas.includes(x) ? null : i).filter(x => x !== null)).length)];
}
return (pos || 5);
}*/
const pos = require('ai-tic-tac-toe');
module.exports = (pista, fichas, favor) => {
const tablero = pista.map(x => {
if(x == fichas[0]) return 'x';
if(x == fichas[1]) return 'o';
return '';
});
return (pos.getmove(tablero, (favor||'x')) || null);
}