Bowlmaster: Failing Tests Challenge -- My Solution

Hi,

I think I made a much cleaner way to trigger Tidy if the 19th shot is a strike, and the 20th shot is not. Not sure if this is the most optimized way to do this but I think it’s much easier to read than the solution in the video :slight_smile:

//Last frame special cases
if(bowl >= 19 && Bowl21Awarded()) {
    if(bowl > 19 && bowls[19-1] == 10 && bowls[20-1] < 10) { //Strike at 19, Not at 20
        bowl += 1;
        return Action.Tidy;
    }
    bowl += 1;
    return Action.Reset;
} else if(bowl == 20 && !Bowl21Awarded()) {
    return Action.EndGame;
}

Please do reply if this gets failures on further tests :slight_smile:

Cheers!

It worked on all the tests I have. (You’ve got another piece in there to EndGame on Bowl21, right?)

Thanks for testing! :slight_smile: Sorry, I didn’t post the whole code. But yes, I did have code to end on Bowl 21. Here’s the snippet of the whole thing:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ActionMaster {

   public enum Action { Tidy, Reset, EndTurn, EndGame };

   private int[] bowls = new int[21];
   private int bowl = 1;

   public static Action NextAction(List<int> pinFalls) {
       ActionMaster actionMaster = new ActionMaster();
       Action currentAction = new Action();

       foreach(int pinFall in pinFalls) {
           currentAction = actionMaster.Bowl(pinFall);
       }
       return currentAction;
   }

   public Action Bowl(int pins) { //TODO make private
       //Check pin validity
       if(pins < 0 || pins > 10) {
           throw new UnityException("Invalid pin count.");
       }

       bowls[bowl - 1] = pins;

       //Very last frame
       if(bowl == 21) {
           return Action.EndGame;
       }

       //Last frame special cases
       if(bowl >= 19 && Bowl21Awarded()) {
           if(bowl > 19 && bowls[19-1] == 10 && bowls[20-1] < 10) { //Strike at 19, Not at 20
               bowl += 1;
               return Action.Tidy;
           }
           bowl += 1;
           return Action.Reset;
       } else if(bowl == 20 && !Bowl21Awarded()) {
           return Action.EndGame;
       }
       

       //Normal bowl cases
       if(bowl % 2 != 0) { //First bowl of frame
           if(pins == 10) { //Strike
               bowl += 2;
               return Action.EndTurn;
           } else {
               bowl += 1;
               return Action.Tidy;
           }
       } else if (bowl % 2 == 0) { //Second bowl of frame
           bowl += 1;
           return Action.EndTurn;
       }

       throw new UnityException("Not sure what action to return.");
   }

   private bool Bowl21Awarded() {
       return (bowls[19-1] + bowls [20-1] >= 10);
   }
}

Privacy & Terms