Project Idea: How to Code a Hangman-style game in Logo

You can open this project in the web-based IDE environment by following this link:

Due to Logo’s enhanced string-handling capabilities and built-in dictionary, making Hangman-style games in turtleSpaces is easy-peasy!

The mechanics of a Hangman game are simple: the computer chooses a word, and then the player chooses letters. If the letters they choose are in the chosen word, then those letters are revealed. If they aren’t in the chosen word, the ‘hanged man’ is built — although these days we probably shouldn’t do that due to nasty historical context, and choose something else.

In our example, we’ve decided to reconstitute the Egyptian god Konshu (aka Khonsu) as a shameless reference to the Marvel TV show Moon Knight. Once Konshu is fully-reconstituted then he will block out the sun forever — we don’t want that! Another potential conceit could be a space portal being built by aliens which once complete will deliver an invading armada.

Whichever way you choose to present it, the mechanic is the same — once you run out of ‘misses’ the game is over.

  • Computer chooses a word
  • The number of ‘misses’ is specified
  • We prompt the user for a selection
  • We check to see if the letter is valid (hasn’t been picked before)
  • We search through the target word looking for the letter
  • If we find it, we reveal it by placing it into the ‘solution’
  • We show the player if they’ve uncovered any letters or not
  • If not, we add more on to Konshu (or the space portal, or what-have-you)
  • We repeat until the target word is entirely revealed, or Konshu is built

Here are the ways in which Logo makes building this program simple:

  • We can use the pick and english primitives to choose a word from the built-in dictionary
  • We can use forever to repeat our game loop… forever!
  • We can use question and answer to prompt for and receive input from the user
  • We can queue the user’s selection into the list that logs the user’s choices
  • We can use the boolean containsp to determine if the user’s choice has already been chosen, by passing it the user’s current choice and the list containing the users previous choices
  • We can use count to ensure the user only provides a single letter as input
  • We can use dountil to repeat the question until we get a valid answer
  • We can use foreach to cycle through each letter of the selected word and check for matches.
  • We can use a combination of setitem and loopcount to set the appropriate space in the ‘solution’ container to the guess, should it match. If we initially set the solution container to be a series of dashes, we can then check for dashes using containsp later, to see if we’ve completed the solution
  • We can call another procedure to build our ‘hangman’, passing it the number of tries remaining, so we know which part of the hangman to build. We can use switch and case to build the relevant part of our hangman
  • We can use decrement to decrease a counter by one, and use if to check to see if we are out of tries.

Because we have all of these tools at our disposal in Logo, the game logic does not occupy very many lines of code, and is nearly english-readable:

TO game
 reset hideturtle
  (print |You must determine the magic word required to 
   banish Konshu back to the netherrealm.|)
  (print |If Konshu is able to reconstitute himself in 
   this realm and awaken, he will banish the sun!|)
  ;instructions (in brackets for display purposes)
  make "attempts 10
  ;this could be made more, or less
  make "target pick english 6
  ;similarly, we could pick a different target word length
  make "solve "------
  ;this container reflects correct player choices
  ;if we change the word length, we will need to change 
  ;the number of dashes
  make "picks []
  ;this holds the players choices so we can discount 
  ;duplicate picks
  forever [
    dountil (and
      not containsp answer :picks
      1 = count answer
    ) [question |Pick letter:|]
    ;repeatedly ask which letter until the player enters only
    ;one letter, and it has not already been picked
    queue answer "picks
    ;add the selection to the :picks list
    make "hit false
    ;set the :hit boolean to false
    foreach "i :target [
      ;for each letter in the :target word:
      if :i = answer [
        ;if the letter matches the users choice
        setitem loopcount "solve answer
        ;update the appropriate character in the :solve
        ;container to reveal the matching letter
        make "hit true
        ;set the :hit boolean to true, so we don't
        ;advance the building of Konshu
    show :solve
    ;show the current state of the :solve container
    if not :hit [
      ;if our selection wasn't in the answer:
      dec "attempts
      ;decrease the :attempts container by one
      konshu :attempts
      ;execute the konshu procedure, passing the
      ;number of remaining attempts to it
      (print |Attempts remaining:| :attempts)
      ;placing round brackets around a statement allows more
      ;parameters to be passed to it than its default
    if not containsp "- :solve [
      ;if there are no dashes left in the :solve container:
      print |You have banished Konshu!|
    if :attempts = 0 [
      ;if we've no attempts left:
      (print |The word was:| :target)
      print |Darkness descends.... forever!|


See? That was pretty easy — it only took an hour to write that code! Then comes the building of our ‘hangman’ model:

TO konshu :attempts
  ;this procedure builds the relevant part of the Konshu
  switch "attempts
  ;switch takes a literal container name
  ;and then subsequent case statements execute
  ;based on the contents of that container:
  case 9 [
  ;this is what we build if there are 9 tries remaining
  case 8 [

You simply put the code to create each stage of construction inside each case block. This could make a great project to tie language / string manipulation to game coding, with a bit of 3D artistry (putting the A in STEAM) in the mix.

Logo is such a powerful, easy-to-use language it can be hard to decide what makes the best introduction for your students. But you know them best, and we hope that one of our examples will work for them, and you!

Thanks for checking out this project!

Random Sine Wave Flowers Made With turtleSpaces Logo

This simple yet attention-grabbing Logo procedure is a real visual treat, creating random three-dimensional ‘flowers’ based on the sine function. This could spice up a math class or just provide a brief introduction to sine waves in general.

The procedure makes use of the move primitive, which allows the turtle to move based on an arbitrary number of degrees away from its current heading, on the X and Y planes. When combined with the sine function, it produces these interesting ‘natural’ 3D designs.

Live model view – click and drag to rotate
TO randomflower
  clearscreen randompencolor
  ;clear the screen and select a random
  ;pen color
  dountil and 200 < abs (:x - :y) 200 < abs (:y - :x) [
    make "x random 2000 make "y random 2000
  ;set :x and :y containers to a random value between 0 and 2000
  ;BUT make sure they aren't within 200 of each other.
  ;The results in that range aren't aesthetically pleasing.
  repeat 3600 [
    ;we move down a tenth of a degree each iteration, so to form
    ;a complete 'circle' we need 3600 repetitions.
    move 0.5 {:x * sin repcount :y * sin repcount}
    ;the move primitive takes a number of turtle steps as its
    ;first argument, and then either a single degree value (X)
    ;or a list of two values (X and Y), which turn the turtle
    ;the specified number of degrees on each plane.
    ;In the case of this example, we're passing a multiple of
    ;:x and :y based on the sine of the current interation
    ;(repcount). This produces the 'flower' effect.
    down 0.1

You could put a wait into the loop so that students can see the process a bit more clearly.

Don’t forget that you can click and drag the view to orbit around the model, click with both buttons and drag to ‘pan’ the orbit point, and use the mouse wheel (or right-click and drag up and down) to zoom in and out.

Children can pick their favourites and share them with the class!

This is the flower listing as seen in the turtleSpaces application editor

PS Adding the following two lines into the repeat 3600 loop can add a little ‘pizzaz’ in the form of frags, ‘fragments’ (think of a broken mirror) or triangles made of three arbitrary points. The frag primitive creates a triangle from the last three points the turtle has stopped at, while pin and pinfrag drop ‘pins’ at points you choose and generate triangles from the last three of those pin points respectively. When combined with the sine flower procedure they add a bit of ornamentation. Just remember to setfillcolor!

    if 0 = remainder repcount 10 [pin] 
    if 0 = remainder repcount 30 [pinfrag]

Thanks for reading! Please let us know how you go at integrating turtleSpaces into your classroom’s studies. E-mail us at