Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 30 additions & 7 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,21 @@ class _OddOneOutGameState extends State<OddOneOutGame> {
List<int> _numbers = [];
int _oddIndex = 0;
int _score = 0;
final String _rule = "Multiples of 4";
String _rule = "";

final List<MathRule> _rules = [
MathRule("Multiples of 4", (n) => n % 4 == 0),
MathRule("Prime numbers", (n) {
if (n < 2) return false;
for (int i = 2; i <= n ~/ 2; i++) {
if (n % i == 0) return false;
}
return true;
}),
MathRule("Even numbers", (n) => n % 2 == 0),
MathRule("Odd numbers", (n) => n % 2 != 0),
MathRule("Multiples of 3", (n) => n % 3 == 0),
];

@override
void initState() {
Expand All @@ -39,29 +53,31 @@ class _OddOneOutGameState extends State<OddOneOutGame> {
}

void _generateRound() {
Set<int> numsSet = {};
_oddIndex = _rand.nextInt(3);
MathRule currentRule = _rules[_rand.nextInt(_rules.length)];

Set<int> numsSet = {};
for (int i = 0; i < 3; i++) {
if (i == _oddIndex) {
// Generate a non-multiple of 4 that isn't already in set
// Generate a number that does NOT match the rule and is unique
int n;
do {
n = _rand.nextInt(50) + 1;
} while (n % 4 == 0 || numsSet.contains(n));
} while (currentRule.match(n) || numsSet.contains(n));
numsSet.add(n);
} else {
// Generate a multiple of 4 that isn't already in set
// Generate a number that matches the rule and is unique
int n;
do {
n = (_rand.nextInt(12) + 1) * 4;
} while (numsSet.contains(n));
n = _rand.nextInt(50) + 1;
} while (!currentRule.match(n) || numsSet.contains(n));
numsSet.add(n);
}
}

setState(() {
_numbers = numsSet.toList();
_rule = currentRule.description;
});
}

Expand Down Expand Up @@ -104,3 +120,10 @@ class _OddOneOutGameState extends State<OddOneOutGame> {
);
}
}

class MathRule {
final String description;
final bool Function(int) match;

MathRule(this.description, this.match);
}