From 1fa262ab856a58c92f8aa284752bdd17cc11908c Mon Sep 17 00:00:00 2001 From: PeculiarProgrammer <179261820+PeculiarProgrammer@users.noreply.github.com> Date: Wed, 18 Dec 2024 22:00:46 -0500 Subject: [PATCH] Better Loader 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. --- README.md | 2 +- lib/algorithm.dart | 2 +- lib/main.dart | 50 ++++++++++++++++++++++++++++++++++------------ 3 files changed, 39 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index b12ac06..17765b2 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/lib/algorithm.dart b/lib/algorithm.dart index b7f2be6..7a62879 100644 --- a/lib/algorithm.dart +++ b/lib/algorithm.dart @@ -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(key: null, value: null); diff --git a/lib/main.dart b/lib/main.dart index eda0713..650d20b 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -367,10 +367,7 @@ class _AlgorithmShowerState extends State { 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: @@ -384,12 +381,15 @@ class _AlgorithmShowerState extends State { snapshot.data![1] as List>; } - 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), @@ -431,7 +431,7 @@ class _AlgorithmShowerState extends State { String recommendedMove = ""; - if (isTurn) { + if (isTurn && !isLoading) { final topFew = []; for (var entry in sortedLetters) { if (entry.value == sortedLetters.first.value) { @@ -491,12 +491,12 @@ class _AlgorithmShowerState extends State { 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( @@ -533,7 +533,12 @@ class _AlgorithmShowerState extends State { ], ), ), - 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( @@ -551,7 +556,7 @@ class _AlgorithmShowerState extends State { "It's not your turn.", style: TextStyle(fontSize: 24, fontWeight: FontWeight.w400), ) - else + else if (!isLoading) SizedBox( height: 256, child: SingleChildScrollView( @@ -602,6 +607,15 @@ class _AlgorithmShowerState extends State { ); }).toList()), ), + ) + else + const Padding( + padding: EdgeInsets.symmetric(vertical: (256 - 128) / 2), + child: SizedBox( + height: 128, + width: 128, + child: CircularProgressIndicator(), + ), ), if (isTurn) const Padding( @@ -619,7 +633,7 @@ class _AlgorithmShowerState extends State { style: TextStyle(fontSize: 18, fontWeight: FontWeight.w600), textAlign: TextAlign.center), ), - if (!isTurn) + if (!isTurn && !isLoading) Padding( padding: const EdgeInsets.all(8.0), child: Text( @@ -630,6 +644,11 @@ class _AlgorithmShowerState extends State { 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), @@ -638,7 +657,7 @@ class _AlgorithmShowerState extends State { style: TextStyle(fontSize: 18, fontWeight: FontWeight.w600), textAlign: TextAlign.center), ), - if (!isTurn) + if (!isTurn && !isLoading) Padding( padding: const EdgeInsets.all(8.0), child: Text( @@ -660,6 +679,11 @@ class _AlgorithmShowerState extends State { textAlign: TextAlign.center, ), ), + if (!isTurn && isLoading) + const Padding( + padding: EdgeInsets.all(8.0), + child: CircularProgressIndicator(), + ), if (!isTurn) Padding( padding: const EdgeInsets.all(8.0),