'how make pattern of array based on row column

I am trying to make a new map array also image random (you can say it is like a slot machine):

get arrayCardsID() {
    const cards = Lib.arrShuffle(CARDS.map(card => card.id))
    return Lib.arrShuffle(cards.reduce((acc, cur, i) => acc.concat(new Array(i+1).fill(cur)), []))
}

createCombination() {
    const cardsID = this.arrayCardsID

    //make new map of array with 3 row and 5 column
    return new Array(3).fill(0).map(() => {
        return new Array(5).fill(0).map(() => {
            return cardsID[Lib.randomInteger(0, cardsID.length - 1)]
        })
    })
}

getLines(cmb, row = 0, col = 0) {
    const res = new Array(5).fill(0).map(() => {
        return { id: cmb[row][col], cards: [[ row, col ]] }
    })

    //result 111
    if (cmb[row]?.[col + 1] === cmb[row][col]) res[0].cards.push(...this.getLines(cmb, row, col + 1)[0].cards)
    //result 222
    if (cmb[row + 1]?.[col + 1] === cmb[row][col]) res[1].cards.push(...this.getLines(cmb, row + 1, col + 1)[1].cards)
    //result 333
    if (cmb[row-1]?.[col+1] === cmb[row][col]) res[2].cards.push(...this.getLines(cmb, row-1, col+1)[2].cards)

    return res
}


getWinLines(cmb) {
    const result = []

    cmb.forEach((line, i) => {
        const lines = this.getLines(cmb, i)
        const winLines = lines.filter(line => line.cards.length === 3 || line.cards.length === 4 || line.cards.length === 5)
        result.push(...winLines)
    })

    return result
}

getWinCards(lines) {
    const cards = lines.reduce((acc, curr) => acc.concat(curr.cards), [])
    const setCards = new Set(cards.map(JSON.stringify))
    const uniqCards = Array.from(setCards).map(JSON.parse)

    return uniqCards.map(pos => new Object({ row: pos[0], col: pos[1] }))
}

getWinSum(lines) {
    return this.bet * lines.reduce((acc, curr) => acc + this.getCardPrice(curr.id), 0)
}

Based on above code, and here the example of maps

    0   1   2   3   4
0   j   r   t   f   l
1   a   r   f   f   r
2   s   f   g   g   g

That alphabet is random images, the numeric is position of map (just to make it easy read, so I put numeric).

From above code, the getLines "result 111" is horizontal result when some random images matching from random of row column and starting to count the length to determine win.

Here example result 111:

    0   1   2   3   4
0   j   r   t   f   l
1   *   *   *   *   *
2   *   *   *   g   g

The result is win with length 5: [1,0],[1,1],[1,2],[1,3],[1,4]

The result is win with length 3: [2,0],[2,1],[2,2]

Here example "result 222" with diagonal win:

    0   1   2   3   4
0   *   r   t   f   *   
1   r   *   r   *   r
2   s   f   *   g   g

The result is win with length 3: [0,0],[1,1],[2,2]

Here example "result 333" also with diagonal win:

    0   1   2   3   4
0   j   r   *   f   l
1   r   *   r   *   r
2   *   f   j   g   g

The result is win with length 3: [2,0],[1,1],[0,2]

The problem is:

If see closely the example map, you notice the result 2 and result 3 are a bit strange.

I can't get win from determine length 4 or length 5 if pattern diagonal.

The pattern cmb (on result 222 and 333) is only diagonal and can't make pattern line of V.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source