Dojo #29: Boliche Exemplo de uso de Tabela de Decisões para o Kata Boliche realizado em 19 de agosto de 2009 na Reunião #29 do Dojo Rio
1. Problema

Create a program, which, given a valid sequence of rolls for one line of American Ten-Pin Bowling, produces the total score for the game. Here are some things that the program will not do:

  • We will not check for valid rolls.
  • We will not check for correct number of rolls and frames.
  • We will not provide scores for intermediate frames.

Depending on the application, this might or might not be a valid way to define a complete story, but we do it here for purposes of keeping the kata light. I think you'll see that improvements like those above would go in readily if they were needed.

We can briefly summarize the scoring for this form of bowling:

  • Each game, or "line" of bowling, includes ten turns, or "frames" for the bowler.
  • In each frame, the bowler gets up to two tries to knock down all the pins.
  • If in two tries, he fails to knock them all down, his score for that frame is the total number of pins knocked down in his two tries.
  • If in two tries he knocks them all down, this is called a "spare" and his score for the frame is ten plus the number of pins knocked down on his next throw (in his next turn).
  • If on his first try in the frame he knocks down all the pins, this is called a "strike". His turn is over, and his score for the frame is ten plus the simple total of the pins knocked down in his next two rolls.
  • If he gets a spare or strike in the last (tenth) frame, the bowler gets to throw one or two more bonus balls, respectively. These bonus throws are taken as part of the same turn. If the bonus throws knock down all the pins, the process does not repeat: the bonus throws are only used to calculate the score of the final frame.
  • The game score is the total of all frame scores.

More info on the rules

2. Soluçãototal = 0
repete (rodada = 1 a 12)
total += pontos(rodada) * qtd(rodada)
3. Detalhes

qtd = vetor indicando quantas vezes os pontos desta rodada serão contados, inicialmente = 1. Se houver:

  • spare na rodada i soma 1 em qtd(i + 1);
  • strike na rodada i soma 1 em qtd(i + 1) e em qtd(i + 2)
pontos = vetor com a quantidade de pontos de cada rodada
4. Definição usando Tabela de Decisões Repetir para cada rodada:
Foi strike?
| Foi spare?
1 . * somar 10 e as duas rodada seguintes
. 1 * somar 10 e a rodada seguinte
0 0 * somar pontuação da rodada
5. Outra Soluçãototal = 0
repete (rodada = 1 a 10)
total += pontos(rodada)
se tipo(rodada) <> normal: total += pontos(rodada + 1)
se tipo(rodada) = strike: total += pontos(rodada + 2)
6. Detalhestipo = vetor indicando se:
  • foi strike,
  • spare ou
  • não houve premiação
pontos = vetor com a quantidade de pontos de cada rodada
Arquivo origem: DojoBoliche.xml.