修复if语句游戏错误

时间:2018-07-30 18:17:59

标签: javascript html html5

我想在以下链接中制作这样的游戏 https://codepen.io/zutk133/pen/NvEGgB

首先,为了测试游戏,我减少了盒子的数量(从64个减少到10个)

不幸的是,我注意到在某些尝试中,游戏无法完成;有些框无法隐藏,因为没有针对它们的正确问题(产品)

例如,两个框的内部文本为5,问题文本(乘积)不等于25,因此玩家将无法在时间到之前完成游戏

我知道这是因为以下“ if语句”

var check0 = function () {
    multi()
    if (b === 0 || b1 === 0 || b1 === b) {
        check0()
    } else {
        total = b * b1;
        document.getElementById("total").innerText = total;
    }
}

这会阻止方形乘积出现在问题框中,但是当我删除(b1 === b)时,游戏变得更加糟糕,许多问题(乘积)仍然没有答案。

1 个答案:

答案 0 :(得分:0)

我修复了它。我从b1 === b函数中删除了check0,因为数字标牌可能重复,因此这永远不是有效的检查。此外,我修改了以下内容:

  a=Math.floor(Math.random()*array.length)
  b=array[a];
  a1=Math.floor(Math.random()*array.length)
  b1=array[a1];

对此:

  a=Math.floor(Math.random()*array.length)
  b=array[a];
  a1=Math.floor(Math.random()*array.length)
  while(a1 === a) {
    a1=Math.floor(Math.random()*array.length)
  }
  b1=array[a1];

从数字标牌中随机选择数字时,aa1之间不会再有任何意外冲突。现在,游戏将始终可以完成。您可以通过运行下面的完整代码段进行尝试:

var b;
var b1;
var mark=0;
var array=[];
var array1=[];
var t;
for(i=0;i<8;i++)
{
      var ran=Math.ceil(Math.random()*9);
      array.push(ran)
      document.getElementById("outline").innerHTML+="<button class='box' onclick='clickcheck("+i+")' id='box"+i+"'>"+array[i]+"</button>";
      array1.push(0);
      document.getElementById("box"+i).setAttribute("disabled",true);
}
var color={1:"DeepPink",2:"ForestGreen",3:"Red",4:"Yellow",5:"RoyalBlue",6:"Chocolate",7:"DarkViolet",8:"MediumAquaMarine"};
for(i=0;i<8;i++)
  {
    var x=document.getElementById("box"+i).innerHTML;
    y=color[x];
    document.getElementById("box"+i).style.background=y;
  }
document.getElementById("total").style.visibility="hidden";
document.getElementById("lose").style.visibility="hidden";
var gamestart=function()
{
  for(i=0;i<8;i++)
  {
    document.getElementById("box"+i).disabled=false;
  }
  t=setInterval(time, 1000);
  start()
}
var start=function()
{  
  if(array+""==array1+"" && s<=90)
  { 
    mark+=4;
    document.getElementById("score").innerText="Score: "+mark;
    alert("Your Score is :"+mark+" Well try")
    document.getElementById("total").style.visibility="hidden";
    document.location.reload(true);
  }
  else
 {
   check0()
  }
}
var check0=function()
{
  multi()
     if(b===0 || b1===0)
    {
      check0()
    }
    else
   {
       total=b*b1;
       document.getElementById("total").innerText=total;
   }
}
var multi=function()
{
  document.getElementById("start").setAttribute("disabled",true);
  document.getElementById("total").style.visibility="visible";
  a=Math.floor(Math.random()*array.length)
  b=array[a];
  a1=Math.floor(Math.random()*array.length)
  while(a1 === a) {
    a1=Math.floor(Math.random()*array.length)
  }
  b1=array[a1];
}

var mul=[];
var check1=[];
var ids=[];
var clickcheck=function(i)
  {
    var c=document.getElementById("box"+i).innerText;
    document.getElementById("box"+i).setAttribute("disabled",true);
    check1.push("box"+i);
    ids.push(i);
    mul.push(c);
    if(mul.length==2)
      {
        document.getElementById(check1[1]).disabled=false;
        document.getElementById(check1[0]).disabled=false;
        total1=mul[0]*mul[1]
        mul=[];
        equalcheck()
      }
  }
var equalcheck=function()
{
  if(total===total1)
  {
     array[ids[1]]=0
     array[ids[0]]=0;
     document.getElementById(check1[0]).style.visibility="hidden";
     document.getElementById(check1[1]).style.visibility="hidden";
     mark+=3;
     document.getElementById("score").innerText="Score: "+mark;
     start()
  }
  else
  {
     alert("wrong");
     mark-=3;
     document.getElementById("score").innerText="Score: "+mark;
  }
  check1=[];
  ids=[];
}
var s=0;
var time=function()
{
  s+=1;
  if(s<10)
  {
     document.getElementById("timer").innerText="Time: 0"+s+" sec";
  }
  else if(s>90)
  {
    alert("You lose..!");
    document.location.reload(true);
  }
  else
  {
    document.getElementById("timer").innerHTML="Time: "+s+" sec";
  }
}
#outline
{
  height:312px;
  width:412px;
  margin-top:30px;
}
.box
{
  height:40px;
  width:50px;
  border:1px solid black;
  float:left;
  font-size:190%;
  padding-top:5px;
  border-radius:13px;
  color:black;
}
#total
{
  height:40px;
  width:50px;
  border:1px solid black;
  font-size:190%;
  padding-top:10px;
  border-radius:10px;
  background-color:gray;
  margin-top:30px;
}
.but
{
  height:40px;
  width:150px;
  border:1px solid black;
  font-size:120%;
  padding-top:5px;
  border-radius:15px;
  background-color:Maroon;
  color:Cornsilk 
}
#lose
{
  margin-top:-400px;
}
<body >
  <center>
     <h1>Maths multipilication game</h1>
    <button class="but" id="timer">Time: 00 sec</button>
    <button class="but" id="start" onclick="gamestart()">start</button>
    <button class="but" id="score">Score: 0</button>
     <div id="total"></div>
     <div id="outline"></div>
    <h1 id="lose" onclick="document.location.reload(true)">Youlose</h1>
     
  </center>
</body>