2012-11-08 112 views
2

我正嘗試在java中創建一個棒球模擬遊戲。我正在利用「pitch」的實例作爲我係統的迭代。在此範圍內,我對結果有幾種不同的可能性。打,小姐(打),犯規球(沒有效果)。我從另一個班級創建了一系列玩家,這些玩家閱讀了我設計的玩家的特定屬性。我正在嘗試利用的唯一屬性是擊球手的力量和他們的一致性。我使用隨機數來產生一定的數值​​,並取決於數值的位置,決定它是一個球還是一個擊球。 '球'邏輯很簡單,有效運作;然而,我只接受擊球手的球數。我被困在如何實現罷工概率(錯過的揮杆)或關於球場是罷工的命中的邏輯。我使用的播放器的構造函數去如下在Java中創建複雜概率

Player a = new Player(false, false, true, 10, 20, 0.75, true, null); 

可以忽略虛假的和真實的和無效,只注重數量 第一個數(10)表示速度,而不是相關的。 第二個數字(20)表示功率。 第三個表示擊球員的一致性。

我很抱歉,如果這可能會令人困惑或太初級,我只在一個月內編程了一段時間。所有的幫助將不勝感激。目前唯一的印刷對我來說看起來有點像

Press 'p' to initiate pitches 
p 
Ball count is: (0,0) 
Ball count is: (0,0) 
Ball count is: (0,0) 
Ball count is: (0,0) 
Ball! 
Ball count is: (1,0) 
Ball count is: (1,0) 
Ball! 
Ball count is: (2,0) 
Ball count is: (2,0) 
Ball count is: (2,0) 
Ball! 
Ball count is: (3,0) 

我不明白爲什麼程序只承認球,沒有描述什麼是打印如無物,我以爲是界外球(但其不打印我的聲明)

請幫助,非常感謝你!

import java.util.Scanner; 
public class Game { 

public static void main(String[] args) { 
System.out.println("Press 'p' to initiate pitches"); 
Scanner kb = new Scanner(System.in); 
String s = kb.nextLine(); 
if(s.equalsIgnoreCase("p")) 
{ 
    int ball = 0; 
     int strike = 0; 
    //10 instances of pitches 
    for(int i = 0; i < 10; i++) 
    { 
    double number = Math.random(); 
    if(number > 0.5) 
    { 
     ball++; 
     if(ball == 4) 
     { 
     System.out.println("Ball four, take your base"); 
     break; 
     } 
     System.out.print("Ball!"); 
    } 
    else if(strike() == true) 
    { 
     { 
      if(isMiss() == true) 
     { 
      System.out.print(" Strike!"); 
      strike++; 
     } 
     else if(isFoul() == true) 
     { 
      System.out.println("Foul ball!"); 
     } 
     } 
     if(strike == 3) 
     { 
     System.out.print(" Player struck out!"); 
     break; 
     } 
      } 
    else 
    { 
     if(isHit() == true) 
     { 
     System.out.println("The ball was hit!"); 
     System.out.println(isHit()); 
     break; 
     } 
    } 
    System.out.println(" Ball count is: " + "(" + ball + "," + strike + ")"); 
    } 
} 
} 

public static boolean strike() 
{ 
if(isMiss() == true) 
{ 
     return true; 
} 
else if(isFoul() == true) 
{ 
     return false; 
} 
else if(isHit() == true) 
{ 
     return true; 
} 
return false; 
} 

public static boolean isHit() 
{ 
double probability = Math.random(); 
Player a = new Player(false, false, true, 10, 20, 0.75, true, null); 
if(a.power > 5) 
{ 
    if(a.consistency > 0.5) 
    { 
      if(probability > 3 && probability < 6) 
      { 
       return false; 
      } 
      if(probability > 6 && probability < 9) 
      { 
       return false; 
      } 
      if(probability > 9 && probability < 12) 
      { 
       return true; 
      } 
      return true; 
     } 
     System.out.println("The ball was hit!"); 
    } 
    return false; 
} 

public static boolean isMiss() 
{ 
    double probability = Math.random(); 
    Player a = new Player(false, false, true, 10, 20, 0.75, true, null); 
    if(a.power > 5) 
    { 
     if(a.consistency > 0.5) 
     { 
      if(probability > 3 && probability < 6) 
      { 
       return true; 
      } 
      if(probability > 6 && probability < 9) 
      { 
       return false; 
      } 
      if(probability > 9 && probability < 12) 
      { 
       return false; 
      } 
      return false; 
     } 
    } 
    return false; 
} 

public static boolean isFoul() 
{ 
    double probability = Math.random(); 
    Player a = new Player(false, false, true, 10, 20, 0.75, true, null); 
    if(a.power > 5) 
    { 
     if(a.consistency > 0.5) 
     { 
      if(probability > 3 && probability < 6) 
      { 
       return false; 
      } 
      if(probability > 6 && probability < 9) 
      { 
       return true; 
      } 
      if(probability > 9 && probability < 12) 
      { 
       return false; 
      } 
     } 

    } 
    return false; 
} 
+1

'概率> 6返回之間的數真的;'。應該是可能的你改變這個變量名稱。 – UmNyobe

+0

你的括號('{'和'}')不匹配。例如2x'{''在'else之後if(strike()== true)' –

回答

0

罷工:

if (Math.random() > consistency) { /* strike! */ } 

你可能需要一個恆定的界外球機會(比如,如果他們打的球,他們有犯規球有10%的機率:

else if (Math.random() < 0.1) { ... } 

同樣的權力,但也許乘以0.3(因爲本壘打是罕見的):

if (Math.random() < power * 0.3) { ... } 

請注意,對於這些工作,您的一致性和權力變量必須是小數。

例如,50%的一致性爲0.5。 20%的一致性是0.2。同樣,1是最大功率量,0非常弱。 0.5將介於兩者之間。

+0

因此,我是否像公共靜態final double FOUL_BALL = 0.1那樣實現常量? ? – user1808763

+0

是的,你可以做到這一點。你應該把變量的一致性和權力變成雙打。 – Doorknob

+0

如何在if語句的邏輯中實現math.random()>一致性? – user1808763

0

我建議你使用你的調試器來遍歷你的代碼,因爲有很多部分對我來說沒有意義,而且我也無法使用Player類來運行你的代碼(並且我無法將其作爲它的補充沒有意義)

節的代碼沒有意義是

double probability = Math.random(); 

所以概率爲[0,1)

if(probability > 3 && probability < 6) // always false. 
if(probability > 6 && probability < 9) // always false 
if(probability > 9 && probability < 12) // always false. 

// prints the ball was hit but returns `false` to isHit 
System.out.println("The ball was hit!"); 
} 
return false;