2016-03-06 111 views
0

只是为了练习,我试图用递归方式解决java中的保龄球问题,我已经解决了这个问题,但是在非递归解决方案中,但我觉得评分部分非常适合递归。以下是我的试验,我确信它不是很准确,但这就是我对这个问题的看法。递归计算保龄球得分

import java.util.Scanner; 

public class Bowling 
{ 

int [] game; 
Scanner s ; 

public Bowling() 
{ 
    s= new Scanner (System.in); 
    game = new int[21]; 

} 

public void playGame() 
{ 
    int pins = 0; 
    String input =""; 
    boolean strickOrSpare =false; 

    for (int i = 0; i < 10 ; i++) 
    { 
     strickOrSpare =false; 
     System.out.println("You are in frame "+(i+1)+ "in the first throw, please enter the number of pins"); 
     input = s.nextLine(); 
     pins = Integer.parseInt(input);  
     game[i*2] = pins; 

     while((pins > 10) || (pins <0)) 
     { 
      System.out.println("You entered invalid number, please enter the number of pins for first throw"); 
      input = s.nextLine(); 
      pins = Integer.parseInt(input);    
      game[i*2] = pins; 
     } 

     if(pins == 10) 
     { 
      System.out.println("A Strik!"); 
      strickOrSpare =true; 
      if (i != 9) 
       continue; 
     } 

     //////////////////////////////////////////////second throw//////////////////////////////////////////////////////////////////// 

     System.out.println("Please enter the number of pins for second throw"); 
     input = s.nextLine(); 
     pins = Integer.parseInt(input); 
     game[(i*2)+1] = pins; 

     while(((game[i*2] + game[(i*2)+1] > 10) || (game[i*2]+ game[(i*2)+1] < 0)) && i!=9) 
     { 
      System.out.println("You entered invalid number, please enter the number of pins for second throw"); 
      input = s.nextLine(); 
      pins = Integer.parseInt(input);    
      game[(i*2)+1] = pins; 
     } 

     if(game[i*2] + game[(i*2)+1] == 10) 
     { 
      strickOrSpare =true; 
      System.out.println("A Spare!"); 
     } 

     ////////////////////////////////////////////////Last Frame case////////////////////////////////////////////////////// 

     if(i == 9) 
     {  
      if(strickOrSpare) 
      { 
       System.out.println("Please enter the number of pins for third throw"); 
       input = s.nextLine(); 
       pins = Integer.parseInt(input); 
       game[(i*2)+2] = pins; 
      } 
     } 
    } 
} 

public int calculateScore() 
{ 
    return helperCalculateScore(0, 0); 
} 

private int helperCalculateScore(int index, int scoreSoFar) 
{ 
    if(index == 18) 
    { 
     if (game[index] ==10) 
      scoreSoFar = scoreSoFar+ 10 + game[19] + game[20]; 
     else if (game[index] + game[index+1 ]==10) 
      scoreSoFar = scoreSoFar+ 10 + game[20]; 
     else 
      scoreSoFar = scoreSoFar+ game[18] + game[19]; 
    } 
    else if((index%2 == 0) && (game[index]==10)) //strik 
    { 
     scoreSoFar = scoreSoFar+ 10 + helperCalculateScore (index+1, scoreSoFar) + helperCalculateScore (index+2, scoreSoFar); 
    } 
    else if((index%2 == 1) && (game[index] + game[index-1] ==10)) //spare 
    { 
     scoreSoFar = scoreSoFar+ 10 + helperCalculateScore (index+1, scoreSoFar); 
    } 
    else 
    { 
     scoreSoFar = scoreSoFar+ game[index]; 
     helperCalculateScore (index+1, scoreSoFar); 
    } 

    return scoreSoFar; 
} 

public static void main(String[] args) 
{ 
    // TODO Auto-generated method stub 
    Bowling b = new Bowling(); 
    b.playGame(); 
System.out.println(b.calculateScore()); 

} 

} 
+0

你的问题是什么? –

+0

这个问题的最佳解决方案是在游戏进行时增加分数。你必须确保在一次罢工之后或者在未来的2/1帧内得分翻倍。而第十帧的逻辑更多一点。 – 4castle

回答

0

不要在计算罢工或备用块使用递归,而不是只对值分别增加从未来2个或1帧。之后,您可以分别在index+2index+1上进行递归调用。确保每次进行递归调用时,都会根据返回的结果增加分数。你忘记了你的else区块。