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


--
-- demo\rosetta\Guess_the_number.exw
--
with javascript_semantics
include pGUI.e

integer secret = rand(10)

function valuechanged_cb(Ihandle guess)
    integer n = IupGetInt(guess,"VALUE")
    if n=secret then
        Ihandle lbl = IupGetBrother(guess,true)
        IupSetAttribute(lbl,"TITLE","Your guess was correct")
        IupRefresh(lbl)
        IupSetInt(guess,"ACTIVE",false)
    end if
    return IUP_DEFAULT
end function

procedure main()
    IupOpen()
    Ihandle lbl = IupLabel("Your guess","RASTERSIZE=58x21"),
            guess = IupText("VALUECHANGED_CB", Icallback("valuechanged_cb"),
                            "RASTERSIZE=21x21"),
            dlg = IupDialog(IupVbox({IupFill(),
                            IupHbox({IupFill(),lbl,guess,IupFill()},
                                    "GAP=10"),
                            IupFill()}),
                            `MINSIZE=300x100,TITLE="Guess the number"`)
    IupShow(dlg)
    IupSetAttribute(lbl,"RASTERSIZE","x21")
    if platform()!=JS then
        IupMainLoop()
        IupClose()
    end if
end procedure
 
main()
