Which algorithm would fit best to solve a word-search game like Boggle with Python

2024/10/18 12:10:37

I'm coding a game similar to Boggle where the gamer should find words inside a big string made of random letters.

For example, there are five arrays with strings inside like this. Five rows, made of six letters each one :

AMSDNS
MASDOM
ASDAAS
DSMMMS
OAKSDO

So, the users of the game should make words using the letters available with the following restrictions and rules in mind:

  • Its not possible to repeat the same letter to make a word. Im talking about the "physical" letter, in the game that is a dice. Its not possible to use the same dice twice or more to make the word.
  • Its not possible to "jump" any letter to make a word. The letters that make the word must be contiguous.
  • The user is able to move in any direction she wants without any restriction further than the two mentioned above. So its possible to go to the top, then bottom, then to the right, then top again, and so on. So the movements to look for words might be somehow erratic.

I want to know how to go through all the strings to make words. To know the words Im gonna use a txt file with words.

I don't know how to design an algorithm that is able to perform the search, specially thinking about the erratic movements that are needed to find the words and respecting the restrictions, too.

I already implemented the UX, the logic to throw the dice and fill the boardgame, and all the logic for the six-letters dice.

But this part its not easy, and I would like to read your suggestions to this interesting challenge.

Im using Python for this game because is the language I use to code and the language that I like the most. But an explanation or suggestion of an algorithm itself, should be nice too, independently of the language.

Answer

The basic algorithm is simple.

  • For each tile, do the following.
    • Start with an empty candidate word, then visit the current tile.
    • Visit a tile by following these steps.
      • Add the tile's position's letter to the candidate word.
      • Is the candidate word a known word? If so, add it to the found word list.
      • Is the candidate word a prefix to any known word?
        • If so, for each adjacent tile that has not been visited to form the candidate word, visit it (i.e., recurse).
        • If not, backtrack (stop considering new tiles for this candidate word).

To make things run smoothly when asking the question "is this word a prefix of any word in my dictionary", consider representing your dictionary as a trie. Tries offer fast lookup times for both words and prefixes.

https://en.xdnf.cn/q/73090.html

Related Q&A

Sort using argsort in python

I try to sort an array: import numpy as nparr = [5,3,7,2,6,34,46,344,545,32,5,22] print "unsorted" print arrnp.argsort(arr)print "sorted" print arrBut the output is:unsorted [5, 3, …

pandas DataFrame resample from irregular timeseries index

I want to resample a DataFrame to every five seconds, where the time stamps of the original data are irregular. Apologies if this looks like a duplicate question, but I have issues with the interpolati…

django makemigrations to rename field without user input

I have a model with CharField named oldName. I want to rename the field to newName. When I run python manage.py makemigrations, I get a confirmation request "Did you rename model.oldName to model.…

Global Python packages in Sublime Text plugin development

1. SummaryI dont find, how Sublime Text plugins developer can use Sublime Text find global Python packages, not Python packages of Sublime Text directory.Sublime Text use own Python environment, not Py…

Can I use pip install to install a module for another users?

Im wish to install Numpy for the www-data user, but I can not log into this user using login. How can I make www-data make us of the Numpy module?To clarify. Numpy is available for root, and for my de…

Set dynamic node shape in network with matplotlib

First time poster here, so please be gentle. :)Im trying to graph a network of characters of different types in Networkx and want to set different node shapes for each type. For example, Id like chara…

How can I strip comments and doc strings from python source code? [closed]

Closed. This question is seeking recommendations for books, tools, software libraries, and more. It does not meet Stack Overflow guidelines. It is not currently accepting answers.We don’t allow questi…

How to install scipy misc package

I have installed (actually reinstalled) scipy:10_x86_64.whl (19.8MB): 19.8MB downloaded Installing collected packages: scipy Successfully installed scipyBut the misc subpackage is apparently not includ…

How to color surface with stronger contrast

In Matlab, I am trying to plot a function on 2-dim Euclidean space with following codes=.05; x=[-2:s:2+s]; y=[-1:s:3+s]; [X,Y]=meshgrid(x,y); Z=(1.-X).^2 + 100.*(Y-X.*X).^2; surf(X,Y,Z) colormap jetHer…

How to know that the interpreter is Jython or CPython in the code? [duplicate]

This question already has answers here:Can I detect if my code is running on cPython or Jython?(5 answers)Closed 9 years ago.Is there a way to detect that the interpreter that executes the code is Jyt…