javascript - 停止分数变为负数且超过 52
问题描述
我有一个基于文本的战争游戏,其中团队以 26 分(userScore 和 computerScore)开始。当玩家获胜时,它会上升,当玩家失败时,它会下降。
问题是,分数变为负数并超过 52(在 51 张牌发生战争的情况下),这将是所有牌加上一些牌(不能超过 52 张牌)。
如何限制 userScore 和 computerScore 为负数或超过 52?
var suits = ["Spades", "Hearts", "Clubs", "Diamonds"];
var cards = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace"];
var attempts = 1;
var war = false;
var computerScore = 26;
var userScore = 26;
while (computerScore < 52 && userScore < 52)
{
var computerIndex = Math.floor(Math.random() * cards.length);
var userIndex = Math.floor(Math.random() * cards.length);
var computerCard = cards[computerIndex];
var userCard = cards[userIndex];
var computerSuit = suits[Math.floor(Math.random()*suits.length)];
var userSuit = suits[Math.floor(Math.random()*suits.length)];
alert("I drew a " + computerCard + " of " + computerSuit +"\nYou drew a " + userCard + " of " + userSuit);
if (computerIndex > userIndex && war == false)
{
computerScore++;
userScore--;
alert("I win. \n\nI have " + computerScore + " cards and you have " + userScore + " cards.");
}
else if (computerIndex < userIndex && war == false)
{
computerScore--;
userScore++;
alert("You win. \n\nI have " + computerScore + " cards and you have " + userScore + " cards.");
}
else if (computerIndex == userIndex && war == false)
{
alert("TIE! TIME FOR WAR! \n\nI have " + computerScore + " cards and you have " + userScore + " cards")
war = true;
var computerIndex = Math.floor(Math.random() * cards.length);
var userIndex = Math.floor(Math.random() * cards.length);
var computerCard = cards[computerIndex];
var userCard = cards[userIndex];
var computerSuit = suits[Math.floor(Math.random()*suits.length)];
var userSuit = suits[Math.floor(Math.random()*suits.length)];
alert("I drew a " + computerCard + " of " + computerSuit +"\nYou drew a " + userCard + " of " + userSuit);
if (computerIndex > userIndex && war == true)
{
computerScore = computerScore + (attempts * 3);
userScore = userScore - (attempts * 3);
alert("I win. \n\nI have " + computerScore + " cards and you have " + userScore + " cards.");
war = false;
attempts = 1;
}
else if (computerIndex < userIndex && war == true)
{
userScore = userScore + (attempts * 3);
computerScore = computerScore - (attempts * 3);
alert("You win. \n\nI have " + computerScore + " cards and you have " + userScore + " cards.");
war = false;
attempts = 1;
}
else
{
alert("TIE! TIME FOR WAR (AGAIN)! \n\nI have " + computerScore + " cards and you have " + userScore + " cards")
attempts++;
var computerIndex = Math.floor(Math.random() * cards.length);
var userIndex = Math.floor(Math.random() * cards.length);
var computerCard = cards[computerIndex];
var userCard = cards[userIndex];
var computerSuit = suits[Math.floor(Math.random()*suits.length)];
var userSuit = suits[Math.floor(Math.random()*suits.length)];
alert("I drew a " + computerCard + " of " + computerSuit +"\nYou drew a " + userCard + " of " + userSuit);
if (computerIndex == userIndex)
{
war = true;
}
else
{
war = false;
}
}
}
}
if (computerScore >= 52)
{
alert("I WIN! GOOD GAME!");
}
else
{
alert("YOU WIN! GOOD GAME!")
}
解决方案
您正在使用 (cards.length) 来计算 computerIndex 和 userIndex,而您应该使用 (cards.length)-1,因为数组总是从索引 0 开始,这就是最终卡片总数超过 52 的原因。西装也一样。编辑:为避免括号放错位置,只需创建一个变量并使用它,如下所示:
<script>
alert("Hi. Let's Play War!");
var computerScore = 48;
var userScore = 4;
var suits = ["Spades", "Hearts", "Clubs", "Diamonds"];
var cards = ["2", "3", "4"];
var attempts = 1;
var war = false;
function mathClampC(min,mid,max)
{
var min = 0;
var max = 52;
var mid = computerScore;
return Math.min(Math.max(min,mid),max);
}
function mathClampU(min,mid,max)
{
var min = 0;
var max = 52;
var mid = userScore;
return Math.min(Math.max(min,mid),max);
}
while (computerScore < 52 && userScore < 52)
{
var computerIndex = Math.floor(Math.random() * cards.length);
var userIndex = Math.floor(Math.random() * cards.length);
var computerCard = cards[computerIndex];
var userCard = cards[userIndex];
var computerSuit = suits[Math.floor(Math.random()*suits.length)];
var userSuit = suits[Math.floor(Math.random()*suits.length)];
alert("I drew a " + computerCard + " of " + computerSuit +"\nYou drew a " + userCard + " of " + userSuit);
if (computerIndex > userIndex && war == false)
{
computerScore++;
userScore--;
alert("I win. \n\nI have " + mathClampC() + " cards and you have " + mathClampU() + " cards.");
}
else if (computerIndex < userIndex && war == false)
{
computerScore--;
userScore++;
alert("You win. \n\nI have " + mathClampC() + " cards and you have " + mathClampU() + " cards.");
}
else if (computerIndex == userIndex && war == false)
{
alert("TIE! TIME FOR WAR! \n\nI have " + mathClampC() + " cards and you have " + mathClampU() + " cards")
war = true;
var computerIndex = Math.floor(Math.random() * cards.length);
var userIndex = Math.floor(Math.random() * cards.length);
var computerCard = cards[computerIndex];
var userCard = cards[userIndex];
var computerSuit = suits[Math.floor(Math.random()*suits.length)];
var userSuit = suits[Math.floor(Math.random()*suits.length)];
alert("I drew a " + computerCard + " of " + computerSuit +"\nYou drew a " + userCard + " of " + userSuit);
if (computerIndex > userIndex && war == true)
{
computerScore = computerScore + (attempts * 3);
userScore = userScore - (attempts * 3);
alert("I win. \n\nI have " + mathClampC() + " cards and you have " + mathClampU() + " cards.");
war = false;
}
else if (computerIndex < userIndex && war == true)
{
userScore = userScore + (attempts * 3);
computerScore = computerScore - (attempts * 3);
alert("You win. \n\nI have " + mathClampC() + " cards and you have " + mathClampU() + " cards.");
war = false;
}
else if (computerIndex == userIndex || war == true)
{
alert("TIE! TIME FOR WAR (AGAIN)! \n\nI have " + mathClampC() + " cards and you have " + mathClampU() + " cards")
attempts++;
if (computerIndex == userIndex)
{
war = true;
}
else
{
war = false;
}
}
}
}
if (computerScore >= 52)
{
alert("I WIN! GOOD GAME!");
}
else
{
alert("YOU WIN! GOOD GAME!")
}
</script>
推荐阅读
- typescript - 如何向 TS 指示参数上的每个属性都应映射到返回类型上的属性?
- python - 根据向量的颜色散点图
- python - 带有 Google Container Registry 的 Docker SDK
- asp.net-core - 如果在模型类中而不是在页面属性上,则会触发自定义验证
- python - 三元运算符在字典 for 循环中不起作用
- typescript - 在 Vue Router 延迟加载路由中坚持打字稿重命名过程
- caching - 在 GCP Memorystore 上获得大量“del”调用。但无法确定其来源
- c - 检查C中的不同字符
- python - PyQt5 模型/视图 - 一个模型,两个视图
- python - ValueError:形状必须为 2 级,但对于 'loss/dense_2_loss/Npairloss/MatMul'(操作:'MatMul')为 1 级,输入形状为:[?]、[?,10]