在 javascript 中的多个数组之间传输项目

transferring items among several arrays in javascript

我遇到了一个非常复杂的问题,我尝试了所有方法,但它无法正常工作。所以概念是(我只是复制了其中有趣的部分,否则会多几百行):

程序是一个纸牌游戏,24张纸牌(4种不同颜色,总有一个更强,称为ADU)随机分配给4个玩家(4个阵列)。 table 放置卡片的位置由 "asztal" 数组表示。首先人类玩家放一张牌,然后计算机应该按这个顺序到达:

  1. 如果它们具有相同的颜色和更高的价值 - 选择那张牌
  2. 如果它们有相同的颜色和任何值 - 选择那张牌
  3. 如果没有匹配的颜色,则使用特殊颜色集中的任何汽车(为简单起见,它是循环在数组中找到的第一张卡片)
  4. 如果他们没有匹配的颜色,也没有来自特殊颜色集的卡片,而不是数组的第一个元素 (player[0])。

如果你 运行 我的代码,你会看到它不是从每个数组中抓取 1/1/1 卡,但有时更多。而那些卡片确实消失了,并且没有进入 asztal 阵列。我的代码:(https://jsfiddle.net/daxa3pL2/)

function CardA(name,value,adu){
    this.name = name;
    this.value = value;
};
function CardB(name,value,adu){
    this.name = name;
    this.value = value;
};
function CardC(name,value,adu){
    this.name = name;
    this.value = value;
};
function CardD(name,value,adu){
    this.name = name;
    this.value = value;
};
CardA.prototype.adu = false;
CardB.prototype.adu = false;
CardC.prototype.adu = false;
CardD.prototype.adu = false;
var a9 = new CardA("Tök kilenc",0);
var a10 = new CardA("Tök tíz",10);
var aal = new CardA("Tök alsó",2);
var afel = new CardA("Tök felső",3);
var akir = new CardA("Tök király",4);
var aasz = new CardA("Tök ász",11);
var b9 = new CardB("Levél kilenc",0);
var b10 = new CardB("Levél tíz",10);
var bal = new CardB("Levél alsó",2);
var bfel = new CardB("Levél felső",3);
var bkir = new CardB("Levél király",4);
var basz = new CardB("Levél ász",11);
var c9 = new CardC("Makk kilenc",0);
var c10 = new CardC("Makk tíz",10);
var cal = new CardC("Makk alsó",2);
var cfel = new CardC("Makk felső",3);
var ckir = new CardC("Makk király",4);
var casz = new CardC("Makk ász",11);
var d9 = new CardD("Szív kilenc",0);
var d10 = new CardD("Szív tíz",10);
var dal = new CardD("Szív alsó",2);
var dfel = new CardD("Szív felső",3);
var dkir = new CardD("Szív király",4);
var dasz = new CardD("Szív ász",11);
CardC.prototype.adu = true;
var player1 = [c9,b9,b10,d9,a9,d10];
var player2 = [a10,aal,dal,c10,cal,bal];
var player3 = [bfel,bkir,basz,dfel,dkir,dasz];
var player4 = [afel,akir,aasz,cfel,ckir,casz];
var asztal = [];
asztal.push(player1.splice(0,1)[0]);
var player2card1 = function() {
for (i = 0; i < player2.length; i++) {
    if (Object.getPrototypeOf(player2[i]) == Object.getPrototypeOf(asztal[0]) && player2[i].value > asztal[0].value) {
        asztal.push(player2.splice(i,i+1)[0])
        return
    }
}
if (asztal.length == 1) {
for (i = 0; i < player2.length; i++) {
    if (Object.getPrototypeOf(player2[i]) == Object.getPrototypeOf(asztal[0])) {
        asztal.push(player2.splice(i,i+1)[0])
        return
    }
}
}
if (asztal.length == 1){
    for (i = 0; i < player2.length; i++) {
    if (player2[i].adu == true)  {
        asztal.push(player2.splice(i,i+1)[0])
        return
    }
}
}
if (asztal.length == 1) {
    asztal.push(player2.splice(0,1)[0])
    return
}
        };
var player3card1 = function() {
for (i = 0; i < player3.length; i++) {
    if (Object.getPrototypeOf(player3[i]) == Object.getPrototypeOf(asztal[0]) && player3[i].value > asztal[0].value) {
        asztal.push(player3.splice(i,i+1)[0])
        return
    }
}
if (asztal.length == 2) {
for (i = 0; i < player3.length; i++) {
    if (Object.getPrototypeOf(player3[i]) == Object.getPrototypeOf(asztal[0])) {
        asztal.push(player3.splice(i,i+1)[0])
        return
    }
}
}
if (asztal.length == 2){
    for (i = 0; i < player3.length; i++) {
    if (player3[i].adu == true)  {
        asztal.push(player3.splice(i,i+1)[0])
        return
    }
}
}
if (asztal.length == 2) {
    asztal.push(player3.splice(0,1)[0])
    return
}
        };
var player4card1 = function() {
for (i = 0; i < player4.length; i++) {
    if (Object.getPrototypeOf(player4[i]) == Object.getPrototypeOf(asztal[0]) && player4[i].value > asztal[0].value) {
        asztal.push(player4.splice(i,i+1)[0])
        return
    }
}
if (asztal.length == 3) {
for (i = 0; i < player4.length; i++) {
    if (Object.getPrototypeOf(player4[i]) == Object.getPrototypeOf(asztal[0])) {
        asztal.push(player4.splice(i,i+1)[0])
        return
    }
}
}
if (asztal.length == 3){
    for (i = 0; i < player4.length; i++) {
    if (player4[i].adu == true)  {
        asztal.push(player4.splice(i,i+1)[0])
        return
    }
}
}
if (asztal.length == 3) {
    asztal.push(player4.splice(0,1)[0])
    return
}
        };
player2card1();
player3card1();
player4card1();
console.log(player1);
console.log(player2);
console.log(player3);
console.log(player4);
console.log(asztal);

所以我调试了你的代码,发现了两个基本错误:

1:在循环中使用拼接时,索引会发生变化。所以,当你这样做时

 asztal.push(player2.splice(i,i+1)[0])

放在一个循环中,只要你拼接,符合你条件的player2的索引就会改变。所以你的循环的下一次迭代将给出不正确的结果/错过应该删除的对象的索引。

一个可能的解决方案是,在您的 for 循环中不进行拼接,而是将元素插入 asztal,并且不要拼接父元素。然后在循环外,使用 FILTER 函数从玩家中拼接它们,如下所示:

 var player2card1 = function() {
 for (i = 0; i < player2.length; i++) {
    if (Object.getPrototypeOf(player2[i]) == Object.getPrototypeOf(asztal[0]) && player2[i].value > asztal[0].value) {
       asztal.push({name: player2[i].name, value: player2[i].value, prototype: player2[i].prototype});
       player2[i].name = "delete";
       return
   }
}
player2.filter((each)=>{return each.name!== "delete"});

2: 第二个错误(我认为这不是这里的问题但仍然会引起麻烦)是你使用了“==”。在 Javascript 中,尽量使用“===”,因为它还会检查类型和相等性。

稍微重构一下就可以清楚地说明这一点。

// define a card type class
function CardType(type, adu)
{
 // This simply says that if ADU is undefined (not passed) then
  // ADU should be set to false by default
  this.adu = (typeof adu === 'undefined' ? false : adu);
  this.type = type;
}

function Card(name, value, type)
{
  this.name = name;
  this.value = value;
  this.type = type;
}

// Define our card types
var CardA = new CardType("A");
var CardB = new CardType("B");
var CardC = new CardType("C", true);// set to be ADU
var CardD = new CardType("D");

// Define our cards
var a9 = new Card("Tök kilenc",0, CardA);
var a10 = new Card("Tök tíz",10, CardA);
var aal = new Card("Tök alsó",2, CardA);
var afel = new Card("Tök felső",3, CardA);
var akir = new Card("Tök király",4, CardA);
var aasz = new Card("Tök ász",11, CardA);
var b9 = new Card("Levél kilenc",0, CardB);
var b10 = new Card("Levél tíz",10, CardB);
var bal = new Card("Levél alsó",2, CardB);
var bfel = new Card("Levél felső",3, CardB);
var bkir = new Card("Levél király",4, CardB);
var basz = new Card("Levél ász",11, CardB);
var c9 = new Card("Makk kilenc",0, CardC);
var c10 = new Card("Makk tíz",10, CardC);
var cal = new Card("Makk alsó",2, CardC);
var cfel = new Card("Makk felső",3, CardC);
var ckir = new Card("Makk király",4, CardC);
var casz = new Card("Makk ász",11, CardC);
var d9 = new Card("Szív kilenc",0, CardD);
var d10 = new Card("Szív tíz",10, CardD);
var dal = new Card("Szív alsó",2, CardD);
var dfel = new Card("Szív felső",3, CardD);
var dkir = new Card("Szív király",4, CardD);
var dasz = new Card("Szív ász",11, CardD);

var player1 = [c9,b9,b10,d9,a9,d10];
var player2 = [a10,aal,dal,c10,cal,bal];
var player3 = [bfel,bkir,basz,dfel,dkir,dasz];
var player4 = [afel,akir,aasz,cfel,ckir,casz];
var asztal = [];

// It doesn't really make sense to splice the array because
// you are changing the array. 
// asztal.push(player1.splice(0,1)[0]);
// This line can be replaced with a simple:
asztal.push(player1[0]);

// This function has lots of redundant code and we can simplify it greatly
// as well as generalize it to work for each player

function getNextCard(player, card){
 // By default we take the first card unless we find a better one along the way.
  var matchCase2 = null, // same type, any value
      matchCase3 = null, // special set
      matchCase4 = player[0]; // any card
  for(i = 0; i < player.length; i++)
  {
   // Check our first case
   if(player[i].type.type == card.type.type && 
        player[i].value > card.value){
     return player[i];
    }
    
    if(matchCase2 === null && player[i].type.type == card.type.type){
     matchCase2 = player[i];
    }
    
    if(matchCase3 === null && player[i].type.adu === true){
     matchCase3 = player[i];
    }
  }
  if(matchCase2 !== null) return matchCase2;
  if(matchCase3 !== null) return matchCase3;
  return matchCase4;
}

console.log(getNextCard(player2, asztal[0]));
console.log(getNextCard(player3, asztal[0]));
console.log(getNextCard(player4, asztal[0]));