Worldle, not Wordle: Mathematica take on the country guessing game​
​by David Reiss
My daughter introduced me to Worldle earlier today. Read this carefully, I said Worldle, not Wordle... (For Wordle, take a look at this blog post: Wordle the Wolfram Way.)
Worldle is at this Worldle web site.
The premise is to try to guess the country from the outline of the country’s boundary in 6 guesses. Somewhat harder than Wordle...
But, I had to say to my daughter, as I did the last time when I was shown Wordle, "I think that I can make a Mathematica version pretty quickly". So I spent a couple of hours and created this:
Here’s the notebook that produces this... Enjoy!
(You can also download it at the end of this post.)
In[]:=
Module{game,$countryNames},​​$countryNames=​​WithallCountriesEntityClass=
all countries, dependencies, and territories
COUNTRIES
,​​DeleteDuplicatesSort@FlattenallCountriesEntityClass
full name
,allCountriesEntityClass
name
​​;​​​​game=​​Framed[#,RoundingRadius5,FrameStyleLightGray]&@​​DynamicModule​​{countryName="",polygon={},randomCountry,randomCountryPolygon,randomCountryGraphic,distance,direction,showMe=False,guesses={},allDone=False,nGuesses=6,youGuessedIt=False,showHint=False},​​​​randomCountry=RandomChoice[$countryNames];​​randomCountryPolygon=CountryDataStringTrim[randomCountry],
polygon
;​​randomCountryGraphic=GeoGraphics[{EdgeForm[Black],Blue,randomCountryPolygon},ImageSize{250,250},GeoBackgroundNone];​​distance=∞;​​direction=0;​​​​Grid​​​​Labeled[​​Framed[Dynamic[randomCountryGraphic],RoundingRadius5,FrameStyleLightGray],​​"Guess me!",Top,​​LabelStyle{14,FontFamily"Arial"}],​​​​Column​​Button"New",​​​​showMe=False;​​randomCountry=RandomChoice[$countryNames];​​randomCountryPolygon=CountryDataStringTrim[randomCountry],
polygon
;​​randomCountryGraphic=GeoGraphics[{EdgeForm[Black],Blue,randomCountryPolygon},ImageSize{250,250},GeoBackgroundNone];​​distance=∞;​​direction=0;​​guesses={};​​allDone=False;​​youGuessedIt=False;​​countryName="";​​polygon={};​​showHint=False​​,​​Method"Queued",ImageSize90,​​​​Framed[#,RoundingRadius5,FrameStyleLightGray]&@(​​InputField[Dynamic[countryName,​​(countryName=#;showHint=False)&],​​String,FieldCompletionFunctionFunction[x,Select[$countryNames,StringMatchQ[#,___~~StringTrim[x]~~___,IgnoreCaseTrue]&]],BaseStyle{12,FontFamily"Arial"},EnabledDynamic[!allDone&&!youGuessedIt]]​​),​​​​ButtonDynamic[If[!allDone,"Guess","All Done!"]],​​Catch@​​​​If[allDone,Throw[Null]];​​If[!MemberQ[$countryNames,StringTrim[countryName]],Throw[Null]];​​polygon=CountryDataStringTrim[countryName],
polygon
;​​distance=GeoDistance[CountryData[countryName,"CenterCoordinates"],CountryData[randomCountry,"CenterCoordinates"]];​​youGuessedIt=TrueQ[CountryData[countryName,"CenterCoordinates"]===CountryData[randomCountry,"CenterCoordinates"]];​​direction=​​If[youGuessedIt,​​"You have arrived!",​​GeoDirection[CountryData[countryName,"CenterCoordinates"],CountryData[randomCountry,"CenterCoordinates"]]​​];​​guesses=Append[guesses,countryName];If[Length[guesses]===nGuesses,allDone=True];​​showHint=False​​,​​Method"Queued",ImageSize90,​​EnabledDynamic[!allDone&&!youGuessedIt],​​​​Dynamic[​​If[polygon==={},​​Spacer[1],​​GeoGraphics[{EdgeForm[Black],Green,polygon},ImageSize{250,250},GeoBackgroundNone]​​],​​TrackedSymbols{polygon}],​​​​Column[​​{​​Row[{"Distance: ",Dynamic[distance]},BaseStyle{Darker[Blue],12,FontFamily"Arial"}],​​Row[{"Direction: ",Dynamic[direction]},BaseStyle{Darker[Blue],12,FontFamily"Arial"}],​​Spacer[1],​​Dynamic[​​Column[​​Flatten[​​{​​Style["Your guesses",Darker[Blue],12,FontFamily"Arial"],​​guesses}],​​AlignmentLeft,BaseStyle{Black,12,FontFamily"Arial"}]],​​Button[Dynamic[If[!allDone,"Show me ","All Done!"]],showMe=True,ImageSize100],​​Dynamic[​​If[TrueQ[showMe]||TrueQ[allDone]||TrueQ[youGuessedIt],​​Style[randomCountry,11,Gray,FontFamily"Arial"],​​""]]​​}​​]​​,​​​​Item[#,AlignmentCenter]&@Column[​​​​Button["Show Hint",showHint=True,ImageSize100],​​Dynamic[​​If[TrueQ[showHint]&&!(polygon==={}),​​Framed[#,RoundingRadius5,FrameStyleLightGray]&@GeoGraphics[​​ Flatten@{EdgeForm[Black],Green,polygon,EdgeForm[Black],Blue,randomCountryPolygon,​​AbsoluteThickness[2],​​Red,​​GeoPath[{CountryData[countryName,"CenterCoordinates"],CountryData[randomCountry,"CenterCoordinates"]}]},​​ImageSize{250,250},GeoBackgroundNone],​​Spacer[1]]]​​​​],SpanFromLeft​​,​​Alignment{Left,Top},​​SaveDefinitionsTrue​​;​​​​CreateDocument[{TextCell[game,"Text",ShowStringCharactersFalse]},Deployed->True]​​;