2017-01-02 122 views
1

我有一個Java程序,計算機將決定哪個玩家先行,並從數組中選擇隨機數字。當玩家刪除數字時,數組會減少。所有這些都將使用隨機完成。但是當我運行我的代碼時,它不是我期望的那樣。我可以知道我哪裏錯了嗎?使用隨機數字的Java遊戲

這是我的代碼:

[的Class]

public class StickBag { 

    private int numOfSticks; 

    public StickBag(int numOfSticks) 
    { 
     this.numOfSticks = numOfSticks; 
    } 

    public int getNumOfSticks() { 
     return numOfSticks; 
    } 

    public void setNumOfSticks(int numOfSticks) { 
     this.numOfSticks = numOfSticks; 
    } 

    public int remove(int n) 
    { 
     numOfSticks = numOfSticks - n; 
     return numOfSticks; 
    } 
} 

[主要]

public class StickGameApp { 

    public static void main(String[] args) { 

     StickBag s1 = new StickBag(25); 

     System.out.println("Welcome to the game of sticks!"); 
     System.out.println("There are initially " + s1.getNumOfSticks() + " sticks on the board."); 

     int minP = 1; 
     int maxP = 2; 
     int randP; 

     int minN = 1; 
     int maxN = 10; 
     int randNum; 

     randP = minP + (int)(Math.random()*(maxP)); 
     randNum = minN + (int)(Math.random()*(maxN)); 

     for (int i=0; i<10; i++) 
     { 
      if(s1.getNumOfSticks() > randNum) 
      { 
      System.out.println("Computer player " + randP + " choose " + randNum + " sticks "); 
      s1.remove(randNum); 
      } 
      else 
      { 
       System.out.println("Computer player " + randP + " wants to choose " + randNum + " sticks but is unable to."); 
       System.out.println("Computer player " + randP + " loses "); 
      } 
     } 

    } 
} 

當運行的代碼,它顯示如下:


歡迎來到st遊戲icks!

板上最初有25根支桿。

電腦玩家2選擇6支

電腦玩家2選擇6支

電腦玩家2選擇6支

電腦玩家2選擇6支

電腦玩家2希望選擇6支,但無法。 電腦播放器2丟失

電腦播放器2想要選擇6支,但無法。 電腦播放器2丟失

電腦播放器2想要選擇6支,但無法。 電腦播放器2丟失

電腦播放器2想要選擇6支,但無法。 電腦播放器2丟失

電腦播放器2想要選擇6支,但無法。 電腦播放器2丟失

電腦播放器2想要選擇6支,但無法。 電腦玩家2輸


但我希望它顯示是這樣的:


歡迎棒的遊戲!

板上最初有25根支桿。

電腦播放器1選擇5支。

電腦播放器2選擇7支。

電腦播放器1選擇7支。

電腦玩家2想要選擇7支,但無法。

電腦玩家2,你輸了。


我可以知道我哪裏錯了嗎?

+0

1.您的播放器在for循環之外被選中。因此,曾經選擇過的玩家永遠不會改變。 2.無論遊戲結束與否,你都可以定義一個10的迭代。你需要根據我猜的棍子的數量來確定循環的結束。 3.如果您使用隨機,則無法確定結果(您希望顯示的結果)。 – kism3t

+0

謝謝你指出我的錯誤。我相當新的Java,但現在我明白了爲什麼。欣賞它 –

回答

1

你的錯誤僅僅是你在運行遊戲的for循環之前放置了播放器和計算機的隨機數發生器randPrandNum,因此隨機數選擇將只執行一次。

你的代碼應該是:

for (int i=0; i<10; i++) 
    { 
    randP = minP + (int)(Math.random()*(maxP)); 
    randNum = minN + (int)(Math.random()*(maxN)); 
     if(s1.getNumOfSticks() > randNum) 
     { 
     System.out.println("Computer player " + randP + " choose " + randNum + " sticks "); 
     s1.remove(randNum); 
     } 
     else 
     { 
      System.out.println("Computer player " + randP + " wants to choose " + randNum + " sticks but is unable to."); 
      System.out.println("Computer player " + randP + " loses "); 
     } 
    } 

} 
+0

不用客氣。 – Oghli

0

一件事,你選擇the.player.outside the.loop,所以無論選擇將始終運行所有回合

+0

非常感謝您指出我的錯誤。實際上,我對Java很陌生。 –

0

您將需要移動的這兩行代碼:

randP = minP + (int)(Math.random()*(maxP)); 
randNum = minN + (int)(Math.random()*(maxN)); 

for循環的開頭。這將確保在每次迭代時,您都可以選擇新玩家和新陣列位置。

+0

謝謝你的解釋。欣賞它。 –

0

我不能肯定這是否是正確的做法,以你的遊戲,但我希望你覺得它有用。

import java.util.*; 

public class Main { 

public static void main(String[] args) { 

    Random rand = new Random(); 
    StickBag s1 = new StickBag(25); 
    int total= s1.getNumOfSticks(); 

    System.out.println("Welcome to the game of sticks!"); 
    System.out.println("There are initially " + s1.getNumOfSticks() + " sticks on the board."); 

    int randP = 0; 
    int randNum; 
    int P1=0,P2=0; 


    for (int i=0; i<10; i++) 
    { 

      randP = rand.nextInt(2)+1; 
     randNum = rand.nextInt(10)+1; 


     if (s1.getNumOfSticks() - randNum > 0) 
     { 
     System.out.println("\n Computer player #" + randP + " chooses " + randNum + " sticks and takes them. "); 
     s1.remove(randNum); 

     System.out.println(" Computer player #" + (!(randP == 2) ? 2 : 1) + " wants to choose " + randNum + " sticks but is unable to."); 

     // Evaluate the score 
     if (randP == 1) 
      randP = (P1 = P1 + randNum); 
     else 
      randP = (P2 = P2 + randNum); 
     } 
    } 

    System.out.println("\n\n---------------------------------------------------------------------------"); 
    System.out.println("\t Player #1 has " + P1 + " sticks and Player #2 has " + P2 + " sticks. \n" + ((P1 > P2) ? "Player 1 wins" : "Player 2 wins")); 
    System.out.println("The sticks remaining in the bag are: " + s1.getNumOfSticks());}} 
+0

哇!這對我確實很有用。謝謝! –