Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- defmodule Hangman.Game do
- alias Hangman.Game
- # start with declaring a struct
- defstruct(
- game_state: :initializing,
- turns_left: 7,
- letters: [],
- used: []
- )
- def new_game do
- %Game{letters: String.codepoints("hello")}
- end
- def new_game(word) do
- %Game{letters: String.codepoints(word)}
- end
- def play(game, guess) do
- make_move(game, guess ,Enum.member?(game.used, guess))
- end
- def make_move(game, _ ,_already_guess=true) do
- Map.put(game, :game_state, :already_guess)
- end
- def make_move(game, guess, _already_guess=false) do
- game = Map.put(game, :used, [guess | game.used])
- check_guess(game, Enum.member?(game.letters, guess))
- end
- def check_guess(game, _correct_guess=true) do
- check_game_won(game, MapSet.subset?(MapSet.new(game.letters), MapSet.new(game.used)))
- end
- def check_guess(game, _correct_guess=false) do
- Map.put(game, :game_state, :bad_guess)
- end
- def check_game_won(game, _won=true) do
- Map.put(game, :game_state, :won)
- end
- def check_game_won(game, _won=false) do
- Map.put(game, :game_state, :good_guess)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement