我做错了什么,Javascript纸牌游戏,更高等于和更低?

时间:2014-12-11 12:54:25

标签: javascript

我有一个有三个选项的纸牌游戏'更高' '等于'并且'降低'当您选择并选择卡片的结果时,您选择的选项(计算机卡的更高/更高/更低/更低)会出现一条横幅,上面写着“您赢了”。如果您选择了错误的结果,那么也会发生这种情况,并且您将失去'。到目前为止,代码我有更高的按钮是唯一一个执行我想要的功能。 (当我选择更高的时候,我的结果高于计算机卡,我告诉我赢了,如果我选择更高,我的猜测错了,它说我输了)。

'等于'按钮和'降低'按钮不能正确执行功能。横幅弹出告诉我我赢了或输了,但这不对,并且与我选择的选项和卡片结果不符。例如:我选择较低,我的卡结果低于计算机卡,它告诉我我已经输了。我应该告诉我,我在这里取得了胜利。

这是我的javascript的一部分:

 function Player(){

var y = Math.floor(Math.random()*13)+1;
total = total + 5;
document.getElementById('img2').src = y+'.gif';
document.getElementById('score').innerHTML = total;

if(document.getElementById('higher').clicked == true && x < y)
{
    document.getElementById("win").style.display = "block";
} 
else (document.getElementById('higher').clicked == true && x > y)
{
    document.getElementById("lose").style.display = "block";
}

if(document.getElementById('equal').clicked == true && x == y)
{
    document.getElementById("win").style.display = "block";
} 
else (document.getElementById('equal').clicked == true && x != y)
{
    document.getElementById("lose").style.display = "block";
}

if(document.getElementById('lower').clicked == true && x > y)
{
    document.getElementById("win").style.display = "block";
} 
else (document.getElementById('lower').clicked == true && x < y)
{
    document.getElementById("lose").style.display = "block";
}

}

2 个答案:

答案 0 :(得分:1)

这是你的问题:

var playerChoice = "higher";

将其设置为玩家实际选择的内容,而不是在那里硬编码"higher"

答案 1 :(得分:0)

我不知道你的意思,所以这是你的游戏。

这是有效的,但并不完美,但应该足够了

&#13;
&#13;
function load(){
  document.getElementById("test3").innerHTML = Math.floor(Math.random()*13)+1;
  document.getElementById("test2").innerHTML = "";
  document.getElementById("test").innerHTML = "";
  document.getElementById("higher").style.visibility = "visible";
  document.getElementById("equal").style.visibility = "visible";
  document.getElementById("lower").style.visibility = "visible";      
}


function Player(object){
  var playerChoice = object.id;
  var y = Math.floor(Math.random()*13)+1;
  var x = document.getElementById("test3").innerHTML;
  alert(playerChoice + " " + x + " " + y);
  document.getElementById("test").innerHTML = y;
  if  ((playerChoice === "higher") && (x < y) || 
   ( playerChoice === "lower") && (x > y) || 
   ( playerChoice === "equal") && (x == y)) 
   {
     document.getElementById("test2").innerHTML = " is " + playerChoice + " than ";
   }
  else{
    document.getElementById("test2").innerHTML = " is not " + playerChoice + " than ";
  }
  document.getElementById("lower").style.visibility = "hidden";
  document.getElementById("equal").style.visibility = "hidden";
  document.getElementById("higher").style.visibility = "hidden";
} 
&#13;
<text id="test"></text>
<text id="test2"></text>
<text id="test3"></text>
<button onclick="Player(this)" id = "higher" style="visibility:hidden"> higher</button>
<button onclick="Player(this)" id = "equal" style="visibility:hidden"> equal</button>
<button onclick="Player(this)" id = "lower" style="visibility:hidden"> lower</button>
<button onclick="load()" id = "newGame"> New Game</button>
&#13;
&#13;
&#13;