gpt4 book ai didi

java - 有两个玩家的骰子游戏,3 个骰子选择和卡片

转载 作者:行者123 更新时间:2023-12-02 03:21:09 25 4
gpt4 key购买 nike

我正在尝试为具有玩家和计算机掷骰子的游戏编写代码,直到其中一个或两者都达到 250(他们可能打成平手)。玩家和计算机可以从 3 个骰子选项中选择 1 个。单面 24 面的领带、2 面 10 面的骰子或 3 面 6 面的骰子。如果骰子都相同,则 10 面和 6 面骰子会有奖励。有 2 个“湖泊”,如果玩家降落在其中,玩家必须在湖泊开始之前回到较低的数字,还有一个泥泞的沼泽,玩家在沼泽中所做的每一个 Action 都会被切入一半。对于每 10 个点(10、20、30、40 等),玩家随机抽一张牌。玩家可以随机获得 11 种不同的卡片:

1-4:玩家从 1-6 随机前进

5:玩家从 4-11 随机向前移动(随机 8 + 4)

6:玩家移动到其他玩家所在的位置(见下文)

7:玩家回到起点(移动到位置 0)

8-9:玩家从 1-6 随机后退

10-11:玩家从 4-11 随机后退

我有几个问题。我的第一个问题是掷骰子不会在每一轮后都改变,它们会保持不变。因此,如果我选择 3 个骰子,我可能会得到 3 个随机数,如果我再次选择那些骰子,我将得到相同的 3 个数字。

我似乎也无法让玩家的死亡人数正确更新。如果玩家总共掷出 18 分,而下一轮他掷出 14 分,则计数将从 18 变为 14。

我的第三个问题是,无论我为湖泊、泥泞的补丁和获胜者公告做什么打印声明似乎总是打印出来。我尝试了一些不同的东西,但似乎没有任何效果。

我是代码编写新手(这是我编写的第四个程序),并且没有广泛的知识来知道什么是错误的。代码不必熟练地完成,我只是希望它能够正常工作。非常感谢任何和所有帮助。

  /*This program will create a "Board" game. Each player can choose 
from several different types of die. The computer and user will take
turns "rolling" a dice. There are several obstacles that can send one
of the players back. The goal is to get above 250*/

import java.util.*;

public class Project4 {

public static void main(String[] args) {
Scanner in=new Scanner(System.in);
//assigning variables
int p1, p2;
p1=p2=0;
int spacesmoved = 0;

//Setting up the randomization of the 24 sided die
int minimum1 = 1;
int maximum1 = 24;
Random rn1 = new Random();
int range1 = maximum1 - minimum1 + 1;
int die1 = rn1.nextInt(range1) + minimum1;

//Setting up the randomization of the 10 sided die
int minimum2 = 1;
int maximum2 = 10;
Random rn2 = new Random();
int range2 = maximum2 - minimum2+ 1;
int die2 = rn2.nextInt(range2) + minimum2;
int die22 = rn2.nextInt(range2) + minimum2;
int die222 = rn2.nextInt(range2) + minimum2;

//Setting up the randomization of the 6 sided die
int minimum3 = 1;
int maximum3 = 10;
Random rn3 = new Random();
int range3 = maximum3 - minimum3+ 1;
int die3 = rn3.nextInt(range3) + minimum3;
int die33 = rn3.nextInt(range3) + minimum3;
int die333 = rn3.nextInt(range3) + minimum3;

//Setting a loop for the players to take turns until one, or both, reach > 250
while (p1 <= 250 && p2 <= 250) {
{System.out.println(" Current positions. Player: " + p1 + " Computer: " + p2);
System.out.println("Which die would you like to roll? die1(1) = one 24-sided die, die2(2) = two 10-sided dice, die3(3) = three 6-sided dice: ");
String diechoice = in.nextLine().toLowerCase();

//Getting the die roll if the player chooses the 24 sided die
if (diechoice.equals ("1")) {
spacesmoved = (die1);
System.out.println("Player rolled a " + die1);
System.out.println("Player moves forward " + die1 +" spaces");
p1+=spacesmoved;

}

//Getting the die roll if the player chooses the two 10 sided die
if (diechoice.equals ("2")) { spacesmoved = (die2 + die22);
System.out.println("First die is " + die2);//TESTTTT
System.out.println("Second die is a " + die22);//TEST
System.out.println(die2 + die22);//TESTTTTtttt
if (die2 == die22); {
spacesmoved = (die2 + die22 + die222);
System.out.println("Player rolled doubles, player gets to roll a 3rd 10 sided die");
System.out.println("Players 3rd dice roll is " + die222);
System.out.println("Player moves forward a total of " + spacesmoved + " spots");
p1 += spacesmoved;
}
// player1spot = (currentspot + spacesmoved);
}

//Getting the die roll if the player chooses three 6 sided die
if (diechoice.equals("3")) { spacesmoved = (die3 + die33 + die333);
System.out.println("die 1 is " + die3);
System.out.println("die 2 is " + die33);
System.out.println("die 3 is " + die333);
System.out.println("Player 1 moves forward a total of " + spacesmoved + " spots");
{ if (die3 == die33)
if (die33 == die333)
spacesmoved = ( spacesmoved * 2);

p1 += spacesmoved;
}}
/*Setting up the lakes and muddy patch. If the player lands in a lake he goes back
to the lower edge of the lake. If in the mud his moves are cut in half ONLY while in the mud */

{if (spacesmoved >= (83) || spacesmoved <= (89)); spacesmoved = (82);
System.out.println("Player landed in a lake, player goes back to space " + spacesmoved);
if (spacesmoved >= (152) || spacesmoved <= (155)); spacesmoved = (151);
System.out.println("Player landed in a lake, player goes back to space " + spacesmoved);
if (spacesmoved >= (201) || spacesmoved <= (233)); spacesmoved = (spacesmoved / 2);
System.out.println("Player landed in mud, players turns are cut in half until player gets out");
}
//Setting up the random cards if the player lands on a 10
if (p1 % 10==0);
{ int minimum4 = 0;
int maximum4 = 11;
Random rn4 = new Random();
int range4 = maximum4 - minimum4 + 1;
int card = rn4.nextInt(range4) + minimum4;

//if player gets a card that moves them ahead a random number between 1-6
if (card >=4);
int minimum = 0;
int maximum = 6;
Random rn = new Random();
int range = maximum - minimum + 1;
int cardmove = rn.nextInt(range) + minimum;
p1 = cardmove;

//if player gets a card that moves them ahead a random number between 4-11
if (card == 5);
int minimum5 = 4;
int maximum5 = 11;
Random rn5 = new Random();
int range5 = maximum5 - minimum5 + 1;
int cardmove5 = rn5.nextInt(range5) + minimum5;
p1 = cardmove5;
//if player gets a card that moves them to the spot of the other player
if (card == 6);
p2 = p1;


//if player gets a card that moves them back to 0 (moves location to 0)
if (card ==7);
p1 = 0;

//if player gets a card that moves them back between 1-6 spaces
if (card == (8) || card == 9);
int minimum6 = 1;
int maximum6 = 6;
Random rn6 = new Random();
int range6 = maximum6 - minimum6 + 1;
int cardmove6 = rn6.nextInt(range6) + minimum6;


//if player gets a card that moves them back between 4-11 spaces
if (card == (10) || card == 11);
int minimum7 = 4;
int maximum7 = 11;
Random rn7 = new Random();
int range7 = maximum7 - minimum7 + 1;
int cardmove7 = rn7.nextInt(range7) + minimum7;
}
//Setting up the computers turn

System.out.println("Computers turn");
{
int minimum = 0;
int maximum = 2;
Random rn = new Random();
int range = maximum - minimum + 1;
int computersturn = rn.nextInt(range) + minimum;

//If computer randomly chooses a 24 sided die
spacesmoved = (die1);
System.out.println("Computer rolled a " + die1);
System.out.println("Computer moved " + die1 +" spaces");
p2+=spacesmoved;

}

//If the computer randomly chooses the two 10 sided die
if (diechoice.equals ("die2")) { spacesmoved = (die2 + die22);
System.out.println("First die is " + die2);//TESTTTT
System.out.println("Second die is a " + die22);//TEST
System.out.println(die2 + die22);//TESTTTTtttt
if (die2 == die22); {
spacesmoved = (die2 + die22 + die222);
System.out.println("Computer rolled doubles, player gets to roll a 3rd 10 sided die");
System.out.println("Computer 3rd dice roll is " + die222);
System.out.println("Computer moves a total of " + spacesmoved + " spots");
p2 += spacesmoved;
}

}

//If the computer randomly chooses three 6 sided die
if (diechoice.equals("die3")) { spacesmoved = (die3 + die33 + die333);
System.out.println("die 1 is " + die3);
System.out.println("die 2 is " + die33);
System.out.println("die 3 is " + die333);
System.out.println("Computer 1 moves a total of " + spacesmoved + " spots");
{ if (die3 == die33)
if (die33 == die333)
spacesmoved = ( spacesmoved * 2);

p2 += spacesmoved;


}




//Setting the lakes and mud for the computer

if (spacesmoved >= (83) || spacesmoved <= (89)); spacesmoved = (82);
System.out.println("Computer landed in a lake, player goes back to space " + spacesmoved);
if (spacesmoved >= (152) || spacesmoved <= (155)); spacesmoved = (151);
System.out.println("Computer landed in a lake, player goes back to space " + spacesmoved);
if (spacesmoved >= (201) || spacesmoved <= (233)); spacesmoved = (spacesmoved / 2);
System.out.println("Computer landed in mud, players turns are cut in half until player gets out");

//Setting up the cards for the computer
if (p1 % 10==0);
{ int minimum4 = 0;
int maximum4 = 11;
Random rn4 = new Random();
int range4 = maximum4 - minimum4 + 1;
int card = rn4.nextInt(range4) + minimum4;

//if computer gets a card that moves them ahead a random number between 1-6
if (card >=4);
int minimum = 0;
int maximum = 6;
Random rn = new Random();
int range = maximum - minimum + 1;
int cardmove = rn.nextInt(range) + minimum;

//if computer gets a card that moves them ahead a random number between 4-11
if (card == 5);
int minimum5 = 4;
int maximum5 = 11;
Random rn5 = new Random();
int range5 = maximum5 - minimum5 + 1;
int cardmove5 = rn5.nextInt(range5) + minimum5;

//if computer gets a card that moves them to the spot of the other player
if (card == 6);
p1 = p2;


//if computer gets a card that moves them back to 0 (moves location to 0)
if (card ==7);
p1 = 0;

//if computer gets a card that moves them back between 1-6 spaces
if (card == (8) || card == 9);
int minimum6 = 1;
int maximum6 = 6;
Random rn6 = new Random();
int range6 = maximum6 - minimum6 + 1;
int cardmove6 = rn6.nextInt(range6) + minimum6;


//if computer gets a card that moves them back between 4-11 spaces
if (card == (10) || card == 11);
int minimum7 = 4;
int maximum7 = 11;
Random rn7 = new Random();
int range7 = maximum7 - minimum7 + 1;
int cardmove7 = rn7.nextInt(range7) + minimum7;
}
}
//Writing a final statment showing the winner, or if both tied.
{ if (p1 > p2);
System.out.println("Player 1 wins! Good job!");

if (p2 >p1);
System.out.println("Computer wins! Better luck next time!");

if (p2 == p1);
System.out.println("The game ends in a tie!");
}
}

}




}

}

最佳答案

关于你提到的三个问题,我注意到以下几点:

问题一:

您在代码执行的最开始设置骰子的值。从那时起,您根本不会改变它们。这就是每回合总是滚动相同数字的问题的原因。您可能会想,每次使用 die1或任何其他模具变量,它正在重新执行文件顶部的代码,但它没有。

文件顶部的代码只执行一次,然后存储在该变量中的值将用于程序执行的其余部分。直到你改变它。所以你会想要更像这样的东西:

//Getting the die roll if the player chooses the 24 sided die
if (diechoice.equals ("1")) {
die1 = rn1.nextInt(range1) + minimum1;
System.out.println("Player rolled a " + die1);
System.out.println("Player moves forward " + die1 +" spaces");
p1+=die1;
}

在掷骰子的其他情况下,您还需要更改它。

这样做的另一个好处是您实际上只需要一个随机数生成器。您实际上并不需要每个模具一个。您可以对所有模具卷使用相同的模具。

问题二:

我不确定骰子到底出了什么问题,如果那里真的出了什么问题,但我确实注意到了一些你想要改变对 p1 和 p2 所做的事情的地方:
  • 当玩家得到一张让他们前进的牌时,你会想要使用 +=而不是 = .即p1 += cardmove5而不是 p1 = cardmove5
  • 当玩家拿到一张让他们后退的牌时,您似乎忘记添加 p1 -= cardmove陈述。
  • 此外,请确保您在正确的位置有 p1 和 p2。例如,我认为轮到电脑时,如果他们拿到牌将他们移动到其他玩家的位置,你的意思是 p2 = p1 , 但是你有 p1 = p2 .与回到 0 的计算机相同。您有 p1 = 0 ,但您似乎想要 p2 = 0 .所以请注意这一点。 (还要小心复制粘贴。我猜这就是发生这种情况的原因)

  • 问题3:

    这个问题看起来是由您使用 || 引起的。您应该使用的运算符 && .当您使用 || ,您实际上是在说“或”。所以这第一个声明
    if (spacesmoved >= (83) || spacesmoved <= (89))

    读作“如果空格移动大于或等于 83 或小于或等于 89”……请考虑一下。是否有任何数字不大于 83 或小于 89?答案是不。每个数字都会满足这个条件。你会想使用 && ,这意味着像这样的“和”:
    if (spacesmoved >= (83) && spacesmoved <= (89))

    “如果空格移动大于或等于 83 并且小于或等于 89”,这仅适用于 83 到 89 之间的数字。

    您还需要在该 block 和其他类似 block 中的“if”语句之后删除分号。如果不这样做,这些条件中的代码将不会被执行。当它发生时,这实际上是一个很难找到的错误。

    要知道的另一件事是,当您希望在“if”条件下执行多个操作时,必须将其括在大括号中 {} ,否则,只有第一行将包含在条件中,并且将无条件执行任何后续行。这是导致第三个问题的另一个事实。

    最后一件事是您应该尝试使用“else if”和“else”语句。它将帮助您的代码流更有意义。我不会为你做所有的工作,但这个代码块应该看起来更像这样:
    if (p1 >= (83) && p1 <= (89))
    {
    p1 = (82);
    System.out.println("Player landed in a lake, player goes back to space " + p1);
    }
    else if (p1 >= (152) && p1 <= (155))
    {
    p1 = (151);
    System.out.println("Player landed in a lake, player goes back to space " + p1);
    }
    else if (p1 >= (201) && p1 <= (233))
    {
    spacesmoved = (spacesmoved / 2);
    p1 -= spacesmoved;
    System.out.println("Player landed in mud, players turns are cut in half until player gets out");
    }

    奖金提示

    你学得很好,而且你似乎很好地考虑了代码流。只要继续工作和学习,你就会得到它。

    看看你对括号的使用。使用它们不会伤害任何东西,但是您使用它们的方式超出了您的需要。

    祝你好运!并继续学习!

    关于java - 有两个玩家的骰子游戏,3 个骰子选择和卡片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39582792/

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