Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

FizzBuzz in Twine 2 + Harlowe

edited August 2015 in Chit-Chat
So I was feeling a bit bored, and decided to see if it was possible to implement FizzBuzz in Twine.

FizzBuzz is a programming challenge which used to be used a lot in interviews for programming jobs. The definition is

"Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”."

My first attempt used the display macro to recursively display the main passage, until a counter reached 101.
(if: $count % 15 is 0)[Fizzbuzz](elseif: $count % 3 is 0)[Fizz](elseif: $count % 5 is 0)[Buzz](else:)[$count]
(set: $count to $count + 1)
(if: $count < 101)[(display: "fizzbuzz")]

The problem was Twine has code to prevent infinite recursion which caused the program to stop after 24 iterations.

So I did a second version using the live macro to update the counter, and append the correct value to the display, every 100ms. Stopping when the counter reaches 101.
(set: $count to 1)
[]<display|
(live: 100ms)[(if: $count % 15 is 0)[(append: ?display)[FizzBuzz<br>]](elseif: $count % 3 is 0)[(append: ?display)[Fizz<br>]](elseif: $count % 5 is 0)[(append: ?display)[Buzz<br>]](else:)[(append: ?display)[$count<br>]](set: $count to $count + 1)(if: $count is 101)[(stop:)] ]

And this version works.

So if anyone asks if Twine can implement FizzBuzz you can answer, "Yes it can."

I've attached the working version.

Comments

  • Throw in a story, some images perhaps and you could make Twine a great tool to teach people math.
  • Talking about learning maths in interactive fiction has just given me flashbacks to having to work out triangular numbers while playing Martello Tower at school.

    You're right though that Twine could be a good tool for teaching maths.
  • triangular numbers while playing Martello Tower at school.

    No idea what that is, but I'm glad you enjoyed those flashbacks. You did enjoy them, right? ;)

  • bruno wrote: »
    triangular numbers while playing Martello Tower at school.

    No idea what that is,

    I should have included a link. Unfortunately the only description I could find of it is a PDF copy of the manual http://www.mocagh.org/miscgame/martello-manual.pdf
    but I'm glad you enjoyed those flashbacks. You did enjoy them, right? ;)

    I'm not sure enjoy is quite the right word...
  • haha that's amazing! I can see a Twine game beginning with:

    It is a warm, sunny day. You are walking along a cliff top above the sea. In the distance, a small ship is bobbing on the waves. Ahead of you on the cliffs you can see an old tower. You decide to explore it but you find yourself in very mysterious surroundings from which it is difficult to escape.

    You have to find a magic word on the way through
Sign In or Register to comment.