在数组javascript中查找元素

uplii1fm  于 2023-01-29  发布在  Java
关注(0)|答案(2)|浏览(171)

我想写一个名为getWinningLines(screen,payLines)的javascript函数,它以参数2数组的形式获取,并在存在3个或更多匹配时返回$screen的索引和payLines之间的匹配行。
注:
screen是1到13之间的整数数组的数组(索引1和2就像Joker,匹配任何其他索引)payLines是单元格数组的数组
示例:
适用于:

const screen = [
    [9, 8, 1],
    [7, 5, 7],
    [6, 8, 7],
    [3, 9, 4],
    [6, 4, 3]
]

const payLines = [
    [[0, 2], [1, 2], [2, 2], [3, 2], [4, 2]],
    [[0, 2], [1, 0], [2, 2], [3, 0], [4, 2]],
]

解决方案getWinningLines(屏幕,支付行)应返回:

{
"0": {
    "win": "0.05",
    "positions": [
        [
            0,
            2
        ],
        [
            1,
            2
        ],
        [
            2,
            2
        ]
    ],
    "matches": 3
},
"1": {
    "win": "0.05",
    "positions": [
        [
            0,
            2
        ],
        [
            1,
            0
        ],
        [
            2,
            2
        ]
    ],
    "matches": 3
}
}

为什么?我们得到第一个索引"1",因为payLines中的第一行(payLines [0])包含:[[0,2],[1,2],[2,2],[3,2],[4,2]],在屏幕内部我们有:屏幕[0][2]= 1(小丑)&屏幕[1][2]= 7 &屏幕[2][2]= 7 &屏幕[3][2]= 4(错误)
因此,我们有匹配= 3,因为第四个是不同的,和立场= payLines [0]
我们得到第二索引"2",因为payLines中的第二行(即payLines [1])包含:[[0,2],[1,0],[2,2],[3,0],[4,2]],在屏幕内部我们有:$屏幕[0][2]= 1(小丑)&屏幕[1][0]= 7 &屏幕[2][2]= 7 &屏幕[3][0]= 3(错误)
所以我们有匹配= 3,因为第四个是不同的,和立场= payLines [1]
我是这样开始的:

for (i=0; i<=(payLines.length)-1 ;i++){
    let matches = 1
   
    let line = []
    line = line.concat(payLines[18])
    // console.log(line)
    for (h=1; h<=(line.length)-1;h++){
         const n = screenn[line[h][0]][line[h][1]]
         for (j=h+1; j<=(line.length)-1;j++){
             if(n === screenn[line[j][0]][line[j][1]]){
                 matches= matches + 1
                
             }   

         }
        
    }console.log(matches)
p3rjfoxz

p3rjfoxz1#

好的,那么所有匹配项应该是1或2或者彼此相等?那么让我们试试这个:

const screen = [
    [9, 8, 1],
    [7, 5, 7],
    [6, 8, 7],
    [3, 9, 4],
    [6, 4, 3]
]

const payLines = [
    [[0, 2], [1, 2], [2, 2], [3, 2], [4, 2]],
    [[0, 2], [1, 0], [2, 2], [3, 0], [4, 2]],
]

function countMatches(screen, payLine) {
    let value = 0,
        matches = 0

    for (let [x, y] of payLine) {
        let s = screen[x][y]

        if (s === 1 || s === 2) {
            matches++
        } else if (!value) {
            matches++
            value = s
        } else if (s === value) {
            matches++
        } else {
            break
        }
    }

    return matches
}

console.log(countMatches(screen, payLines[0]))
owfi6suc

owfi6suc2#

function getWinningLines(screen, payLines) {
     let winningLines = [];
     payLines.forEach((line, index) => {
         let array = line.map((item) => screen[item[0]][item[1]]);
         let numberOfMatches = array.filter(item => (item === 1 || item === 2) 
         || array.indexOf(item) !== array.lastIndexOf(item)).length;
         if (numberOfMatches >= 3) {
          winningLines.push({
              "line": index + 1,
              "matches": numberOfMatches,
              "Array": array
          });
        
           }
   
         });
          return winningLines;
        }

相关问题