Last active
March 11, 2022 19:03
-
-
Save aschiavon91/66e0b7dcb7b48be984fd0803c8adfc38 to your computer and use it in GitHub Desktop.
Elixir random game number generator
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 MegaGen do | |
| @min_choice 1 | |
| @max_choice 60 | |
| @valid_choices @min_choice..@max_choice | |
| @total_choices 6 | |
| defguard game_complete(total_choices, acc) when map_size(acc.map) == total_choices | |
| def min_choice, do: @min_choice | |
| def max_choice, do: @max_choice | |
| def valid_choices, do: @valid_choices | |
| def total_choices, do: @total_choices | |
| def game(total_choices \\ @total_choices, valid_choices \\ @valid_choices, acc \\ MapSet.new()) | |
| def game(total_choices, _valid_choices, acc) when game_complete(total_choices, acc) do | |
| acc.map | |
| |> Map.keys() | |
| |> IO.inspect(charlists: :as_lists) | |
| end | |
| def game(total_choices, valid_choices, acc) do | |
| new = random(valid_choices, acc) | |
| if MapSet.member?(acc, new) do | |
| game(total_choices, valid_choices, acc) | |
| else | |
| game(total_choices, valid_choices, MapSet.put(acc, new)) | |
| end | |
| end | |
| defp random(valid_choices, invalid_choices) do | |
| valid_choices | |
| |> Enum.to_list() | |
| |> MapSet.new() | |
| |> MapSet.difference(invalid_choices) | |
| |> Enum.random() | |
| end | |
| end | |
| System.argv() | |
| |> then(fn | |
| [total_choices, min_choice, max_choice] -> | |
| [ | |
| String.to_integer(total_choices), | |
| String.to_integer(min_choice)..String.to_integer(max_choice) | |
| ] | |
| [total_choices, max_choice] -> | |
| [String.to_integer(total_choices), MegaGen.min_choice()..String.to_integer(max_choice)] | |
| [total_choices] -> | |
| [String.to_integer(total_choices), MegaGen.valid_choices()] | |
| [] -> | |
| [MegaGen.total_choices(), MegaGen.valid_choices()] | |
| end) | |
| |> then(&apply(MegaGen, :game, &1)) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Works great for MegaSena, LotoFácil and other brazilian loto games!
Running