【问题标题】:Dice game with two players, 3 die choices and cards有两个玩家的骰子游戏,3 个骰子选择和卡片
【发布时间】:2016-09-19 22:01:52
【问题描述】:

我正在尝试为具有玩家和计算机掷骰子的游戏编写代码,直到其中一个或两者达到 250(他们可能打成平手)。玩家和计算机可以从 3 个骰子选项中选择 1 个。单面 24 面的领带、2 面 10 面的骰子或 3 面 6 面的骰子。如果骰子都相同,则 10 面和 6 面骰子会有奖励。有 2 个“湖泊”,如果玩家降落在其中,玩家必须在湖泊开始之前回到较低的数字,还有一个泥泞的沼泽,玩家在沼泽中所做的每一个动作都会被切入一半。对于每 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!");    
       }
    }

    }




}

}

【问题讨论】:

  • 一次只问一个问题

标签: java dice


【解决方案1】:

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

问题 1:

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

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

//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;
}

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

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

问题 2:

我不确定掷骰子到底出了什么问题,如果那里真的出了什么问题,但我确实注意到了一些你想要改变对 p1 和 p2 所做的事情的地方:

  • 当玩家得到一张可以推动他们前进的牌时,您需要使用+= 而不是=。即p1 += cardmove5 而不是p1 = cardmove5
  • 当玩家拿到一张让他们后退的卡片时,您似乎忘记添加 p1 -= cardmove 语句。
  • 另外,请确保 p1 和 p2 在正确的位置。例如,我在想,轮到电脑时,如果他们拿到牌将他们移动到其他玩家的位置,你打算做p2 = p1,但你却有p1 = p2。计算机回到 0 时也是如此。你有 p1 = 0,但似乎你想要 p2 = 0。所以请注意这一点。 (还要小心复制粘贴。我猜这就是发生这种情况的原因)

问题 3:

这个问题看起来是因为您使用了|| 运算符,而您应该使用&amp;&amp;。当您使用|| 时,您实际上是在说“或”。所以这是第一个声明

if (spacesmoved >= (83) || spacesmoved <= (89))

读作“如果空格移动大于或等于 83 OR 小于或等于 89”... 想一想。是否有任何数字不大于 83 或小于 89?答案是不。每个数字都会满足这个条件。你会想使用&amp;&amp;,这意味着“和”像这样:

if (spacesmoved >= (83) && spacesmoved <= (89))

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

您还需要删除该块和其他类似块中“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");
}

奖金提示

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

查看您对括号的使用情况。使用它们不会造成任何伤害,但您使用它们的次数超出了您的需要。

祝你好运!并继续学习!

【讨论】:

    猜你喜欢
    • 2013-12-29
    • 2014-03-16
    • 2014-02-12
    • 2013-06-05
    • 2018-10-19
    • 1970-01-01
    • 2012-02-29
    • 2013-09-17
    • 1970-01-01
    相关资源
    最近更新 更多