2016-09-19 112 views
0

我想写一个游戏,有一个球员和一个电脑掷骰子的代码,直到一个或两个,达到250(它可能为他们配合)。玩家和电脑可以从3种模具选择中选择1种。一个 - 24面的领带,两个 - 10面的模具,或三 - 6面的模具。如果模具完全相同,则10和6面模具会有一个奖励。有2个“湖泊”,如果玩家登陆其中,玩家必须在湖的开始之前回到较低的数字,那里还有一个泥泞的沼泽,玩家在沼泽中进行的每一个动作都被切入半。每10个景点(10,20,30,40等),玩家随机抽取一张卡片。有11张不同的卡的玩家可以随机得到:骰子游戏与两个玩家,3个骰子选项和卡片

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.

我的第三个问题看起来好像不管我为湖泊打印声明,泥泞的补丁和赢家公告总是打印。我尝试了一些不同的东西,似乎没有任何工作。

我是代码编写新手(这是我编写的第4个程序),没有丰富的知识来了解错误。代码不一定要熟练完成,我只是希望它能正常工作。任何和所有的帮助,不胜感激。

/*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

一次提出一个问题 –

回答

2

下面是我关于注意到你提到的三个问题的事情:

问题数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:

这个问题看起来像它的事实,您正在使用的||算哪里造成的你应该使用&&。当您使用||时,您实际上是在说“或”。所以这第一条语句

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

读作“如果spacesmoved大于或等于83 OR小于或等于89” ......想想看,一个第二。有没有不大于83或小于89的数字?答案是不。每个号码都能满足这个条件。您可能需要使用&&,意思是“和”像这样:

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

“如果spacesmoved大于或等于83 小于或等于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"); 
} 

奖金提示

你学习好,看来你是思考代码流很好。只要继续工作和学习,你就会得到它。

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

祝你好运!并继续学习!