Skip to content

Commit

Permalink
Better Loader
Browse files Browse the repository at this point in the history
The `determinePercentage` function was mostly improved, but it might be better to just check the nimber value of each child. Nimber values are distinctly different from percentages though, so I'll keep the current code for now.
  • Loading branch information
PeculiarProgrammer committed Dec 19, 2024
1 parent 4e68c0f commit 1fa262a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 15 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ Note: Although this is advertised as AI, it is not. It's simply a semi-optimized
- [ ] Add more dictionary options
- [ ] Implement tests
- [x] Improve efficiency of solving algorithm
- [ ] Improve efficiency of `determinePercentage` and similar
- [x] Improve efficiency of `determinePercentage` and similar

If you can think of any more, please leave an issue.
2 changes: 1 addition & 1 deletion lib/algorithm.dart
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ double determinePercentage(
return good / count;
}

// This is a slightly modified version of the trie implementation from retrieval (10x faster than the original for this use case)
// This is a slightly modified version of the trie implementation from retrieval (10x faster than the original evaluation, 6x overall)
class Trie {
final root = TrieNode<void>(key: null, value: null);

Expand Down
50 changes: 37 additions & 13 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -367,10 +367,7 @@ class _AlgorithmShowerState extends State<AlgorithmShower> {
case ConnectionState.none:
return const SizedBox();
case ConnectionState.waiting:
return const Padding(
padding: EdgeInsets.symmetric(vertical: 16.0),
child: CircularProgressIndicator(),
);
return primaryInterface(context, isLoading: true);
case ConnectionState.active:
return const SizedBox();
case ConnectionState.done:
Expand All @@ -384,12 +381,15 @@ class _AlgorithmShowerState extends State<AlgorithmShower> {
snapshot.data![1] as List<MapEntry<String, double>>;
}

return loadedInterface(context);
return primaryInterface(context);
}
});
}

RenderObjectWidget loadedInterface(BuildContext context) {
RenderObjectWidget primaryInterface(BuildContext context,
{bool isLoading = false}) {
// Note: optimalGame and sortedLetters can **only** be used when loading is false.

if (widget.dictionaryTrie.has(widget.path)) {
return const Padding(
padding: EdgeInsets.symmetric(horizontal: 16.0, vertical: 12.0),
Expand Down Expand Up @@ -431,7 +431,7 @@ class _AlgorithmShowerState extends State<AlgorithmShower> {

String recommendedMove = "";

if (isTurn) {
if (isTurn && !isLoading) {
final topFew = <String>[];
for (var entry in sortedLetters) {
if (entry.value == sortedLetters.first.value) {
Expand Down Expand Up @@ -491,12 +491,12 @@ class _AlgorithmShowerState extends State<AlgorithmShower> {
crossAxisAlignment: CrossAxisAlignment.center,
children: [
const SizedBox(height: 12),
if (isTurn && recommendedMove != "")
if (isTurn && (recommendedMove != "" || isLoading))
const Text(
"Recommended move:",
style: TextStyle(fontSize: 24, fontWeight: FontWeight.w400),
),
if (isTurn && recommendedMove != "")
if (isTurn && recommendedMove != "" && !isLoading)
Padding(
padding: const EdgeInsets.symmetric(vertical: 4.0),
child: Row(
Expand Down Expand Up @@ -533,7 +533,12 @@ class _AlgorithmShowerState extends State<AlgorithmShower> {
],
),
),
if (isTurn && recommendedMove != "")
if (isTurn && isLoading)
const Padding(
padding: EdgeInsets.symmetric(vertical: 4.0),
child: CircularProgressIndicator(),
),
if (isTurn && (recommendedMove != "" || isLoading))
const Padding(
padding: EdgeInsets.symmetric(horizontal: 12.0),
child: Text(
Expand All @@ -551,7 +556,7 @@ class _AlgorithmShowerState extends State<AlgorithmShower> {
"It's not your turn.",
style: TextStyle(fontSize: 24, fontWeight: FontWeight.w400),
)
else
else if (!isLoading)
SizedBox(
height: 256,
child: SingleChildScrollView(
Expand Down Expand Up @@ -602,6 +607,15 @@ class _AlgorithmShowerState extends State<AlgorithmShower> {
);
}).toList()),
),
)
else
const Padding(
padding: EdgeInsets.symmetric(vertical: (256 - 128) / 2),
child: SizedBox(
height: 128,
width: 128,
child: CircularProgressIndicator(),
),
),
if (isTurn)
const Padding(
Expand All @@ -619,7 +633,7 @@ class _AlgorithmShowerState extends State<AlgorithmShower> {
style: TextStyle(fontSize: 18, fontWeight: FontWeight.w600),
textAlign: TextAlign.center),
),
if (!isTurn)
if (!isTurn && !isLoading)
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
Expand All @@ -630,6 +644,11 @@ class _AlgorithmShowerState extends State<AlgorithmShower> {
textAlign: TextAlign.center,
),
),
if (!isTurn && isLoading)
const Padding(
padding: EdgeInsets.all(8.0),
child: CircularProgressIndicator(),
),
if (!isTurn)
const Padding(
padding: EdgeInsets.fromLTRB(8, 28, 8, 0),
Expand All @@ -638,7 +657,7 @@ class _AlgorithmShowerState extends State<AlgorithmShower> {
style: TextStyle(fontSize: 18, fontWeight: FontWeight.w600),
textAlign: TextAlign.center),
),
if (!isTurn)
if (!isTurn && !isLoading)
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
Expand All @@ -660,6 +679,11 @@ class _AlgorithmShowerState extends State<AlgorithmShower> {
textAlign: TextAlign.center,
),
),
if (!isTurn && isLoading)
const Padding(
padding: EdgeInsets.all(8.0),
child: CircularProgressIndicator(),
),
if (!isTurn)
Padding(
padding: const EdgeInsets.all(8.0),
Expand Down

0 comments on commit 1fa262a

Please sign in to comment.