我正在为学校的棋盘游戏制作应用程序,该游戏基于Game of the Goose。在这个游戏中,一旦你在某个地方并且你超过它设置的最高数字,你就会留下剩余的价值。例如,如果您是64并且滚动4,则返回到现场66,因为67是最高数字。我使用math.random和math.floor从1到6为玩家添加一个数字。
*编辑:
这是我写的代码,但它有点草率,对不起。
var players = [
{
name: "Speler 1",
positie: 0
},
{
name: "Speler 2",
positie: 0
},
{
name: "Speler 3",
positie: 0
},
{
name: "Speler 4",
positie: 0
}
];
var position = 0;
var currentPlayer = players[position];
function rolClick(){
if (position >= players.length){
position = 0;
};
currentPlayer = players[position++];
var rollen = Math.floor(Math.random() * 6) + 1;
if (rollen === 1){
currentPlayer.positie += 1;
}else if(rollen === 2){
currentPlayer.positie += 2;
}else if(rollen === 3){
currentPlayer.positie += 3;
}else if(rollen === 4){
currentPlayer.positie += 4;
}else if(rollen === 5){
currentPlayer.positie += 5;
}else if(rollen === 6){
currentPlayer.positie += 6;
}};
答案 0 :(得分:0)
像这样......
function onRoll(this) {
currentVal=this.value;
x=Math.random();//Ensure you have your limitations of random number in place;
newVal=currentVal+x;
if(newVal>66)//whatever your limit
return 66;
else
return newVal;
}
答案 1 :(得分:0)
假设您正在增加的变量称为当前。
function getRandomBetween(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
var highest = 67;
current += getRandomBetween(1, 6);
if(current > highest) return highest-1;
答案 2 :(得分:0)
这个脚本做你想要的(我认为):
maxValue = 67
dice = Math.floor((Math.random() * 10) + 1)
oldValue = 64
if(oldValue + dice > maxValue){
alert("You reached the maximum. Your Value is set to " + (maxValue-1))
newValue = maxValue-1
}else{
newValue = oldValue+dice
}
你应该阅读if语句...... 在这里你可以阅读和学习它: http://www.w3schools.com/js/js_if_else.asp