尝试编写一个 JS 骰子游戏,将当前掷骰数组与之前的所有掷骰进行比较,直到达到一定数量的唯一掷骰为止

问题描述 投票:0回答:1

我在解决这个问题时遇到了很多麻烦。我是编码新手,所以可能是一个明显的错误。

目标是:

制作多于 1 个元素的数组或当前滚动 将它与包含以前的 rolls 或 prevs 的数组数组进行比较 如果有任何完全匹配,则再次滚动。 如果没有匹配项,则将 current 推入 prevs,将成功滚动计数加 1,然后继续。 重复此过程,直到唯一卷的数量达到目标数量。 我觉得我卡在比较功能上了。

我首先使用 for 循环遍历 prevs 中的数组,将它们与 current 进行比较如果找到任何匹配项,则返回 0 表示唯一,这会导致主 while 循环不前进,因为计数器 cnt 停留在相同的。但是,如果没有找到匹配项,那么我将 current 推入 prevs,返回 1 for unique,这导致 cnt 增加 1,while 循环前进。

我脑子里的逻辑是当前的 roll 必须只与之前的比较,然后才被推入数组,否则总会有匹配项,while 循环将永远进行下去。

但出于某种原因,当我使用 console.log 进行测试以查看正在比较的数组的内容时,当前滚动和上一个滚动始终相同。我不知道这是怎么回事。

非常感谢您阅读所有这些内容,感谢您提供的任何建议。

这是我的代码:

var cnt=0; //number of successes
var current=[]; //array for the latest roll
var prevs=[[]]; //array of arrays of all previous unique rolls
var unique=1; //whether the latest roll is unique
var breaker; //emergency stopper for while loop
var goal=5;
var cnt=0;

function compare() { //checks the latest roll array against all previous arrays
    for (var i=0;i<prevs.length;i++) { //iterate through previous rolls, comparing each against the current roll, returning 0 for unique if any are a match
        if (compareArrays(current,prevs[i])) {
            return 0; 
        }
    }

    prevs.push(current); //if no matches are found, add the latest roll into the previous rolls
    return 1; //return a unique
}

function compareArrays(a,b) { //turn arrays into strings to compare
  return JSON.stringify(a) === JSON.stringify(b);
};

function dice() { //rolls a random number
  var min = Math.ceil(1);
  var max = Math.floor(10);
  var toss = Math.floor(Math.random() * (max - min)); 
  return toss;
}

function main() { //main function
  while (cnt<goal) {
    breaker++;   
    if (breaker>50) { //this is here in case of emergency 
      break; 
    }
    
    current[0]=dice();     //first roll, two elements added to current roll array
    current.push(dice());
   
    unique=compare();   //run the compare function
    if (unique) { 
       console.log("Rolled unique, continuing.");
    cnt++; //only continue the count if roll was unique
    } else {
        console.log("Rolled duplicate, retrying.");
    }
    
  }
}

main();

javascript arrays multidimensional-array string-comparison
1个回答
0
投票

通过一些更改,您的代码可以很好地工作。

我假设你希望每卷使用两个骰子,你可以在顶部更改这个数字。

主要的变化是为每个卷创建一个新的

current
卷阵列,而不是继续将结果添加到同一个。

var cnt=0; //number of successes
var current=[]; //array for the latest roll
var prevs=[]; //array of arrays of all previous unique rolls
var unique=1; //whether the latest roll is unique
var breaker=0; //emergency stopper for while loop
var goal = 5; // Number of rolls to aim for
var numberOfDicePerRoll = 2; // Number of dice per roll

function compare() { //checks the latest roll array against all previous arrays
    for (var i=0;i<prevs.length;i++) { //iterate through previous rolls, comparing each against the current roll, returning 0 for unique if any are a match
        if (compareArrays(current,prevs[i])) {
            return 0; 
        }
    }

    prevs.push(current); //if no matches are found, add the latest roll into the previous rolls
    return 1; //return a unique
}

function compareArrays(a,b) { //turn arrays into strings to compare
  return JSON.stringify(a) === JSON.stringify(b);
}

function dice() { //rolls a random number
  var min = 1;
  var max = 10;
  var toss = Math.floor(min + Math.random() * (max - min + 1)); 
  return toss;
}

function rollToString(roll) {
  return "[" +roll.join(",") + "]";
}

function main() { //main function
  console.log("Starting rolls: Goal: " + goal)
  while (cnt<goal) {
    breaker++;   
    if (breaker>50) { //this is here in case of emergency 
      break; 
    }

    current = Array.from({ length: numberOfDicePerRoll }, (v,k) => dice());

    unique=compare();   //run the compare function
    if (unique) { 
       console.log("Attempt #" + breaker + " - Rolled unique: " + rollToString(current) + " continuing...");
       cnt++; //only continue the count if roll was unique
    } else {
        console.log("Attempt #" + breaker + " - Rolled duplicate: " + rollToString(current) + " retrying...");
    }

  }
  console.log("Goal reached: rolls:" , prevs.map(rollToString).join(" "))
}

main();
.as-console-wrapper { max-height: 100% !important; }

© www.soinside.com 2019 - 2024. All rights reserved.