假设我们有两个数组,如下所示
let result = []
let array1 = ['2h', '3h', '4h', '5d', '8d']
let array2 = ['Kd', 'Ks']我需要从array2中获取每个元素,并将每个元素逐个替换到数组中,然后存储到结果中
结果应该是这样的
result = ['Kd', '3h', '4h', '5d', '8d']
['2h', 'Kd', '4h', '5d', '8d']
['2h', '3h', 'Kd', '5d', '8d']
.....
['Ks', '3h', '4h', '5d', '8d']
['2h', 'Ks', '4h', '5d', '8d']
['2h', '3h', 'Ks', '5d', '8d']
.....同时也取代了array1中的整个array2
result = ['Kd', 'Ks', '4h', '5d', '8d']
['2h', 'Kd', 'Ks', '5d', '8d']
.....
['Kd', 'Ks', '4h', '5d', '8d']
['Kd', '3h', 'Ks', '5d', '8d']
['Kd', '3h', '4h', 'Ks', '8d']
.....这是我当前的代码,需要分析每个玩家的扑克手,然后按实力排序
let ranks = ['2','3','4','5','6','7','8','9','T','J','Q','K','A']
let suits = ['h','d','c','s']
let gameTypes = ['texas-holdem','omaha-holdem','five-card-draw']
//Lets learn poker rules
// Card == table + player
let royalFlush
let straightFlush
let fourKind
let fullHouse
let royal
let straight
let threeKind
let twoPair
let twoKind
let highCard
var combos = []
function variants(table, player) {
let result = []
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;
result.push(tempArray);
}
});
return result
}
function sortHand(input) {
let handAnal = input.split(' ')
handAnal.forEach((e, index) => {
if(index == 0 || index == 1) {
return
} else {
combos.push(variants(handAnal[1], handAnal[index]))
}
})
}
//console.log(sortHand('texas-holdem 4cKs4h8s7s Ad4s Ac4d As9s KhKd 5d6d'))
sortHand('texas-holdem 2h3h4h5d8d 9hJh')
console.log(combos);
//console.log(sortHand('omaha-holdem 3d3s4d6hJc Js2dKd8c KsAsTcTs Jh2h3c9c Qc8dAd6c 7dQsAc5d'))
//console.log(sortHand('five-card-draw 7h4s4h8c9h Tc5h6dAc5c Kd9sAs3cQs Ah9d6s2cKh 4c8h2h6c9c'))发布于 2021-08-09 16:34:37
这是我的解决方案。
let result = [];
let array1 = ["2h", "3h", "4h", "5d", "8d"];
let array2 = ["Kd", "Ks"];
array2.forEach((item) => { for(let i = 0; i < array1.length; i++) {
const tempArray = [...array1];
tempArray[i] = item;
result.push(tempArray);
}
});
console.log(result);
在这里,我们遍历array2并获取每一项。对于array2的每一项,我们迭代第一个数组,复制它,并将其存储在tempArray中。之后,我们将第一个数组的当前项放入for循环中的索引i处,并将其放入tempArray。
发布于 2021-08-10 13:00:07
手动排序,没有任何数学或巧妙的解决方案:(
function variants(table, player) {
let result = []
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;
result.push(tempArray);
}
});
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])
}
result.push(tempArray);
}
return result
}https://stackoverflow.com/questions/68715502
复制相似问题