我有一个问题随机数生成。我为游戏“猜数字游戏”制作了一个AI,它可以正常工作,直到玩家命中数字低而不是搜索到的数字。在那之后Math.random(); 可能,很可能会产生负数。如果玩家击中的数字低于搜索到的数字。 (由AI引起的数字范围由两个nums定义(nums lower []; 比搜索最高,而 high []; 比搜索中最低)
以下是GitHub上的完整代码,因为它太大而无法放在这里:documentation
我的问题是,如何正确调试应用程序以找到此类问题。我正在使用else if(higher_l > 0 && lower_l > 0)
{ //losuje od 0 (ZERA) do (max-min+1) i dodaje + min;
guessed = Math.floor(Math.random()*((higher[0])-((lower[0]+1)+1))+(lower[0]+1));
if(guessed > drawn)
{
c2--;
document.getElementById("notes2").innerHTML = p2 + " remaining chances: " + c2;
higher.push(guessed);
higher_l = higher.length;
higher.sort(function(a, b) {return a-b;});
resultH = resultH + guessed + " ";
document.getElementById("higher").innerHTML = "Proper number is lower than: " + resultH;
counter = setTimeout(checkCounters, 100);
switch_trigger = setTimeout(switchPlayers, 2000);
return;
}
else if(guessed < drawn)
{
c2--;
document.getElementById("notes2").innerHTML = p2 + " remaining chances: " + c2;
lower.push(guessed);
lower_l = lower.length;
lower.sort(function(a, b) {return b-a});
resultL = resultL + guessed+ " ";
document.getElementById("lower").innerHTML = "Proper number is higher than: " + resultL;
counter = setTimeout(checkCounters, 100);
switch_trigger = setTimeout(switchPlayers, 2000);
return;
}
else if(guessed == drawn)
{
p2w++;
document.getElementById("all_wins").innerHTML = p1 + " wins: " + p1w + "/" + wins + "<br/>" + p2 + " wins " + p2w + "/" + wins;
if(p2w < wins)
{
document.getElementById("msg").innerHTML = "AI found the number: " + guessed + "first!<br/>" + currentp + " and gets a point!";
document.getElementById("next").disabled = false;
}
else if(p2w == wins)
{
alert("Winner is " + currentp + "!");
document.getElementById("msg").innerHTML = "AI found the number: " + guessed + " <br/> Player: " + currentp + " reached required number of: " + wins + " wins first!";
}
end = setTimeout(endMatch, 100);
return;
}
}
逐个显示函数,以便执行它们。我已经发出了显示数组,数组[0],arrays.length的警报,但它没有帮助我,我被卡住了。我没有更多的想法,我能做什么。
在执行这部分代码时,函数开始产生负面影响:
object LineItem {
// Makes it possible to provide the validation before allocating the item
def apply(string: String): LineItem = {
require(string.length == 10)
new LineItem(string) // Call the companion-accessible constructor
}
}
// private[LineItem] makes sure that `new` only works from companion object
final case class LineItem private[LineItem](string: String)
我知道代码本身可能比这更好,但是当整个事情发挥作用时我想改进它。
答案 0 :(得分:-1)
(代表OP发布)。
用debuger花了一点时间之后,我找到了问题的根源。播放器提供的Nums不是parseInt();所以他们是来自Math.random()的nums的字符串;在哪里。解析后一切正常。感谢每一个人,我已经吸取了教训! :)