0

First of all, excuse my English. I will try to explain the best that I can.

I want to develop a dominoes console game.

I stored all the dominoes into an array named dominoes.

function createNewDominoes(dominoes) {
  newDomino = {
    a: Math.floor((Math.random() * 7) - 0),
    b: Math.floor((Math.random() * 7) - 0),
    tipo: null
  }

  newDominoFlipped = {
    a: nuevaFicha.b,
    b: nuevaFicha.a,
    tipo: null
  }
  console.log(nuevaFicha, nuevaFichaVolteada)

  if ((includes(dominoes, newDomino)) || (includes(dominoes, newDominoFlipped))) {
    console.log("Element already exist")
  } else {
    array.push(nuevaFicha)
  }
}

I am used Node.js, and I included array-includes modules into the app.js file, that allows me to check the element of my array.

The thing is that when I get the array, the function return 28 elements, that's okay, but some are repeated. Example:

  [ { a: 3, b: 0, tipo: null },
  { a: 5, b: 2, tipo: null },
  { a: 1, b: 4, tipo: null },
  { a: 1, b: 6, tipo: null },
  { a: 2, b: 4, tipo: null },
  { a: 4, b: 2, tipo: null },
  { a: 4, b: 2, tipo: null },
  { a: 3, b: 3, tipo: null },
  { a: 5, b: 5, tipo: null },
  { a: 3, b: 4, tipo: null },
  { a: 0, b: 6, tipo: null },
  { a: 6, b: 0, tipo: null },
  { a: 4, b: 4, tipo: null },
  { a: 6, b: 3, tipo: null },
  { a: 5, b: 2, tipo: null },
  { a: 3, b: 4, tipo: null },
  { a: 3, b: 5, tipo: null },
  { a: 2, b: 3, tipo: null },
  { a: 3, b: 4, tipo: null },
  { a: 3, b: 3, tipo: null },
  { a: 5, b: 3, tipo: null },
  { a: 0, b: 1, tipo: null },
  { a: 2, b: 2, tipo: null },
  { a: 0, b: 3, tipo: null },
  { a: 2, b: 3, tipo: null },
  { a: 4, b: 4, tipo: null },
  { a: 0, b: 4, tipo: null },
  { a: 5, b: 1, tipo: null } ]

I need to get all the 28 dominoes, but they can't be repeated or flipped-repeated.

Hope you can help me

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122

2 Answers2

0

Write your own function to see if the dominos are equal. Then use the Array.some method to see if any dominos meet that condition:

const arr = [{ a: 3, b: 0, tipo: null }]

const same = {a: 3, b: 0, tipo: null};
const reverse = {a: 0, b: 3, tipo: null};
const diff = {a: 4, b: 0, tipo: null};

function dominoEqual(d1, d2) {
    if (d1.tipo !== d2.tipo) return false;
    return (d1.a === d2.a && d1.b === d2.b) || (d1.a === d2.b && d1.b === d2.a);
}

console.log(arr.some(d => dominoEqual(d, same))) // repeated
console.log(arr.some(d => dominoEqual(d, reverse))) // repeated but reversed
console.log(arr.some(d => dominoEqual(d, diff))) // not repeated

Then use that instead of includes to check for duplicates:

if (dominos.some(d => dominoEqual(d, newDomino))) {
    console.log("Element already exist")
} else {
    array.push(nuevaFicha)
}
CRice
  • 29,968
  • 4
  • 57
  • 70
0

You will have to create custom function to check for duplicates. Each time you are creating new object, which will never satisfy to Array.indexOf() function. You can write a function like this:

function containsItemOrFlippedItem(list, item) {
    return list.some(function(current) {
        return (current.a === item.a && current.b === item.b)
            || (current.a === item.b && current.b === item.a);
    });
}

function createNewDominoes(dominoes) {
    var newDomino = {
        a: Math.floor((Math.random() * 7) - 0),
        b: Math.floor((Math.random() * 7) - 0),
        tipo: null
    };
    if (containsItemOrFlippedItem(dominoes, newDomino)) {
        console.log("Element already exist");
    } else {
        dominoes.push(newDomino);
    }
}

Here I have used the some() function of Array. Have a look on the documentation of some() at MDN. I hope this helps you!!

Kamal Singh
  • 990
  • 8
  • 13