-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathExample.cs
62 lines (51 loc) · 1.65 KB
/
Example.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using System.Linq;
using UnityEngine;
namespace MFG.Bowling
{
internal class Example : MonoBehaviour
{
[SerializeField]
private bool perfectGame;
private Player[] players;
private void Start()
{
players = new[] {new Player("Hazza"), new Player("Sabina")};
Simulate();
}
private void Simulate()
{
for (var i = 0; i < players.Length; i++)
{
var player = players[i];
// Listen to the event for all the frames to be complete.
var framesCompleted = false;
player.OnFramesCompleted += () =>
{
framesCompleted = true;
HandlePlayerFramesCompleted(player);
};
// Track the current frames score so we know how many pins remaining can be knocked down.
var currentFramesScore = 0;
// Each time a frame is complete, reset the currentFramesScore.
player.OnFrameComplete += frame => currentFramesScore = 0;
// Simulate the game
while (!framesCompleted)
{
var max = (currentFramesScore < Constants.maxRollScore ? Constants.maxRollScore - currentFramesScore : Constants.maxRollScore) + 1;
var roll = perfectGame ? Constants.maxRollScore : Random.Range(0, max);
currentFramesScore += roll;
player.Roll(roll);
}
}
}
private static void HandlePlayerFramesCompleted(Player player)
{
foreach (var frame in player.Frames)
{
Debug.Log(frame.Roll.Aggregate("Frame:", (current, roll) => current + " / " + "roll: " + roll.Score) + (frame.BonusScore > 0 ? " / bonus: " + frame.BonusScore : "") + " - " +
frame.ScoreType + " - Total: " + frame.TotalScore);
}
Debug.Log(player.Name + " scored a total of " + player.TotalScore + " points");
}
}
}