Skip to content

Instantly share code, notes, and snippets.

@nhessler
Created April 13, 2016 00:01
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 nhessler/c204c473957a81c3854178249b9315ad to your computer and use it in GitHub Desktop.
Save nhessler/c204c473957a81c3854178249b9315ad to your computer and use it in GitHub Desktop.
defmodule ElixirBaseball.Game do
def play_ball do
IO.puts inspect(play_inning([], 0, 0))
end
def play_inning(top_of_inning, _, _) when length(top_of_inning) < 9 do
bottom_of_inning = [get_inning | top_of_inning]
play_inning(bottom_of_inning,
total_score(bottom_of_inning, :home),
total_score(bottom_of_inning, :away))
end
def play_inning(top_of_inning, tie_score, tie_score) do
bottom_of_inning = [get_inning | top_of_inning]
play_inning(bottom_of_inning,
total_score(bottom_of_inning, :home),
total_score(bottom_of_inning, :away))
end
def play_inning(complete_innings, _, _), do: complete_innings
def total_score(innings, team) do
Enum.reduce(innings, 0, &(Map.get(&1, team) + &2))
end
def get_inning do
%{home: get_score, away: get_score}
end
def get_score, do: :rand.uniform(10) - 1
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment