Skip to content

Instantly share code, notes, and snippets.

@jjhop
Last active May 16, 2016 16:28
Show Gist options
  • Save jjhop/bf9376ae80e5db26ff6a23541e0ae1ce to your computer and use it in GitHub Desktop.
Save jjhop/bf9376ae80e5db26ff6a23541e0ae1ce to your computer and use it in GitHub Desktop.
defmodule GuessingGame do
def start do
IO.puts("Pomyśl liczbę (z przedziału 1 do 100), gdy będziesz gótów/gotowa wciśnij [ENTER].")
IO.read(:stdio, :line)
IO.puts("Będę zgadywał a Ty wciśnij
< - jeśli Twoja liczba jest mniejsza oraz
> - jeśli jest większa, od podanej przeze mnie,
= - jeśli zgadłem
i cokolwiek innego, gdy masz już dość :)")
case guess(0, 100, 1) do
{:success, n} -> IO.puts("Zgadłem za #{n} razem!")
:exit -> IO.puts("Do następnego razu")
end
end
defp guess(current_min, current_max, n) do
mid = trunc((current_min + current_max) / 2)
IO.puts("Czy Twoja liczba to #{mid}")
case String.rstrip(IO.read(:stdio, :line)) do
"<" -> guess(current_min, mid, (n+1))
">" -> guess(mid, current_max, (n+1))
"=" -> {:success, n}
_ -> :exit
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment