Created
April 13, 2016 00:02
-
-
Save rickr/77fb3997f4c2ea54c7ee9138149d35cf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Baseball do | |
def simulate(innings) do | |
IO.puts "Simulating game with #{innings} innings" | |
simulate_inning(1, innings, 0, 0) | |
end | |
def simulate_inning(current_inning, total_innings, away_score, home_score) when current_inning > total_innings do | |
IO.puts "Game done. Mets win" | |
IO.puts "Away: #{away_score}" | |
IO.puts "Home: #{home_score}" | |
end | |
def simulate_inning(current_inning, total_innings, away_score, home_score) do | |
IO.puts "Simulating inning #{current_inning}" | |
away_score = away_score + random_runs | |
:timer.sleep(random_inning_delay) | |
home_score = home_score + random_runs | |
:timer.sleep(random_inning_delay) | |
IO.puts "Away: #{away_score}" | |
IO.puts "Home: #{home_score}" | |
IO.puts "Inning over." | |
simulate_inning(current_inning + 1, total_innings, away_score, home_score) | |
end | |
def random_runs do | |
:random.seed(:erlang.now()) | |
rem(:random.uniform(1000000), 2) | |
end | |
def random_inning_delay do | |
:random.seed(:erlang.now()) | |
:random.uniform(1000) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment