/gist:d37daa8198e30f462d6f Secret
Created
March 25, 2015 23:23
Star
You must be signed in to star a gist
This file contains 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
:- module hello. | |
:- interface. | |
:- import_module io. | |
:- pred main(io::di, io::uo) is det. | |
:- implementation. | |
:- import_module char, int, list, set, string. | |
:- func string_to_set(string) = set(char). | |
string_to_set(String) = Set :- | |
Set = set.from_list(string.to_char_list(String)). | |
:- func set_to_string(set(char)) = string. | |
set_to_string(Set) = String :- | |
String = string.from_char_list(set.to_sorted_list(Set)). | |
:- func format_word(string, set(char)) = string. | |
format_word(Word, Guesses) = Formatted :- | |
Letter = (func(C) = D :- (if set.contains(Guesses, C) then D = C else D = '_')), | |
Formatted = string.from_char_list(list.map(Letter, string.to_char_list(Word))). | |
:- pred main(string::in, set(char)::in, int::in, io::di, io::uo) is det. | |
main(Word, Guesses, Lives, !IO) :- | |
( if Lives =< 0 | |
then io.format("You died. The word was: %s\n", [s(Word)], !IO) | |
else if set.subset(string_to_set(Word), Guesses) | |
then io.write_string("You won!\n", !IO) | |
else io.format("Word: %s\nGuesses: %s\nLives: %d\n", [s(format_word(Word, Guesses)), s(set_to_string(Guesses)), i(Lives)], !IO), | |
io.read_line_as_string(IOString, !IO), | |
( if IOString = ok(String) | |
then NewGuesses = string_to_set(string.strip(String)), | |
main(Word, set.union(Guesses, NewGuesses), Lives - set.count(set.difference(NewGuesses, string_to_set(Word))), !IO) | |
else io.write_string("Something went wrong!\n", !IO) | |
) | |
). | |
main(!IO) :- main("hello", set.init, 5, !IO). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment