gpt4 book ai didi

javascript - 如何在 JavaScript 骰子游戏中制作一个回合系统

转载 作者:行者123 更新时间:2023-11-30 19:12:12 24 4
gpt4 key购买 nike

我正在尝试制作一个 JS 骰子游戏,需要帮助制作一个回合系统(2 个或更多玩家)

我尝试过编写各种代码行,但我不知道如何实现它

var score = 0;



function rollDice() {
var die1 = document.getElementById("die1");
var die2 = document.getElementById("die2");
var status = document.getElementById("status");
var status2 = document.getElementById("status2");
var d1 = Math.floor(Math.random() * 6) + 1;
var d2 = Math.floor(Math.random() * 6) + 1;
var diceTotal = d1 + d2;
var totalscore = 0;
totalscore += diceTotal;
die1.innerHTML = d1;
die2.innerHTML = d2;
status.innerHTML = "You rolled " + diceTotal + ".";
status2.innerHTML = "Total Points " + totalscore + ".";
if (d1 == d2) {
status.innerHTML += " DOUBLES! You get a free turn!!";
}
if (diceTotal > 0) {
status2.innerHTML = score += totalscore;
}
}

最佳答案

您可以将玩家作为一个数组,并保留一个变量来跟踪回合。并在 roleDice 函数末尾更新 turn 变量。类似于下面的代码片段。


var score = 0;
var players = [player1, player2, player3];
var turn = 0;

function rollDice() {

var currentPlayer = players[turn];

....
....

turn++;
if(turn == players.length) {
turn = 0;
}

}

关于javascript - 如何在 JavaScript 骰子游戏中制作一个回合系统,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58361162/

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