Skip to content

Instantly share code, notes, and snippets.

@michaelee
Created September 27, 2023 18:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaelee/39392475ba07eb9deb668271b9da5f03 to your computer and use it in GitHub Desktop.
Save michaelee/39392475ba07eb9deb668271b9da5f03 to your computer and use it in GitHub Desktop.
periwinkle-branch-3225

periwinkle-branch-3225

Created with <3 with dartpad.dev.

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