I would like to make a little slotmachine for a bigger twine game.
First I thought, this would be no problem, and wrote the code below. But there is a problem with the money. Gambling, you know... ^^ Just kidding.... it's much more bad: Floating-Point Arithmetic!!! /o\
Sometimes some calculations like "
5.23 dollars - 0.01 dollars" are not that easy. Not if you use this simple code below. For some reasons the answer of it is 5.220000000000001 dollars.
I have two Questions, now:
1. Is there some kind of workaround for this problem?
2. I would like to convert cent to dollars at some point in my game, but I don't know how, exactly. It shouldn't be very difficult. I think "print" could be one part of the answer for both problems. But I tried it and failed. Any ideas?
THE SLOTMACHINE
<<set $slot1 to either ("Lemon", "Apple", "Strawberry")>>
<<set $slot2 to either ("Lemon", "Apple", "Strawberry")>>
<<set $slot3 to either ("Lemon", "Apple", "Strawberry")>>
The machine makes a strange noise then it shows these symbols:
<<timed 1s>><<print $slot1>><</timed>>
<<timed 2s>><<print $slot2>><</timed>>
<<timed 3s>><<print $slot3>><</timed>>
<<if $slot1 is "Lemon" and $slot2 is "Lemon" and $slot3 is "Lemon">><<set $Win to 1>>
<<elseif $slot1 is "Apple" and $slot2 is "Apple" and $slot3 is "Apple">><<set $Win to 1>>
<<elseif $slot1 is "Strawberry" and $slot2 is "Strawberry" and $slot3 is "Strawberry">><<set $Win to 1>>
<<else>><<set $Win to 0>><</if>>
<<timed 4s>><<if $Win is 1>>YOU WIN!<<set $Money to $Money +0.25>><</if>><</timed>>
<<timed 4s>><<if $Win is 0>>YOU LOOSE!<<set $Money to $Money -0.01>><</if>><</timed>>
Your Money now: <<timed 5s>><<print $Money>> Dollars<</timed>>
Once again!->THE SLOTMACHINE
Stop gambling!
Comments
Do not use floating point math for currency as it will bite you in the arse—especially, if you do not have a firm grasp of floating point math. I would recommend either keeping track of the smallest unit of currency which will be available in your project—e.g. cents—or keeping track of the major divisions of currency separately—e.g. dollars and cents.
Personally, I'd say just track cents and use widgets to make things easy on yourself.
For example. Create a new passage, tag it with widget, then paste the following into it: That will create a <<money>> widget macro which is used thus: You could separate the widget into two—one for printing, one for changes—if you wanted.
OTHER TIPS:
Here's what I'd suggest for your THE SLOTMACHINE passage:
I really didn't want to use it, but I didn't know another way, till now.
Okay. The whitespace was for the readability (in this forum not in my game) - it isn't the originally passage. Now, that you told me about "C" I won't do it again.... that looks much better.
I'm no "real" programmer, as you maybe see, so I really don't know what that means. Can you tell me about that, please?
I will. But how and why would I do that?
In your original example you had a $Win variable and if it was true that the player had won you set the variable to 1, otherwise if it was false that the player had won you set it to 0. So a better way to represent that situation using a Boolean variable would be: note: you don't have to include an <<else>> macro when checking a Boolean variable.
Variables that start with a dollar sign like $variable are added to the story's History whenever the reader traversals to another passage. (eg. via clicking on a link).
There are times when you want to use a variable but you don't want it's value to be remembered after the current passage, in these cases you can use a temporary variable which is one who's name starts with an underscore instead. note: a temporary variable can be set to other value type than String, I just used that as an example.
Thank you! That helps me!
It was covered fairly well by greyelf, however, you might find something insightful within the docs.
125 displayed as a two-decimal number, after dividing by 100 = $1.25
Most games remove cents/pennies, and just append ".00" to the whole-dollar amount.