每次我运行程序,相同的数字为骰子出现。此外,我的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)
<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();
有人能在这里给我指个正确的方向吗?我的游戏工作完美,但我想添加一些实际的互动/目标。谢谢
rollDice( )应使用兰德( )随机生成介于1-6之间的数字 返回兰德( )生成的数字 3)实现一个功能,功能原型为Int ;Playgame( Void ;); 根据用户的赢或输的次数给用户一个适当的消息 返回值为EXIT_SUCCESS 这里是我现在拥有的,但它告诉我有错误。有谁能帮我完成这个任务吗? ^ X.C:10:1:错误:程序中杂散“\240” X.C:12:1:错误:程序中杂
我是一个C++初学者,我需要创建一个骰子游戏模拟掷两个骰子。我对头文件的使用感到很困惑。但首先,为什么我需要返回骰子的票面号码?其次,int roll函数做什么?来重置价值观和面孔?如果是,默认值是多少?而最后一个函数骰子(int n),我是否使用这个函数来控制骰子值的最大总和?函数必须有一个具有以下函数的类头文件:
我正在创建一个骰子游戏,其中2个玩家有他们自己的骰子,每个人都有自己的轮到扔他的骰子,玩家可以完全失去分数或获得分数取决于他们站在窗口的位置,游戏结束时,任何一个玩家首先站在终点车道上,获胜者是得分最高的玩家,我已经在游戏的设计到目前为止,但还没有在逻辑上工作。 这个链接是游戏应该是什么样子的图片: 我想知道我如何才能添加球员1和球员2在每个瓷砖上面的照片,所以基本上每次当一个球员玩他的回合,我希
该程序应模拟滚动两个骰子,并计算和。添加一个循环,使程序可以玩10,000个游戏。添加计数器,计算玩家赢了多少次,输了多少次。在10,000场比赛结束时,计算获胜的概率[即赢/(赢+输)]并输出该值。从长远来看,谁会赢得最多的比赛,你还是房子?注意:要生成一个随机数x,其中0x≤<1,使用x=math.random();例如,乘以6并转换为一个整数,结果是一个介于0和5之间的整数。
我试图为一个游戏的掷骰子程序,其中用户输入一个下注金额和2个六面骰子滚动,如果7或11是滚动,他们赢了。如果掷2,3或12就输了,如果掷其他任何一个数字,它就显示该数字为一个点。它会继续掷骰子,直到掷7或该点为止,如果掷7就输了,否则就赢了。出于某种原因,在掷骰子时,他会掷骰子,然后再掷一次,看看他们是否赢了或输了。我不知道如何解决这个问题,任何帮助都不会附带