[英]How do I get my dice game program to generate new "dice" numbers each roll
每次運行程序時,都會出現相同的骰子數字。 此外,我的 while 語句最終顯示用戶和計算機獲勝。 我也只希望有多達五輪的回合,之后程序會顯示游戲的獲勝者。 我如何獲得它,以便只有數字較高的玩家贏得這一輪。
<script>
var computerRandomNumberOne = Math.floor((Math.random() * 10) + 1);
var computerRandomNumberTwo = Math.floor((Math.random() * 10) + 1);
var userRandomNumberOne = Math.floor((Math.random() * 10) + 1);
var userRandomNumberTwo = Math.floor((Math.random() * 10) + 1);
var userPoints;
var computerPoints;
var userTotal;
var computerTotal;
var userWin = 0;
var computerWin = 0;
alert("Let's shake some dice!");
userTotal = userRandomNumberOne + userRandomNumberTwo;
computerTotal = computerRandomNumberOne + computerRandomNumberTwo;
while(userPoints || computerPoints != 5)
{
alert("Your turn to roll \n\nYou shook a " + userRandomNumberOne + " and a " + userRandomNumberTwo + ", so you have " + userTotal);
alert("My turn to roll \n\nI shook a " + computerRandomNumberOne + " and a " + computerRandomNumberTwo + ", so I have " + computerTotal);
if(computerTotal > userTotal)
{
computerWin++;
computerPoints++;
alert("I win " + computerTotal + " to " + userTotal + "\n\nI am winning " + computerWin + " to " + userWin);
}
else if(computerTotal < userTotal)
{
userWin++;
userPoints++;
alert("You win " + userTotal + " to " + computerTotal+ "\n\nYou win " + computerTotal + " to " + userTotal + "\n\nYou are winning " + computerWin + " to " + userWin);
}
if(computerTotal == userTotal)
{
alert("Tie! Roll Again! \n\n");
}
if(userPoints == 5)
{
alert("You win the game!");
}
else if(computerPoints == 5)
{
alert("The computer wins the game!");
}
}
</script>
干得好! 有幾件事是錯誤的。 在while循環的()中,|| 必須分隔完整的布爾語句。 在添加變量之前,您必須為變量賦值。 在其中一個警報中,您已連續兩次明確輸入“您贏了”短語。 如果您剛剛贏得了這一輪而不是檢查整個游戲的狀態,那么您的“我是/您正在獲勝”語句就會運行。 您不會在循環內重新計算擲骰子,因此它每次只使用相同的數字。 我假設您希望 userWin 和 computerWin 運行每個玩家贏得的整個游戲數量的總數,但它的編碼與回合總數相同(不會像他們應該的那樣每回合重置為 0)。 我相信就是這樣。 這是修改后的代碼:
var userWin = 0;
var computerWin = 0;
function play(){
var userTotal = 0;
var computerTotal = 0;
var userPoints = 0;
var computerPoints = 0;
alert("Let's shake some dice!");
while(userPoints < 5 && computerPoints < 5){
var computerRandomNumberOne = Math.floor((Math.random() * 10) + 1);
var computerRandomNumberTwo = Math.floor((Math.random() * 10) + 1);
var userRandomNumberOne = Math.floor((Math.random() * 10) + 1);
var userRandomNumberTwo = Math.floor((Math.random() * 10) + 1);
userTotal = userRandomNumberOne + userRandomNumberTwo;
computerTotal = computerRandomNumberOne + computerRandomNumberTwo;
alert("Your turn to roll \n\nYou shook a " + userRandomNumberOne + " and a " + userRandomNumberTwo + ", so you have " + userTotal);
alert("My turn to roll \n\nI shook a " + computerRandomNumberOne + " and a " + computerRandomNumberTwo + ", so I have " + computerTotal);
if(computerTotal > userTotal){
computerWin++;
computerPoints++;
var winningMessage = "You are winning " + userWin + " to " + computerWin;
var computerIsWinning = (computerWin > userWin);
if(computerWin == userWin){
winningMessage = "We are tied " + userWin + " to " + computerWin;
}
else if(computerIsWinning){
winningMessage = "I am winning " + computerWin + " to " + userWin;
}
alert("I win " + computerTotal + " to " + userTotal + "\n\n" + winningMessage);
}
else if(computerTotal < userTotal){
userWin++;
userPoints++;
var winningMessage = "You are winning " + userWin + " to " + computerWin;
var computerIsWinning = (computerWin > userWin);
if(computerWin == userWin){
winningMessage = "We are tied " + userWin + " to " + computerWin;
}
else if(computerIsWinning){
winningMessage = "I am winning " + computerWin + " to " + userWin;
}
alert("You win " + userTotal + " to " + computerTotal + "\n\n" + winningMessage);
}
else{
alert("Tie! Roll Again! \n\n");
}
if(userPoints == 5){
alert("You win the game!");
}
else if(computerPoints == 5){
alert("The computer wins the game!");
}
}
userPoints = 0;
computerPoints = 0;
}
play();
我已經保留了你擁有的隨機性,但如果你想讓它更干凈一點,你可以使用randojs.com讓它變得簡單。 你所要做的就是輸入:
rando(1, 10)
而不是每次都這樣:
Math.floor((Math.random() * 10) + 1)
但這是一個偏好問題! 如果您想使用randojs.com ,只需在 HTML 文檔的 head 標簽的頂部添加以下內容:
<script src="https://randojs.com/1.0.0.js"></script>
然后您就可以改用此代碼:
var userWin = 0;
var computerWin = 0;
function play(){
var userTotal = 0;
var computerTotal = 0;
var userPoints = 0;
var computerPoints = 0;
alert("Let's shake some dice!");
while(userPoints < 5 && computerPoints < 5){
var computerRandomNumberOne = rando(1, 10);
var computerRandomNumberTwo = rando(1, 10);
var userRandomNumberOne = rando(1, 10);
var userRandomNumberTwo = rando(1, 10);
userTotal = userRandomNumberOne + userRandomNumberTwo;
computerTotal = computerRandomNumberOne + computerRandomNumberTwo;
alert("Your turn to roll \n\nYou shook a " + userRandomNumberOne + " and a " + userRandomNumberTwo + ", so you have " + userTotal);
alert("My turn to roll \n\nI shook a " + computerRandomNumberOne + " and a " + computerRandomNumberTwo + ", so I have " + computerTotal);
if(computerTotal > userTotal){
computerWin++;
computerPoints++;
var winningMessage = "You are winning " + userWin + " to " + computerWin;
var computerIsWinning = (computerWin > userWin);
if(computerWin == userWin){
winningMessage = "We are tied " + userWin + " to " + computerWin;
}
else if(computerIsWinning){
winningMessage = "I am winning " + computerWin + " to " + userWin;
}
alert("I win " + computerTotal + " to " + userTotal + "\n\n" + winningMessage);
}
else if(computerTotal < userTotal){
userWin++;
userPoints++;
var winningMessage = "You are winning " + userWin + " to " + computerWin;
var computerIsWinning = (computerWin > userWin);
if(computerWin == userWin){
winningMessage = "We are tied " + userWin + " to " + computerWin;
}
else if(computerIsWinning){
winningMessage = "I am winning " + computerWin + " to " + userWin;
}
alert("You win " + userTotal + " to " + computerTotal + "\n\n" + winningMessage);
}
else{
alert("Tie! Roll Again! \n\n");
}
if(userPoints == 5){
alert("You win the game!");
}
else if(computerPoints == 5){
alert("The computer wins the game!");
}
}
userPoints = 0;
computerPoints = 0;
}
play();
干杯!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.