Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么';我的代码只生成2个类型_Javascript_Html_Css_Memory - Fatal编程技术网

Javascript 为什么';我的代码只生成2个类型

Javascript 为什么';我的代码只生成2个类型,javascript,html,css,memory,Javascript,Html,Css,Memory,所以我用香草JS做了一个记忆游戏。游戏应该只生成一种卡(口袋妖怪)的两个实例。我的游戏有4种不同类型的牌。为了生成一个类型的2个实例,我对每个类型使用不同的实例计数器,并使用一些if语句。出于某种原因,当我运行代码时,它会生成随机数量的类型。我怎样才能解决这个问题 如果你第三次得到一张卡,你只需再次生成一个ranodm号码,当然这可能是第四次的类型,一种类型有3张卡 您可以填充一个数组,如[0,0,1,1,2,2,3,3,4,4,5,5,…],对array.length进行随机抽样,在索引处获

所以我用香草JS做了一个记忆游戏。游戏应该只生成一种卡(口袋妖怪)的两个实例。我的游戏有4种不同类型的牌。为了生成一个类型的2个实例,我对每个类型使用不同的实例计数器,并使用一些if语句。出于某种原因,当我运行代码时,它会生成随机数量的类型。我怎样才能解决这个问题


如果你第三次得到一张卡,你只需再次生成一个ranodm号码,当然这可能是第四次的类型,一种类型有3张卡

您可以填充一个数组,如[0,0,1,1,2,2,3,3,4,4,5,5,…],对array.length进行随机抽样,在索引处获取值,并将其从数组中移除,直到数组为空

var cards = new Array(20);

for(var i=0;i<cards.length/2;i++){
    cards[i*2] = i;
    cards[i*2 +1] = i;
}

console.log(cards);

while (cards.length > 0) {
    var index = Math.floor(Math.random() * cards.length);
    console.log(cards[index]);
    cards.splice(index, 1);

}
var卡=新阵列(20);
对于(变量i=0;i 0){
var index=Math.floor(Math.random()*cards.length);
控制台日志(卡片[索引]);
卡片.拼接(索引1);
}

问题在于,在每个if子句之后都会生成一个随机数, 这样做时,脚本不会进一步检查

//...
this.type = Math.floor(Math.random() * 4);
  if(this.type == 0){
     if(t0C < 2){
        t0C++;
     } else if(t0C == 2){
        this.type == Math.floor(Math.random() * 4);
        //what happens if this.type == 0?
     }
  } 

  if(this.type == 1){
  //...
将其他函数排除在Card类之外,可以使if子句更具可读性和有效性:

var cards = []; //it will be 4 x 2 = 8 cards
var types = 4;
var peekCard = function(max) { //helper function to get a random number from 0 to max
  return Math.floor(Math.random() * max);
}


// helper function 
// https://stackoverflow.com/questions/6274339/how-can-i-shuffle-an-array
/**
 * Shuffles array in place.
 * @param {Array} a items An array containing the items.
 */
function shuffle(a) {
    var j, x, i;
    for (i = a.length - 1; i > 0; i--) {
        j = Math.floor(Math.random() * (i + 1));
        x = a[i];
        a[i] = a[j];
        a[j] = x;
    }
    return a;
}

//peek 4 different cards
while(cards.length < types) {
  var card = peekCard(types);
  if (cards.indexOf(card) === -1) {
    cards.push(card);
  }
}

//duplicate cards:
cards  = cards.concat(cards);
//shuffle cards:
cards = shuffle(cards);
console.log(cards);
var卡=[]//它将是4 x 2=8张卡
var类型=4;
var peekCard=function(max){//helper函数获取从0到max的随机数
返回Math.floor(Math.random()*max);
}
//辅助函数
// https://stackoverflow.com/questions/6274339/how-can-i-shuffle-an-array
/**
*洗牌阵法就位。
*@param{Array}a items包含这些项的数组。
*/
函数洗牌(a){
变量j,x,i;
对于(i=a.length-1;i>0;i--){
j=数学地板(数学随机()*(i+1));
x=a[i];
a[i]=a[j];
a[j]=x;
}
返回a;
}
//看4张不同的卡片
while(卡片长度<类型){
var卡=PEEK卡(类型);
如果(卡片索引)(卡片)=-1){
卡片。推(卡片);
}
}
//复制卡:
卡片=卡片。concat(卡片);
//洗牌:
牌=洗牌(牌);
控制台.日志(卡片);
现在你有了一个数字数组,代表你的一对卡片

var c = [];
   for(var i = 0; i < cards.length; i++) {
    c[i] = new Card("c"+i, cards[i]);
    //...
   }
var c=[];
对于(变量i=0;i
请在此处发布MVCE。您能添加一些示例代码吗?您是正确的,但如果一种类型的卡只有一张,那么代码可能会生成另一张类型的卡,我不明白,代码总是生成偶数数组大小的对(在您的示例中为8),或者您能证明我错了吗?
var c = [];
   for(var i = 0; i < cards.length; i++) {
    c[i] = new Card("c"+i, cards[i]);
    //...
   }