Skip to content

Instantly share code, notes, and snippets.

@svoboda-jan
Created November 3, 2024 19:50
Show Gist options
  • Save svoboda-jan/1a6e6ac273cdc7988aae664c1e3bf756 to your computer and use it in GitHub Desktop.
Save svoboda-jan/1a6e6ac273cdc7988aae664c1e3bf756 to your computer and use it in GitHub Desktop.
A dice betting game using Opal Ruby to Javascript source-to-source compiler
# Dice Betting Game, https://opalrb.com/try/
bank = 100;
while bank > 0 do
bet = `prompt("Place your bet")`.to_i
dice1 = (rand * 6).ceil
dice2 = (rand * 6).ceil
outcome = dice1 == dice2
bank += bet if outcome
bank -= bet unless outcome
puts "Numbers are: #{dice1} #{dice2}"
puts outcome_txt = outcome ? "You Win" : "You Lose"
outcome_txt += ", bank is now #{bank}"
puts "Bank is now #{bank}"
`alert(outcome_txt)`
end
@rubyFeedback
Copy link

Is there a standalone example? E. g. can I just put the ruby code into a .html file too, or do
I need to use "opal --compile"?

Would be nice to add it to the official examples, if it works. Right now I am not sure how to
get it to work (I am an opal-noob at this point in time).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment