2012-11-08 111 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;