递归计算保龄球得分

时间:2016-03-06 22:08:21

标签: java recursion

仅仅是为了练习,我试图以递归方式解决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());

}

}

2 个答案:

答案 0 :(得分:0)

不要在计算罢工或备件的块中使用递归,而只是分别添加下一个2帧或1帧的值。之后,您可以分别在index+2index+1进行递归调用。确保在您进行递归调用时,您将通过返回的内容递增分数。你在else区块中忘记了这一点。

答案 1 :(得分:0)

在我的一次工作面试中,我被要求实现一个ScoringBowling计算器。您可以在GitHub上找到我的解决方案:https://github.com/othmankurdi/ScoringBowling

我知道现在为时已晚,但可能会对其他人有所帮助:)