Created
April 13, 2016 00:01
-
-
Save nhessler/c204c473957a81c3854178249b9315ad to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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