2016-09-19 87 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"); 
} 

獎金提示

你學習好,看來你是思考代碼流很好。只要繼續工作和學習,你就會得到它。

看看你對括號的用法。使用它們並不會傷害任何東西,但是您使用它們的方式超過了您的需要。

祝你好運!並繼續學習!