Task

Write a program where the program chooses a number between   1   and   10.

A player is then prompted to enter a guess.   If the player guesses wrong,   then the prompt appears again until the guess is correct.

When the player has made a successful guess the computer will issue a   "Well guessed!"   message,   and the program exits.

A   conditional loop   may be used to repeat the guessing until the user is correct.



Related tasks

   Bulls and cows
   Bulls and cows/Player
   Guess the number/With Feedback
   Mastermind


:- module guess.
:- interface.
:- import_module io.
:- pred main(io::di, io::uo) is det.
:- implementation.
:- import_module random, string.
 
main(!IO) :-
        time(Time, !IO),
        random.init(Time, Rand),
        random.random(1, 10, N, Rand, _),
        main(from_int(N) ++ "\n", !IO).
 
:- pred main(string::in, io::di, io::uo) is det.
main(N, !IO) :-
        io.write_string("Guess the number: ", !IO),
        io.read_line_as_string(Res, !IO),
        (
                Res = ok(S),
                ( if S = N then io.write_string("Well guessed!\n", !IO)
                  else main(N, !IO) )
        ;
                Res = error(E)
        ;
                Res = eof
        ).
 
:- pred time(int::out, io::di, io::uo) is det.
 
:- pragma foreign_decl("C", "#include <time.h>").
:- pragma foreign_proc("C", time(Int::out, _IO0::di, _IO1::uo),
                       [will_not_call_mercury, promise_pure],
                       "Int = time(NULL);").