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!]]
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!]]