Created with <3 with dartpad.dev.
Created
September 27, 2023 18:10
-
-
Save michaelee/39392475ba07eb9deb668271b9da5f03 to your computer and use it in GitHub Desktop.
periwinkle-branch-3225
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:collection/collection.dart'; | |
import 'dart:math'; | |
class Dice { | |
final List<int?> _values; | |
final List<bool> _held; | |
Dice(int numDice) | |
: _values = List<int?>.filled(numDice, null), | |
_held = List<bool>.filled(numDice, false); | |
List<int> get values => List<int>.unmodifiable(_values.whereNotNull()); | |
int? operator [](int index) => _values[index]; | |
bool isHeld(int index) => _held[index]; | |
void clear() { | |
for (var i = 0; i < _values.length; i++) { | |
_values[i] = null; | |
_held[i] = false; | |
} | |
} | |
void roll() { | |
for (var i = 0; i < _values.length; i++) { | |
if (!_held[i]) { | |
_values[i] = Random().nextInt(6) + 1; | |
} | |
} | |
} | |
void toggleHold(int index) { | |
_held[index] = !_held[index]; | |
} | |
} | |
void main() { | |
var dice = Dice(5); | |
dice.roll(); | |
print(dice.values); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment