我正在尝试使用Javascript制作保龄球模拟器。我创建了一个函数,它在0和0之间取一个随机数。 10.在保龄球比赛中,你每轮可以投2次。我设法得到2个值,如5和8,但问题是你不能在保龄球投掷超过10针。当我扔6时,我无法弄清楚如何得到值4,3,2,1或0。
这是我在代码段中的代码......
var team1 = ["Jason", "Jake", "Jane", "Joe"];
var team2 = ["John", "Drake", "Nick", "Joseph"];
var rounds = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var pin = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
for (var team1 = 0; team1 < 2; team1++) {
console.log(getRandomPin(team1));
}
for (var team2 = 0; team2 < 2; team2++) {
console.log(getRandomPin(team2));
}
function getRandomPin(min, max) {
min = Math.ceil(0);
max = Math.floor(11);
return Math.floor(Math.random() * (11 - 0)) + 0;
}
提前谢谢。
答案 0 :(得分:1)
这里有一种方法可以跟踪留下多少针脚以及有多少针脚......这里我只使用了一个&#39;团队&#39;,但你可以很容易地做到两个或更多团队。 请看一下,看看这对你有帮助。
var team1 = ["Jason", "Jake", "Jane", "Joe"];
var team2 = ["John", "Drake", "Nick", "Joseph"];
var rounds = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var pin = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var aux2 = 0;
var maxVal = 11;
for (var team2 = 0; team2 < 2; team2++) {
maxVal = maxVal - aux2;
console.log('Pins Remaining: ' + (maxVal-1));
var pins = getRandomPin(0, maxVal);
aux2 += pins;
if (pins == 10){
console.log('Round-' + (team2 + 1) +' | STRIKE!');
break; //leaves the function because when strike occurs no second round is played
}else{
console.log('Round-' + (team2 + 1) +' | pins striked: ' + pins);
}
}
console.log('Total pins striked: ' + aux2);
aux2 = 0;
maxVal = 11;
function getRandomPin(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}
&#13;
答案 1 :(得分:0)
您需要一个变量来跟踪剩余的针数。不要忘记在轮次之间重置它。
var team1 = ["Jason", "Jake", "Jane", "Joe"];
var team2 = ["John", "Drake", "Nick", "Joseph"];
var rounds = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var pins = 11;
for (var team1 = 0; team1 < 2; team1++) {
console.log('Adam', getRandomPin());
}
pins = 11;
for (var team2 = 0; team2 < 2; team2++) {
console.log('Tom', getRandomPin());
}
function getRandomPin() {
var pinsKnocked = Math.floor(Math.random() * pins);
pins -= pinsKnocked;
return pinsKnocked;
}
答案 2 :(得分:0)
我的代码有点冗长,但我尝试添加注释来解释我在编写时所思考的一切。它仍然可以进一步优化,但我希望这会让你以一种稍微不同的方式思考这个问题。我接下来要做的一个这样的优化是将重复代码(两个相似的循环)移动到单个函数中。另一个优化是添加if
语句以跳过第二次投掷,如果第一次投掷获得10个引脚。无论如何,你走了:
var team1 = ["Adam", "Ayman", "Matthijs", "Joey"];
var team2 = ["Tom", "Afzal", "Jef", "Anouar"];
var rounds = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
// Let's loop over all the rounds
for (var roundNumber = 1; roundNumber <= rounds.length; roundNumber++) {
// This will tell us what round we are on
console.log('******Round Number:', roundNumber);
// Let's let team number 1 go first
console.log('***Team One');
for (var i = 0; i < team1.length; i++) {
// There are always 10 pins standing in the beginning
var standingPins = 10;
// The we see how many pins are hit first
var pinsHitFirst = getRandomPin(standingPins);
// The number of pins left standing
standingPins = standingPins - pinsHitFirst;
// and the number of pins the player hit the second attempt
var pinsHitSecond = getRandomPin(standingPins);
console.log(team1[i] + '\'s Score: ', pinsHitFirst, pinsHitSecond);
} // Now it's time for team number 2
console.log('***Team Two');
for (var i = 0; i < team2.length; i++) {
// There are always 10 pins standing in the beginning
var standingPins = 10;
// The we see how many pins are hit first
var pinsHitFirst = getRandomPin(standingPins);
// The number of pins left standing
standingPins = standingPins - pinsHitFirst;
// and the number of pins the player hit the second attempt
var pinsHitSecond = getRandomPin(standingPins);
console.log(team2[i] + '\'s Score: ', pinsHitFirst, pinsHitSecond);
}
}
// This will allow us to randomly select the number of pins hit
// based on how many pins were still standing
function getRandomPin(remainingPins) {
return Math.round(Math.random() * remainingPins);
}