javascript中的骰子滚动 函数rolldice(){ var x=Math.floor(Math.random()*((6-1)+1)+1); var y=Math.floor(Math.random()*((6-1)+1)+1); var dicetotal=x+y; var-double=0; $('.dice1').attr('id',“dice”+x); $('.dice2').attr('id',“dice”+y); 如果(x==y){/
您只需要进行递归调用:javascript中的骰子滚动 函数rolldice(){ var x=Math.floor(Math.random()*((6-1)+1)+1); var y=Math.floor(Math.random()*((6-1)+1)+1); var dicetotal=x+y; var-double=0; $('.dice1').attr('id',“dice”+x); $('.dice2').attr('id',“dice”+y); 如果(x==y){/,javascript,Javascript,您只需要进行递归调用: function rolldice() { var x = Math.floor(Math.random() * ((6 - 1) + 1) + 1); var y = Math.floor(Math.random() * ((6 - 1) + 1) + 1); var dicetotal = x + y; var double = 0; $('.dice1').attr('id', "dice" + x); $('.dic
function rolldice() {
var x = Math.floor(Math.random() * ((6 - 1) + 1) + 1);
var y = Math.floor(Math.random() * ((6 - 1) + 1) + 1);
var dicetotal = x + y;
var double = 0;
$('.dice1').attr('id', "dice" + x);
$('.dice2').attr('id', "dice" + y);
if (x == y) { //<----checking if there is a double
var double = double++; //<---increment double count
//Now reroll the dice, but if you hit 3 doubles in a row, you get message go to jail.
}
};
var-dbl=0;
函数rolldice(){
var x=Math.floor(Math.random()*((6-1)+1)+1);
var y=Math.floor(Math.random()*((6-1)+1)+1);
var dicetotal=x+y;
$('.dice1').attr('id',“dice”+x);
$('.dice2').attr('id',“dice”+y);
如果(x==y){/我认为您需要创建如下内容:
var dbl = 0;
function rolldice() {
var x = Math.floor(Math.random() * ((6 - 1) + 1) + 1);
var y = Math.floor(Math.random() * ((6 - 1) + 1) + 1);
var dicetotal = x + y;
$('.dice1').attr('id', "dice" + x);
$('.dice2').attr('id', "dice" + y);
if (x == y) { //<----checking if there is a double
dbl++; //<---increment double count
if(dbl%3==0) $('.out').attr('id', "jail");
//Now reroll the dice, but if you hit 3 doubles in a row, you get message go to jail.
rolldice();
}
};
var-double=0;
函数rolldice(){
var x=数学地板(数学随机()*((6-1)+1)+1);
变量y=Math.floor(Math.random()*((6-1)+1)+1);
var dicetotal=x+y;
$('.dice1').attr('id',“dice”+x);
$('.dice2').attr('id',“dice”+y);
如果(x==y){
如果(双<3){
double++;//增加双刀
rolldice();//再次调用rolldice。。。
}否则{
//这里有三个一排。。。。
}
}
}
这有一些复杂性。当更换玩家时,需要重置变量值以检查双掷
请执行以下操作:
var double = 0;
function rolldice(){
var x = Math.floor(Math.random() * ((6-1)+1) + 1);
var y = Math.floor(Math.random() * ((6-1)+1) + 1);
var dicetotal = x + y;
$('.dice1').attr('id', "dice" + x);
$('.dice2').attr('id', "dice" +y);
if(x==y) {
if (double < 3) {
double++; // increase dobule
rolldice(); // Call rolldice again...
} else {
// Here there is 3 in a row....
}
}
}
var-dblRolls;
函数userChange(){//在用户更改时调用此函数
dblRolls=0;
滚动骰子();
}
函数rolldice(){
var x=Math.floor(Math.random()*((6-1)+1)+1);
var y=Math.floor(Math.random()*((6-1)+1)+1);
var dicetotal=x+y;
var-double=0;
$('.dice1').attr('id',“dice”+x);
$('.dice2').attr('id',“dice”+y);
如果(x==y){/不要使用循环
相反,添加doubles计数器作为rolldice()
函数的参数,并从函数本身调用该函数:
var dblRolls;
function userChange(){//call this on change of user
dblRolls=0;
rollDice();
}
function rolldice() {
var x = Math.floor(Math.random() * ((6 - 1) + 1) + 1);
var y = Math.floor(Math.random() * ((6 - 1) + 1) + 1);
var dicetotal = x + y;
var double = 0;
$('.dice1').attr('id', "dice" + x);
$('.dice2').attr('id', "dice" + y);
if (x == y) { //<----checking if there is a double
dblRoll++; //<---increment double count
if(dblRoll==3)
//jail
else
rollDice();
}
函数滚动骰子(双重计数){
var x=Math.floor(Math.random()*((6-1)+1)+1);
var y=Math.floor(Math.random()*((6-1)+1)+1);
var dicetotal=x+y;
$('.dice1').attr('id',“dice”+x);
$('.dice2').attr('id',“dice”+y);
如果(x==y){/好的,除此之外,这已经有两个多小时了,并且已经有4个答案了,我想加上我的2美分
你说你想做一个垄断游戏。在大多数(如果不是全部的话)掷骰子之后,玩家必须做出决定。这意味着每次掷骰子之后,你都要等待用户输入(例如,按下一些按钮)
postet的所有其他答案都建议以某种方式使用递归调用。相反,我建议将双打数与当前播放机一起存储在某个全局变量中。您不使用循环,而是类似于:
function rolldice(doubleCount) {
var x = Math.floor(Math.random() * ((6 - 1) + 1) + 1);
var y = Math.floor(Math.random() * ((6 - 1) + 1) + 1);
var dicetotal = x + y;
$('.dice1').attr('id', "dice" + x);
$('.dice2').attr('id', "dice" + y);
if (x == y) { //<----checking if there is a double
doubleCount++;
if (doubleCount == 3)
{
//go to jail
}
else
{
rolldice(doubleCount);
}
}
};
var-doubleCount=0;
函数rolldice(){
var x=Math.floor(Math.random()*((6-1)+1)+1);
var y=Math.floor(Math.random()*((6-1)+1)+1);
var dicetotal=x+y;
$('.dice1').attr('id',“dice”+x);
$('.dice2').attr('id',“dice”+y);
如果(x==y){/此脚本工作:
var doubleCount = 0;
function rolldice() {
var x = Math.floor(Math.random() * ((6 - 1) + 1) + 1);
var y = Math.floor(Math.random() * ((6 - 1) + 1) + 1);
var dicetotal = x + y;
$('.dice1').attr('id', "dice" + x);
$('.dice2').attr('id', "dice" + y);
if (x == y) { //<----checking if there is a double
doubleCount++; //<---increment double count
if (doubleCount > 2) {
// Got to Jail
}
}
// Proceed as usual and come back to this, when the user presses the "Roll" Button again
};
函数rollDice(){
var dice1=document.getElementById(“dice1”);
var dice2=document.getElementById(“dice2”);
var status=document.getElementById(“状态”);
var d1=数学地板(数学随机()*6)+1;
var d2=数学地板(数学随机()*6)+1;
总风险=d1+d2;
dice1.innerHTML=d1;
dice2.innerHTML=d2;
status.innerHTML=“您滚动了“+diceTotal+”;
如果(d1==d2){
status.innerHTML+=“
双倍!您可以自由转身!!”;
}
}
这是一种可能的解决方案
function rollDice(){
var dice1 = document.getElementById("dice1");
var dice2 = document.getElementById("dice2");
var status = document.getElementById("status");
var d1 = Math.floor(Math.random() * 6) + 1;
var d2 = Math.floor(Math.random() * 6) + 1;
var diceTotal = d1 + d2;
dice1.innerHTML = d1;
dice2.innerHTML = d2;
status.innerHTML = "You rolled "+diceTotal+".";
if(d1 == d2){
status.innerHTML += "<br />DOUBLES! You get a free turn!!";
}
}
或
不要调用变量double
。稍后你会感谢我的。如果你在这里添加一个循环,你将无法接受两个循环之间的用户输入。这真的是你在垄断游戏中想要的吗?第二个var double
是一个重新定义。只要double++;
就足够了。嗯,你是对的。一旦打到了double,我会的必须移动monnopoly棋子,这取决于它落在哪里,玩家可能不需要再次掷骰子。但我可以稍后添加这些条件,对吗?我强烈建议,你现在忽略双打,对游戏的其余部分进行编码,稍后再在双打上添加骰子。这样更简单。“双打”是一个受限/保留字。请重命名变量。我不确定您在@If(dbl%3==0)执行什么操作?我知道您正在检查dbl是否等于3,但不确定为什么有一个%。如果dbl余数为0,则可被3整除(模运算),然后您可以将dbl重置为0,如果您愿意的话。好的。我想将#滚动(其id)按钮的值更改为“再次滚动”?如果条件满足。什么?为什么更改按钮id?范围是什么?
function rolldice(dbl=0){
var x = Math.floor(Math.random()*((6-1)+1) + 1);
var y = Math.floor(Math.random()*((6-1)+1) + 1);
if(x===y){
if(dbl!==3){
dbl++;
rolldice(dbl);
}else{
//goto jail
}
}else{
//no double
dbl=0;
}
}
function rolldice(dbl=0){
var x = Math.floor(Math.random()*((6-1)+1) + 1);
var y = Math.floor(Math.random()*((6-1)+1) + 1);
if(x===y&&dbl!==3)
dbl++;
rolldice(dbl);
}else if(x===y&&dbl===3){
//goto jail
}else{
//no double
dbl=0;
}
}