嘿伙计们,我正在制作一个javascript摇滚,纸张,剪刀游戏!所以代码在下面,它不起作用警报没有显示,我真的不知道我搞砸了.
function randomNumber(x,y){ //returns a random number from an interval given
var z;
if (x>y) {z=x; x=y; y=z;} // turns the interval upside down if the numbers of the interval are entered in unconsecutive order
else if(x==y) {return x; break;} //if there's no interval but two same digits return the number entered
var randomNo = (y-x)*Math.random() + x; // the random value itself in float type
Math.round(randomNo); // round it to integer
}
function outcomeType(value){ //gives the type of hand-symbol in game for each player according to the random No given
var outcome;
if (value==1 || value==4){outcome="rock"} //value 1 or 4 gives rock, same way with other two...
else if(value==2) {outcome="paper"}
else {outcome="scissors"}
return outcome;
}
function result(x,y){ // compares the numbers so that a winner is decided
if(x>y){return 1} //player A wins
else if(x==y){return 0;} //draw
else {return 2} //player B wins
}
function game(){
var a=randomNumber(1,3); // random number for player A
var b=randomNumber(1,3);// random number for player B
if(a!=2 && b!=2 && a!=b){ //the case rock-scissors, rocks from 1 beecomes 4 in order to beat in result()
if(a>b){b=4}
else{a=4}
}
var winner = result(a,b); // we have a winner!!!
if (winner==1) {alert("Player A wins with"+outcomeType(a)+"against"+outcomeType(b););} // the alert should be like: Player A wins with "scissors" against "paper"
else if (winner==0) {alert("Draw with"+outcomeType(a););} //draw
else {alert("Player B wins with"+outcomeType(b)+"against"+outcomeType(a););} //player B winning alet
}
Run Code Online (Sandbox Code Playgroud)
感谢任何帮助
Pau*_*sey 11
我认为给你自己回答这个问题的工具更重要,而不是发布一些有效的代码.你目前如何使用javascript?
我强烈建议使用firefox + firebug,学会使用它,你就可以在几分钟内自行修复.
它指出代码中的所有3个语法错误,然后运行,但总是返回相同的值.在游戏功能中添加断点并快速逐步显示随机功能被破坏,并且没有返回.
一个相当严重的问题是你的"randomNumber"例程实际上return没有任何东西.最后一行应该(可能)
return Math.round(randomNo); // round it to integer
Run Code Online (Sandbox Code Playgroud)