/*=============================*/ /* "NumberMind" game for ARexx */ /* © 1996 John Filsak */ /*=============================*/ /* Set up some constants */ /* ===================== */ esc='1b'x g.cr='0a'x g.cls='0c'x g.blackon=esc'[31m' g.whiteon=esc'[32m' g.blueon=esc'[33m' g.normal=esc'[0m' g.boldon=esc'[1m' g.wback=esc'[42m' g.gback=esc'[40m' call random(,,time("s")) /*Seed random number generator*/ call open(outwin,'con:450/14/200/238/Progress window') call open(win,'raw:30/14/400/239/NumberMind') signal on break_c call title call writeln(win," I have picked a series of four random numbers"g.cr" between 0 and 7 for you to guess.") call writeln(win,g.cr" You have 10 turns in which to find it.") call writech(win,g.whiteon g.cr" Press any key to continue. "g.normal) call readch(win,1) /*================*/ /* Main game loop */ /*================*/ stoppit=0 do while ~stoppit progstring=numpick() /*Program picks its numbers*/ turn=0;call title call grid /*Initialise display*/ keepgoing=1;success=0;turn=1 do while keepgoing numstring=guess(turn) /*You pick your numbers*/ keepgoing=workitout(progstring,numstring) end call title if success then do call cpos(win,5,18,g.boldon g.whiteon||esc"[41m Got it! "g.normal g.cr) do a=1 to 4;writech(win,g.cr);end end else do call writeln(win,g.cr" I'm sorry, but you haven't succeeded."g.cr) call writeln(win," The number was"g.boldon progstring||g.normal"."g.cr) call writeln(win," Better luck next time!"g.cr) end yn=choice(" Would you like to play again? (Y/N) ") if upper(yn)='N' then stoppit=1 end call title call ending exit /*=============*/ /* Subroutines */ /*=============*/ /* Program picks its numbers */ /* ========================= */ numpick: procedure do a=1 to 8 no.a=a-1 end no.9=7 progstring='' do a=1 to 4 b=random(1,9) progstring=progstring||no.b end return progstring /* You pick your numbers */ /* ===================== */ guess: procedure expose g. turnrow. arg turn call title call writeln(win,g.boldon" Turn "turn g.normal) yn='y' do while upper(yn)='Y' call writech(win,g.cr" Enter your guess here: ") numstring='' call cpos(outwin,turnrow.turn,7,g.blueon||g.wback||g.boldon) do a=1 to 4 num=readch(win,1) if num=0|num=1|num=2|num=3|num=4|num=5|num=6|num=7 then do call writech(win,num) numstring=numstring||num call cpos(outwin,turnrow.turn,5+a*2,num" ") end else do a=a-1 end end call cpos(outwin,turnrow.1+1,3,g.normal) call writech(win,g.cr) yn=choice(" Do you want to change your mind? (Y/N) ") end return numstring /* Find matches */ /* ============ */ workitout: procedure expose turn g. success turnrow. arg progstring,numstring black=0;white=0 parse var progstring 1 p.1 2 p.2 3 p.3 4 p.4 parse var numstring 1 n.1 2 n.2 3 n.3 4 n.4 /* Find right number, right place */ do a=1 to 4 if p.a=n.a then do black=black+1 n.a="*";p.a="#" end end /* Find right number, wrong place */ do a=1 to 4 do b=1 to 4 if n.b=p.a then do white=white+1 p.a="#";n.b="*" end end end blobs=g.boldon||g.blackon||copies('a4'x,black)g.whiteon||copies('a4'x,white)g.normal call cpos(outwin,turnrow.turn,17,blobs) call cpos(outwin,turnrow.1+1,3,"") keepgoing=1 if numstring=progstring then do keepgoing=0;success=1 end turn=turn+1 if turn=11 then do keepgoing=0 end return keepgoing /* Get Y/N input */ /* ============= */ choice: procedure expose g. parse arg text yn="" do until upper(yn)='Y'|upper(yn)='N' call writech(win,text) yn=readch(win,1) call writech(win,yn g.cr) if upper(yn)~='Y'&upper(yn)~='N'then call writeln(win," Please type Y or N.") end return yn /* Initialise grid */ /* =============== */ grid: procedure expose g. turnrow. call writech(outwin,g.cls) call cpos(outwin,1,2,g.boldon||g.wback||g.blueon" NUMBERMIND "g.normal) row=3 do a=10 to 1 by -1 if a=10 then turn$=a; else turn$=" "a turn$=turn$" "g.boldon||g.blueon||g.wback" # # # # "g.gback||g.normal call cpos(outwin,row,2,turn$) turnrow.a=row row=row+1 end call cpos(outwin,row+1,3,g.boldon'a4'x||g.normal" - right number") call cpos(outwin,row+2,7,"right place") call cpos(outwin,row+3,3,g.boldon||g.whiteon'a4'x||g.normal" - right number") call cpos(outwin,row+4,7,"wrong place") call cpos(outwin,turnrow.1+1,3,"") return /* Position cursor */ /* =============== */ cpos: procedure parse arg window,row,col,text call Writech(window,'9b'x||row'3B'x||col'48'x) call Writech(window,text) return /* Clear screen */ /* ============ */ title: call writech(win,g.cls) call cpos(win,1,18,g.boldon||g.wback" NUMBERMIND "g.gback) if turn=0 then call cpos(win,2,15,g.whiteon'a9'x"1996 John Filsak") call writech(win,g.normal g.cr g.cr) return /* Closing courtesies */ /* ================== */ ending: call close(outwin) call writeln(win,g.cr" Thank you for playing NumberMind."g.cr) call writeln(win,g.blueon||g.boldon" NumberMind was written for your enjoyment by") call writech(win,g.blackon" John Filsak. ") do a=1 to 3750;end return /* Trap Ctrl-C */ /* =========== */ break_c: call title call writeln(win," Program halted.") call ending exit