gpt4 book ai didi

javascript - 一款javascript石头剪刀布游戏!

转载 作者:行者123 更新时间:2023-12-01 08:02:54 25 4
gpt4 key购买 nike

大家好,我正在制作一个 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

}

感谢任何帮助

最佳答案

我认为为您提供自己回答这个问题的工具比发布一些工作代码更为重要。您目前使用 JavaScript 的情况如何?

我强烈建议使用 firefox + firebug,学会使用它,您将能够在几分钟内自行修复此问题。

它指出代码中的所有 3 个语法错误,然后运行,但始终返回相同的值。给游戏函数加个断点,快速单步执行显示随机函数坏了,没有返回。

关于javascript - 一款javascript石头剪刀布游戏!,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4129193/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com