changeSight function
- SightExercise ex,
- List<
SightEvent> written, - SightChange change,
- Random rng,
Aplica uma mudança de change a written. null quando não há nota onde
aplicá-la (por exemplo, accidental sem nenhuma nota alterada pela
armadura).
Implementation
SightChanged? changeSight(
SightExercise ex,
List<SightEvent> written,
SightChange change,
Random rng,
) {
var spots = _spots(ex, written, change);
if (change == SightChange.rhythm) {
// Dividir e juntar com a mesma chance. Se o sorteado não cabe em lugar
// nenhum, os lugares que sobram são todos do outro tipo.
final join = rng.nextBool();
final preferred = [
for (final s in spots)
if ((s.partner != null) == join) s,
];
if (preferred.isNotEmpty) spots = preferred;
}
if (spots.isEmpty) return null;
// Primeiro o compasso, depois a nota dentro dele: os quatro compassos saem
// com chance parecida, mesmo que um tenha mais notas.
final measures = {for (final s in spots) s.measure}.toList()..sort();
final measure = measures[rng.nextInt(measures.length)];
final inMeasure = [
for (final s in spots)
if (s.measure == measure) s,
];
final spot = inMeasure[rng.nextInt(inMeasure.length)];
return switch (change) {
SightChange.pitch => _pitch(ex, written, spot.index, rng),
SightChange.accidental => _accidental(ex, written, spot.index),
SightChange.rhythm =>
spot.partner == null
? _divide(written, spot.index)
: _join(written, spot.index, spot.partner!),
};
}