Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/86.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.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
rps函数不工作(javascript)_Javascript_Jquery_Html - Fatal编程技术网

rps函数不工作(javascript)

rps函数不工作(javascript),javascript,jquery,html,Javascript,Jquery,Html,Rps游戏代码不起作用。我的battle函数应该将win设置为1、2或3,但它始终保持默认值0。我通过控制台检查了其他值,它们似乎都在工作。这是我的密码 function Player(number) { this.number = number; this.choice = 0; } var player1 = new Player(1); var player2 = new Player(2); var win = 0; var battle = function() { if (pla

Rps游戏代码不起作用。我的battle函数应该将win设置为1、2或3,但它始终保持默认值0。我通过控制台检查了其他值,它们似乎都在工作。这是我的密码

function Player(number) {
this.number = number;
this.choice = 0;
}

var player1 = new Player(1);
var player2 = new Player(2);
var win = 0;

var battle = function() {
if (player1.choice === player2.choice) {
    win = 3;
} else if (player1.choice + 2 === player2.choice && player1.choice === 1){
    win = 2;
} else if (player1.choice + 1 === player2.choice && player1.choice === 1) {
    win = 1;
} else if (player1.choice + 1 === player2.choice && player1.choice === 2) {
    win = 1;
} else if (player1.choice - 1 === player2.choice && player1.choice === 2) {
    win = 2;
} else if (player1.choice - 1 === player2.choice && player1.choice === 3) {
    win = 2;
} else if (player1.choice - 2 === player2.choice && player1.choice === 3) {
    win = 1;
} else {
    alert ('someone pressed the wrong button')
}
}

var Reset = function () {
win = 0;
player1.choice = 0;
player2.choice = 0;
}

$(document).ready(function() {
$(document).keydown(function(event) {
    if (event.which === 81) {
        player1.choice = 1;
    } else if (event.which === 87){
        player1.choice = 2;
    } else if (event.which === 69){
        player1.choice = 3;
    } else if (event.which === 37){
        player2.choice = 1;
    } else if (event.which === 40){
        player2.choice = 2;
    } else if (event.which === 39){
        player2.choice = 3;
    } 
})
if (player1.choice > 0 && player2.choice > 0) {
    battle();
    if (win === 1) {
        $('.winner').append('<p>player1 wins!</p>')
    } else if (win === 2) {
        $('.winner').append('<p>player2 wins!</p>')
    } else if (win === 3) {
        $('.winner').append('<p>It is a draw!</p>')
    }
}

})
功能播放器(数字){
这个数字=数字;
这个选项=0;
}
var player1=新玩家(1);
var player2=新玩家(2);
var-win=0;
var=函数(){
如果(player1.choice==player2.choice){
win=3;
}else if(player1.choice+2==player2.choice&&player1.choice==1){
win=2;
}else if(player1.choice+1==player2.choice&&player1.choice==1){
win=1;
}else if(player1.choice+1==player2.choice&&player1.choice==2){
win=1;
}else if(player1.choice-1==player2.choice&&player1.choice==2){
win=2;
}else if(player1.choice-1==player2.choice&&player1.choice==3){
win=2;
}else if(player1.choice-2==player2.choice&&player1.choice==3){
win=1;
}否则{
警报('有人按错了按钮')
}
}
变量重置=函数(){
win=0;
player1.choice=0;
player2.choice=0;
}
$(文档).ready(函数(){
$(文档).keydown(函数(事件){
if(event.which==81){
player1.choice=1;
}else if(event.which==87){
player1.choice=2;
}else if(event.which==69){
player1.choice=3;
}else if(event.which==37){
player2.choice=1;
}else if(event.which==40){
player2.choice=2;
}else if(event.which==39){
player2.choice=3;
} 
})
如果(player1.choice>0&&player2.choice>0){
战斗();
如果(win==1){
$('.winner')。追加('player1赢!

')) }否则如果(赢===2){ $('.winner')。追加('player2赢!

')) }否则如果(赢===3){ $('.winner')。追加('这是平局!

')) } } })
我的html有一个div,里面有类“winner”。 附加问题
在两个玩家都选择了选项后,如何取消按键功能。

您是否在创建的玩家类中为玩家提供了选择?应该是这样的:

function Player(number, choice) {
this.number = number;
this.choice = choice;
}

这是因为从未调用过battle函数。如果将console.log()添加到battle()函数中,很容易看出这一点。类似于
console.log('battleing!')
。查看两个玩家是否都做出了选择的检查只在文档第一次准备好时发生一次。这种情况发生在两个玩家都没有足够的时间做出选择的时候,因此不满足条件,也不会调用battle()。您需要设置某种循环以持续运行,并检查两个玩家是否都进行了选择。