From 25f65f212c210638d15d73c0c8dfe1fc08bd96a2 Mon Sep 17 00:00:00 2001 From: Andinus Date: Wed, 12 Jan 2022 12:42:38 +0530 Subject: Add visualize feature using Cairo This takes parts from Fornax: https://github.com/andinus/fornax Each step is visualized and a video solution is generated. --- lib/Octans/WordSearch.rakumod | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) (limited to 'lib/Octans/WordSearch.rakumod') diff --git a/lib/Octans/WordSearch.rakumod b/lib/Octans/WordSearch.rakumod index 23487dc..73d05cd 100644 --- a/lib/Octans/WordSearch.rakumod +++ b/lib/Octans/WordSearch.rakumod @@ -1,9 +1,9 @@ use Octans::Neighbors; use Octans::RangeSearch; -# word-search walks the given grid & tries to find words in the -# dictionary. It walks in Depth-First manner (lookup Depth-First -# search). +#| word-search walks the given grid & tries to find words in the +#| dictionary. It walks in Depth-First manner (lookup Depth-First +#| search). sub word-search( # @dict holds the dictionary. @puzzle holds the puzzle. @dict, @puzzle, @@ -18,12 +18,19 @@ sub word-search( Int $y, Int $x, $str? = @puzzle[$y][$x], # @visited holds the positions that we've already visited. - @visited? is copy --> List + @visited? is copy, + + # If true then every iteration of walking is returned. + Bool :$visualize, + + --> List ) is export { # If @visited was not passed then mark the given cell as visited # because it's the cell we're starting at. @visited[$y][$x] = True unless @visited; + take "", @visited if $visualize; + # neighbor block loops over the neighbors of $y, $x. neighbor: for neighbors(@puzzle, $y, $x).List -> $pos { # Move on to next neighbor if we've already visited this one. @@ -53,7 +60,7 @@ sub word-search( # subset of words that start with "a", so keeping # this in mind we pass the output of last # range-starts-with (@list). - @list, @puzzle, $pos[0], $pos[1], $word, @visited + @list, @puzzle, $pos[0], $pos[1], $word, @visited, :$visualize, ); } } -- cgit 1.4.1-2-gfad0