Сортировка покерной комбинации в объекте с помощью Javascript

#javascript #arrays #sorting #object #poker

Вопрос:

С помощью ввода sortHand('texas-holdem QhJhThAsTs 3h5h AhKh TcTd 9h8h JcJs AdKd') я генерирую все возможные комбинации из 5 карт из настольных карт и карт каждого игрока.

Затем я обнаруживаю самую высокую комбинацию и помещаю в объект:массив. Например, если флеш-рояль будет обнаружен, итерация должна быть остановлена и начата снова для другого игрока.

Есть мой код, но он не работает должным образом. Я буду признателен вам за ваши усилия.

 const order = "23456789TJQKA"
let result = {
    'royalFlush' : [],
    'straightFlush' : [],
    'fourKind' : [],
    'fullHouse' : [],
    'flush' : [],
    'straight' : [],
    'threeKind' : [],
    'twoPair' : [],
    'twoKind' : [],
    'highCard' : []
}
//Lets learn poker rules
// Card == table   player
function royalFlush (card) {
    return (card.includes('Ah', 'Kh') || card.includes('Ac', 'Kc') || card.includes('As', 'Ks') || card.includes('Ad', 'Kd')) amp;amp; straight(card) amp;amp; flush(card) ? true : false
}
function straightFlush (card) {
    return straight(card) amp;amp; flush(card) ? true : false
}
function fourKind (ranks) {
    const faces = ranks.map(a => String.fromCharCode([77 - order.indexOf(a[0])])).sort()
    const counts = faces.reduce(count, {})
    const duplicates = Object.values(counts).reduce(count, {})
    return duplicates[4] ? true : false
}
function fullHouse (ranks) {
    const faces = ranks.map(a => String.fromCharCode([77 - order.indexOf(a[0])])).sort()
    const counts = faces.reduce(count, {})
    const duplicates = Object.values(counts).reduce(count, {})
    return duplicates[3] amp;amp; duplicates[2] ? true : false
}
function flush(suits) {
    const toSuit1 = (card) => card.charAt(card.length - 1); //only color
    const sortSuits = (hand) => hand.map(toSuit1).sort();
    suits = sortSuits(suits); 
    return suits[0] === suits[4] ? true : false
}
function straight(ranks) {
    const faces = ranks.map(a => String.fromCharCode([77 - order.indexOf(a[0])])).sort()
    const first = faces[0].charCodeAt(0)
    const lowStraight = faces.join("") === "AJKLM"
    faces[0] = lowStraight ? "N" : faces[0]
    return lowStraight || faces.every((f, index) => f.charCodeAt(0) - first === index) ? true : false
}
function threeKind (ranks) {
    const faces = ranks.map(a => String.fromCharCode([77 - order.indexOf(a[0])])).sort()
    const counts = faces.reduce(count, {})
    const duplicates = Object.values(counts).reduce(count, {})
    return duplicates[3] ? true : false
}
function twoPair (ranks) {
    const faces = ranks.map(a => String.fromCharCode([77 - order.indexOf(a[0])])).sort()
    const counts = faces.reduce(count, {})
    const duplicates = Object.values(counts).reduce(count, {})
    return duplicates[2] > 1 ? true : false
}
function twoKind (ranks) {
    const faces = ranks.map(a => String.fromCharCode([77 - order.indexOf(a[0])])).sort()
    const counts = faces.reduce(count, {})
    const duplicates = Object.values(counts).reduce(count, {})
    return duplicates[2] ? true : false
}
let highCard
function count(c, a) {
    c[a] = (c[a] || 0)   1
    return c
}
function variants(table, player) {
    let tableArr = table.split(/(?=(?:..)*$)/)
    let playerArr = player.split(/(?=(?:..)*$)/)
    playerArr.forEach((item) => {
        for(let i = 0; i < tableArr.length; i  ) {                                              
            const tempArray = [...tableArr]
            tempArray[i] = item
            if (royalFlush(tempArray)) {
                result.royalFlush.push(player)
                return
            }
            if (straightFlush(tempArray)) {
                result.straightFlush.push(player)
                return
            }
            if (fourKind(tempArray)) {
                result.fourKind.push(player)
                return
            }
            if (fullHouse(tempArray)) {
                result.fullHouse.push(player)
                return
            }
            if(flush(tempArray)) {
                result.flush.push(player)
                return
            }
            if(straight(tempArray)) {
                result.straight.push(player)
                return
            }
            if(threeKind(tempArray)) {
                result.threeKind.push(player)
                return
            }
            if(twoPair(tempArray)) {
                result.twoPair.push(player)
                return
            }   
            if(twoKind(tempArray)) {
                result.twoKind.push(player)
                return
            }               
        }                                                                                     
    });    
    for(let i = 1; i <= 10; i  ) {
        const tempArray = [...tableArr]
        if(i<=4) {
            tempArray.splice(0, 1, playerArr[0])
            tempArray.splice(0 i, 1, playerArr[1])
        } else if(i<=7) {
            tempArray.splice(1, 1, playerArr[0])
            tempArray.splice(-3 i, 1, playerArr[1])
        } else if(i<=9) {
            tempArray.splice(2, 1, playerArr[0])
            tempArray.splice(-5 i, 1, playerArr[1])
        } else {
            tempArray.splice(3, 1, playerArr[0])
            tempArray.splice(4, 1, playerArr[1])
        }
        if (royalFlush(tempArray)) {
            result.royalFlush.push(player)
            return
        }
        if (straightFlush(tempArray)) {
            result.straightFlush.push(player)
            return
        }
        if (fourKind(tempArray)) {
            result.fourKind.push(player)
            return
        }
        if (fullHouse(tempArray)) {
            result.fullHouse.push(player)
            return
        }
        if(flush(tempArray)) {
            result.flush.push(player)
            return
        }
        if(straight(tempArray)) {
            result.straight.push(player)
            return
        }
        if(threeKind(tempArray)) {
            result.threeKind.push(player)
            return
        }
        if(twoPair(tempArray)) {
            result.twoPair.push(player)
            return
        }   
        if(twoKind(tempArray)) {
            result.twoKind.push(player)
            return
        }      
    }
}
function sortHand(input) {
    let handAnal = input.split(' ')
    handAnal.forEach((e, index) => {
        if(index == 0 || index == 1) {
            return
        } else {
            variants(handAnal[1], handAnal[index])
        }
    })
}
sortHand('texas-holdem QhJhThAsTs 3h5h AhKh TcTd 9h8h JcJs AdKd')
console.log(result)