Personal FormPage Wordle game​
​by Jessica Shi
Decided to also take a stab at making a Wolfram-based Wordle game! My version is a lot simpler than the actual game or the version created by David Reiss in Wolfram Language, but I think it's just super cool how few lines of code is necessary to make and deploy a game to web from Mathematica.
In[]:=
wordle=ResourceData[​​ResourceObject[​​CloudObject["https://www.wolframcloud.com/obj/arnoudb/DeployedResources/Data/Wordle-Word-List"]]]
In[]:=
five=Select[WordList[],StringLength[#]==5&]
Compared to the words of length five in the Wolfram WordList[], we can see that wordle's word list is significantly larger.
Next, I worked on implementing how the Wordle game evaluates how to color a certain letter, so I wrote these functions to replace the character with 0 if it's in the right place and 1 if its in the wrong place but in the word. For simplification, I ignored the case where there are multiple copies of the same letter in the guessed word or target word.
In[]:=
check1[letter_,target_]:=If [letter==target,0,letter]
In[]:=
check1["h","i"]
Out[]=
h
In[]:=
check1["t","t"]
Out[]=
0
In[]:=
check2[letter_,targetword_]:=If[MemberQ[targetword,letter],1 ,letter]
In[]:=
check2["a","paint"]
Out[]=
a
In[]:=
check[guess_,target_]:=Map[check2[#1,target]&,MapThread[check1[#1,#2]&,{guess,target}]]
In[]:=
check[Characters["weary"],Characters["hairy"]]
Out[]=
{w,e,1,0,0}
We must also check if an guessed word is a member of the Wordle word list.
In[]:=
wordlevalid[word_]:=MemberQ[wordle,word]
In[]:=
wordlevalid["hello"]
Out[]=
True
In[]:=
wordlevalid["sjdoi"]
Out[]=
False
In[]:=
wordlevalid["everyone"]
Out[]=
False
Then, we use this information to color the letters. I used basic text styling to represent the green and yellow blocks in the Wordle game.
In[]:=
green[letter_]:=Style[letter,Darker@Green,Large]
In[]:=
yellow[letter_]:=Style[letter,Orange,Large]
In[]:=
black[letter_]:=Style[letter,Large]
The following function takes a letter that has passed through the check function, and converts the letter to its appropriate formatting.
In[]:=
convert[letter_,index_]:=If [index==0,green[letter],If[index==1,yellow[letter],black[letter]]]
In[]:=
convert["a",0]
Out[]=
a
In[]:=
convert["b",1]
Out[]=
b
In[]:=
convert["c","c"]
Out[]=
c
We combine these functions in the following format function where we can input our "guess" word and "target" word, and it will perform the checks and color the letters.
In[]:=
format[guess_,target_]:=MapThread[convert[#1,#2]&,{Characters[guess],check[Characters[guess],Characters[target]]}]
In[]:=
format["weary","hairy"]
Out[]=
w,e,a,r,y
In this case, 'w' and 'e' are not in "hairy" at all, so they are black.'a' is in hairy but in the wrong place so it is colored orange. 'r' and 'y' are the correct letters in the correct place, so they are colored green.
​
To select the "target" word, we can randomly choose one from the Wordle list.
In[]:=
randomword[]:=wordle[[RandomInteger[{1,Length[wordle]}]]]
I decided to use CloudExpressions to hold both the user's previous guesses and the current correct answer.
​
Each time the user makes a new guess, the formatted guess is appended to the CloudExpression. This expression is cleared to an empty list each time the user guesses
In[]:=
gs=CreateCloudExpression[{}]
Out[]=
CloudExpression
UUID: 2f1d78e9-df7b-43c3-a95c-c26e92cb2380
URI: https://www.wolframcloud.com/obj/2f1d78e9-df7b-43c3-a95c-c26e92cb2380

In[]:=
word=CreateCloudExpression[randomword[]]
Out[]=
CloudExpression
UUID: 0405022a-22fc-453b-8c5a-818b2ef95c58
URI: https://www.wolframcloud.com/obj/0405022a-22fc-453b-8c5a-818b2ef95c58

Now, we can put it all together and deploy it as a FormPage. The input guess is converted to lowercase and checked to see if it is a valid 5 character Wordle word. Then we grab the previous guesses from gs. If the list is empty, we can generate a new word and put it into the CloudExpression word. We retrieve the current target word, and using this and the guess we can call the format function and append the result to gs.
In[]:=
form = FormPage["guess""String",If[wordlevalid[ToLowerCase[#guess]],​​ Module[{x,lst,wd,g},​​       g=ToLowerCase[#guess];​​       lst=Get[gs];​​       If[Length[lst]==0,Put[randomword[],word],];​​       wd=Get[word];​​       x=format[g,wd];​​       AppendTo[gs,x];​​       lst=Get[gs];​​       If [StringMatchQ[wd,g]||Length[Get[gs]]≥6,lst=Append[lst,{"The","correct","word","is",wd}],];​​       If[Length[lst]≥7||StringMatchQ[wd,g],Put[{},gs],];​​       Grid[lst]],​​Grid[Append[Get[gs],{"Input","not","valid,","try","again"}]]]&]
Out[]=
This FormPage can now be deployed to web. Keep in mind, the game doesn't work well if multiple people are accessing the page at the same time since they are updating the same cloud expressions.
In[]:=
CloudDeploy[form,"wordle",Permissions"Public"]
Out[]=
CloudObject[
https://www.wolframcloud.com/obj/jessicashi605/wordle
]
Super cool to see that we were able to make the game in only 28 lines of code! Here's all of the code used again:
In[]:=
wordle=ResourceData[ResourceObject[​​CloudObject[ "https://www.wolframcloud.com/obj/arnoudb/DeployedResources/Data/Wordle-Word-List"]]] ;​​check1[letter_,target_]:=If [letter==target,0,letter];​​check2[letter_,targetword_]:=If[MemberQ[targetword,letter],1 ,letter];​​check[guess_,target_]:=Map[check2[#1,target]&,MapThread[check1[#1,#2]&,{guess,target}]];​​wordlevalid[word_]:=MemberQ[wordle,word];​​green[letter_]:=Style[letter,Darker@Green,Large];​​yellow[letter_]:=Style[letter,Orange,Large];​​black[letter_]:=Style[letter,Large];​​convert[letter_,index_]:=If [index==0,green[letter],If[index==1,yellow[letter],black[letter]]];​​format[guess_,target_]:=MapThread[convert[#1,#2]&,{Characters[guess],check[Characters[guess],Characters[target]]}];​​randomword[]:=wordle[[RandomInteger[{1,Length[wordle]}]]];​​gs=CreateCloudExpression[{}];​​word=CreateCloudExpression[randomword[]];​​form = FormPage["guess""String",If[wordlevalid[ToLowerCase[#guess]],​​ Module[{x,lst,wd,g},​​       g=ToLowerCase[#guess];​​       lst=Get[gs];​​       If[Length[lst]==0,Put[randomword[],word],];​​       wd=Get[word];​​       x=format[g,wd];​​       AppendTo[gs,x];​​       lst=Get[gs];​​       If [StringMatchQ[wd,g]||Length[Get[gs]]≥6,lst=Append[lst,{"The","correct","word","is",wd}],];​​       If[Length[lst]≥7||StringMatchQ[wd,g],Put[{},gs],];​​       Grid[lst]],​​Grid[Append[Get[gs],{"Input","not","valid,","try","again"}]]]&];​​CloudDeploy[form,"wordle",Permissions"Public"];
Of course, the user interface of the game is not nearly on par with the actual game, but I think it gets the main idea. Next, I think it would be useful to implement the keyboard visual to show what has already been guessed and fix the issue of when 2 of the same letter are guessed at the same time.