Hi, guys!
I’m passing now Bowl Master section, and game is almost finished (I’ve passed to the Epic TDD challenge) but I still have one serious problem.
It is appeared few lectures before, when I did the Bowl method in ActionMaster script, which automatically controls all these Tidy and Reset actions.
I’ve unsuccessfully tried to solve it and finally decided to go forward, expecting to find a solution later, but I didn’t find it.
Problem is that after I do the very first bowl, I get an EndGame statement.
Spoiler does Tidy, Tidy, Reset then EndGame (which is for now just a message in console). It looks like game goes through all the bowls from 1 to 21 and gives me EndGame statement in the end.
I copied the code of ActionMaster from the ActionMasterOld script from the added to the course archive, but problem is still there. So I ask you guys for help. Do you have any ideas where is the problem?
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 am = new ActionMaster();
Action currentAction = new Action();
foreach (int pinFall in pinFalls)
{
currentAction = am.Bowl(pinFall);
}
return currentAction;
}
private Action Bowl(int pins)
{
if (pins < 0 || pins > 10) { throw new UnityException("Invalid pins"); }
bowls[bowl - 1] = pins;
Debug.Log(bowl);
if (bowl == 21)
{
return Action.EndGame;
}
// Handle last-frame special cases
if (bowl >= 19 && pins == 10)
{
bowl++;
return Action.Reset;
}
else if (bowl == 20)
{
bowl++;
if (bowls[19 - 1] == 10 && bowls[20 - 1] == 0)
{
return Action.Tidy;
}
else if (bowls[19 - 1] + bowls[20 - 1] == 10)
{
return Action.Reset;
}
else if (Ball21Awarded())
{
return Action.Tidy;
}
else
{
return Action.EndGame;
}
}
if (bowl % 2 != 0)
{ // First bowl of frame
if (pins == 10)
{
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 Ball21Awarded()
{
return (bowls [19 - 1] + bowls[20 - 1] >= 10);
}
}