Some comments on AutoGeneratedPackage
Some comments on AutoGeneratedPackage
This notebook has its AutoGeneratedPackage option sent to AutoGeneratedPackage.
This display notebook contains only Input cells for formatting purposes. The downloadable version has, as appropriate, package code in Code cells.
Options[EvaluationNotebook[],AutoGeneratedPackage]
{AutoGeneratedPackageAutomatic}
This causes a .m file of the same name as this notebook to be saved in this notebook’s directory each time the notebook is saved. Only those cells that have their InitializationCell option set to True will be saved to the .m file. Thus be careful not to put unnecessary code into Input cells and also be sure to put the code that you wish to be part of the package into Code cells. (These are the default behaviors of these cells: Code cells have InitializationCell->True and Input cells have InitializationCell->False. You can of course override these in a custom style sheet, but best not to do that as it may confuse the user behind the scenes in a way that they will not realize.
The notebook has a few other options set, for example in its style sheet that I sometimes prefer. Feel free to execute this to see them.
Options[EvaluationNotebook[]]
Code for cycling any development
Code for cycling any development
This is come convenient code to use when cycling through development. It makes is so that you do not need to Quit[] each time you make edits. Un comment it to use in a load/reload cycle. Keep it in an Input cell so that it is not included in the package output.
(*Module[{mwordleContexts,mwordleSymbolPatterns},mwordleContexts={"Wordle`Wordle`"};mwordleSymbolPatterns={#<>"*",#<>"Private`*",#<>"Private`*Private`*"}&/@mwordleContexts;Quiet[Unprotect@@Flatten[mwordleSymbolPatterns]];Quiet[Remove@@Flatten[mwordleSymbolPatterns]];Unprotect[$Packages];$Packages=DeleteCases[$Packages,Alternatives@@mwordleContexts];Protect[$Packages];(*justchecktoseeifanyoneisleftover*)Names[mwordleSymbolPatterns]]*)
Initial comments
Initial comments
(*AroughimplementationofWordle,https://www.powerlanguage.co.uk/wordle/byDavidReiss:dreiss@scientificarts.comPermissionisgrantedtouseandtomodifythecodeforanynon-commercialpurposes.Enjoy:Iprobablywillnotsupplyanyupdatestothisbuthavefunwiththecodeifyouwishtomodifyandexperimentwithit.Thisnotebook,whensaved,alsocreatesa.mpackagefilewiththesamename.*)
(*OncethecodeisexecutedyouthenhaveaccesstoMWordle[Deploy]whichlaunchesthegame.Youcantabbetweenthefieldswhereyoutypeletters,oryoucanclickwithineachateachstep.However,Ihavenotimplementedtheabilitytojusttypefromthekeyboardandautomaticallyplacelettersineachsubsequentfield.*)
BeginPackage
BeginPackage
BeginPackage["MWordle`MWordle`"]
Usage Messages
Usage Messages
$MWordleNotebook::usage="$MWordleNotebook is the NotebookObject of an open MWordle application.";
resetMWordleParameters::usage="resetMWordleParameters[] resets the applicaiton parameter to their original state \so that a new instance of the game cal be played. ";
$mwordleSizes::usage="$mwordleSizes is a list of word lengths that the game can be played with.";
mwordleInstructions::usage="mwordleInstructions[] generates the instructions for the game.";
MWordle::usage="MWordle[Default] launches the application in its own window. \\n\nMWordle[] generates an instance of the application. You should not \have more than one instance of the application open at any one time.";
$mwordleWordLength::usage="$mwordleWordLength is the number of characters in the game's word.";
$mwordleData::usage="$mwordleData holds the data for the current state of game play guesses.";
$defaultMWordleData::usage="$defaultMWordleData is the initial default value for $mwordleData. ";
mwordleGrid::usage="mwordleGrid[] generates a grid for the application.";
$enabled::usage="$enabled determines which row in the puzzle is currently enabled for entry.";
$defaultEnabled::usage="$defaultEnabled is the initial default value of $enabled.";
$wordList::usage="$wordList contains the full dictionary of words to be used in the application. \It is an Association with integer keys for each length of word you have chosen.";
checkWords::usage="checkWords[] is the funciton that checks the status of the word that you are entering.";
$checkMessage::usage="$checkMessage holds any messages that should be shown in the applicaiton.";
$mwordleDataBackgrounds::usage="$mwordleDataBackgrounds holds the durrent backgorund \colors for all the character inputs.";
$defaultMWordleDataBackgrounds::usage="$defaultMWordleDataBackgrounds is the initial \default value of $mwordleDataBackgrounds.";
$almostColor::usage="$almostColor is the backgorund color for a correct character \that is not in the correct location.";
$exactColor::usage="$exactColor is the backgorund color for a correct character that \is in the correct location.";
$noMatchColor::usage="$noMatchColor is the backgorund color for an incorrect character.";
keyboard::usage="keyboard[] generates the application's keyboard.";
$notAMatchCharacters::usage="$notAMatchCharacters holds the list of characters that \are not a match ot the word in the current \state of application play.";
$cheating::usage="$cheating is a parameter that actually holds the word that you are trying to guess. \Play fair! don't use this ;-).";
$wordTypes::usage="$wordTypes is the set of rules for possible groups of words to use in \the application: {\"KnownWords\",\"Noun\",\"Verb\",\"Adjective\"}. \in the list of rules, \"KnownWords\" is referred to as All.";
$currentWordType::usage="$currentWordType is the currently chosen menu selection out of $wordTypes.";
`Private
`Private
Begin["`Private`"]
Code
Code
$mwordleWordLength, $mwordleData, $defaultMWordleData, $enabled, $defaultEnabled
$mwordleWordLength, $mwordleData, $defaultMWordleData, $enabled, $defaultEnabled
Various parameters used in the code are set up here. General rule: name each parameter (in contrast to a function) with “$” as the first character of its name-- this way you can easily spot them.
These parameters are passed around so that various parts of the application can make use of them as the game is played. Any changes to their values are sometimes a “side effect” of the functions that comprise the playing of the game. Though side effects are often frowned upon by programmers, the trade-off in some cases would be to have to include everything in a large top-level DynamicModule. Folks have strong feelings about side effects: my approach is to use them with great care when they make life much simpler than it would otherwise be. The alternative approach in this code would be to have additional arguments to some of the functions and to add attributes in various cases to hold the arguments so that they could be used within various Dynamics.
(*initializations*)
$mwordleSizes={4,5,6,7};
$mwordleWordLength=5;
$mwordleData=AssociationThread[Flatten[Table[{i,j},{i,1,$mwordleWordLength+1},{j,1,$mwordleWordLength}],1]->""];
$defaultMWordleData=$mwordleData;
$mwordleDataBackgrounds=AssociationThread[Flatten[Table[{i,j},{i,1,$mwordleWordLength+1},{j,1,$mwordleWordLength}],1]->White];
$defaultMWordleDataBackgrounds=$mwordleDataBackgrounds;
$enabled=KeySort[Join[<|1->True|>,AssociationThread[Range[2,$mwordleWordLength+1]->False]]];
colors
colors
resetMWordleParameters
resetMWordleParameters
This is used to reset each $-sign variable when a new instance of the application is created. Note that the $currentWordType parameter is not included here so that it is not reset: i.e., so that you stay with a Verb if that is what you chose.
mwordleGrid
mwordleGrid
This creates the grid of InputFields. The Dynamic in each InputField is set up with a second argument that makes sure that only one character appears in the input field and that it is ToUpperCase.
keyboard
keyboard
Create a keyboard comprised of Buttons, where each letter of the letters that you so-far guessed, that is not included in the word is colored with a gray background. This could be extended to color the letters based on which ones also are correct, as in the on-line version.
mwordleInstructions
mwordleInstructions
Instructions for the game which is used in the PopupWindow function later on.
Dictionary
Dictionary
Build the lists of words, using Mathematica’s WordList function, to populate from Mathematica’s supplied dictionary. The application is tied to that dictionary, which is missing some words that you’d expect it to have and also has some strange things (such as some Roman numerals!) which you would not like the game to have.
These are all the word in the dictionary and they include plurals. It’s important to be sure that the words only comprise LetterCharacter characters so we do not get contractions, etc... Since these may not include plurals those are added using the Pluralize function which I Quiet, so that it does not generate messages when it cannot find a plural.
These are the words that are of of {“Noun”,”Verb”,”Adjective”}:
CheckWords, $checkMessage
CheckWords, $checkMessage
This is the main function for checking the current state of game play and generating messages to show at the bottom of the game window.
I have included a $cheating parameter so that you have an out if you get overwhelmed and just want the answer — it happens!
MWordle
MWordle
The full layout of the game. It can have a word as its argument, in which case that is the hidden word to be guessed. Or its argument can be Automatic, which causes a word to be randomly selected each time it’s called.
This is the code that opens the game in its own window. It carefully checks whether the game’s NotebookObject is already amongst Notebooks[]. If it is it just brings it to the front. If it is not then it opens a new game and assigns the game’s NotebookObject to $MWordleNotebook. Since there are various $-sign parameters that are set and changed as a game is played, you do not want to have more than one game open at a time. If you did then an action in one game will change the parameters for the other game, putting each into an unknown, scrambled, state. It’s a good principle for User Interfaces like this in general to keep careful track of which instances of the DynamicModules are open at any given time to be sure that parameter that are shared (and are not localized) do not get accidentally, undesirably, overwritten.
I have set SaveDefinitionsFalse in this DynamicModule (which is DynamicModule’s default anyway). If you change it to True then you can save the application to disk to be used independently. If you then save it to the Cloud, you would hope that it would work there: it does not. parts do and parts do not. The Cloud is picky — and it is perhaps because there are too many layers of DynamicModule for the Cloud to handle. Perhaps this application would be a good debugging instance for the ongoing Cloud development. I had high hopes that it would work there.
End
End
End the `Wordle`Wordle`Private Context
EndPackage
EndPackage
End the Wordle`Wordle` Context.
Enjoy!