骰子游戏有两个玩家,3个骰子选择和卡

时间:2016-09-19 22:01:53

标签: java dice

我正在尝试为具有播放器和计算机掷骰子的游戏编写代码,直到其中一个或两个达到250(这可能使他们打成平手)。玩家和计算机可以从3种模具中选择1种。一个 - 24面双面打结,两个 - 10面双面打印,或三 - 六面模具。如果模具全部相同,则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!");    
       }
    }

    }




}

}

1 个答案:

答案 0 :(得分:2)

以下是我提到的与你提到的三个问题有关的事情:

问题编号1:

您正在设置代码执行开始时的骰子值。从那时起,你根本就没有改变它们。这就是每回合总是滚动相同数字的问题的原因。您可能会认为每次使用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;
}

在模具滚动的其他情况下,您还需要更改它。

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

问题2:

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

  • 当玩家获得一张可以向前移动的卡时,您会想要使用+=代替=。即p1 += cardmove5而不是p1 = cardmove5
  • 当玩家获得一张可以将其移回的卡时,您似乎忘记添加p1 -= cardmove语句。
  • 另外,请确保在正确的位置有p1和p2。例如,我想在计算机上,如果他们将卡片移到另一个玩家的位置,你打算做p2 = p1,而是你有p1 = p2。与计算机返回0相同。您有p1 = 0,但似乎您需要p2 = 0。所以要小心一点。 (还要注意复制粘贴。我猜这就是为什么会发生这种情况)

问题3:

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

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

读为&#34;如果spacemoved大于或等于83 OR 小于或等于89&#34; ...想一想。是否有任何数字不大于83或小于89?答案是不。每个数字都会满足这个条件。您可能希望使用&&,这意味着&#34;和&#34;像这样:

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

&#34;如果spacemoved大于或等于83 AND 小于或等于89&#34;,这只适用于83到89之间的数字。

您还需要在&#34; if&#34;之后删除分号。该块中的语句和其他类似的块。如果你不这样做,那些条件中的代码就不会被执行。当它发生时,这确实是一个非常难以找到的错误。

另一件要知道的事情是,当你想要在&#34; if&#34;条件,您必须将它括在花括号{}中,否则,只有第一行将包含在条件中,并且任何后续行将无条件执行。这是导致第三个问题的另一个事实。

最后一件事是你应该尝试使用&#34;否则如果&#34;和&#34;否则&#34;声明。它将帮助您的代码流更有意义。我不打算为你做所有的工作,但是这个代码块应该看起来更像这样:

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");
}

奖金提示

你学得很好,似乎你正在考虑代码流程。只要继续工作和学习,你就能得到它。

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

祝你好运!继续学习!

相关问题