Skip to content

Instantly share code, notes, and snippets.

@paweljw
Created December 12, 2017 19:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paweljw/9e5ccddbac6e12746ab9970ca4dc1813 to your computer and use it in GitHub Desktop.
Save paweljw/9e5ccddbac6e12746ab9970ca4dc1813 to your computer and use it in GitHub Desktop.
defmodule Roulette do
@min_bet 0.1
def minimum_bet, do: @min_bet
def print_score(game) do
IO.puts("R: #{game.rounds}\tL: #{game.last_number}\t$#{game.cash}\tB$#{game.bet}\tH: #{game.hits}\tM: #{game.misses}\tH/M: #{h_to_m(game)}")
end
def h_to_m(%{hits: hits, misses: misses} = game) do
hits / misses
end
def play_round(game) do
game = %{game | cash: game.cash - game.bet}
game = %{game | last_number: next_number()}
game = case bet_won(game) do
true -> %{game | hits: game.hits + 1, cash: game.cash + game.bet * 2, bet: @min_bet, bet_on: next_bet(game.bet_on)}
false -> %{game | misses: game.misses + 1, bet: game.bet * 2}
end
game = %{game | rounds: game.rounds + 1}
print_score(game)
if(game.cash < 0, do: Kernel.exit(:shutdown))
play_round(game)
end
def bet_won(game) do
if(game.bet_on == :black, do: number_black?(game.last_number), else: number_red?(game.last_number))
end
def next_bet(current_bet) do
case current_bet do
:black -> :red
:red -> :black
end
end
def number_black?(number) do
case number do
x when x in 1..10 -> Integer.mod(x, 2) == 0
x when x in 19..28 -> Integer.mod(x, 2) == 0
x when x in 11..18 -> Integer.mod(x, 2) != 0
x when x in 29..36 -> Integer.mod(x, 2) != 0
_ -> false
end
end
def number_red?(number) do
case number do
x when x in 1..10 -> Integer.mod(x, 2) != 0
x when x in 19..28 -> Integer.mod(x, 2) != 0
x when x in 11..18 -> Integer.mod(x, 2) == 0
x when x in 29..36 -> Integer.mod(x, 2) == 0
_ -> false
end
end
def next_number() do
:rand.uniform(36)
end
end
game = %{cash: 100.0, hits: 0, misses: 0, rounds: 0, bet_on: :black, bet: Roulette.minimum_bet, last_number: nil}
Roulette.play_round(game)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment