home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 9 / amigaformatcd09.iso / readerstuff / john_filsak / numbermind.rexx < prev    next >
Encoding:
OS/2 REXX Batch file  |  1996-11-13  |  5.0 KB  |  214 lines

  1. /*=============================*/
  2. /* "NumberMind" game for ARexx */
  3. /*      © 1996 John Filsak     */
  4. /*=============================*/
  5.  
  6. /* Set up some constants */
  7. /* ===================== */
  8. esc='1b'x
  9. g.cr='0a'x
  10. g.cls='0c'x
  11. g.blackon=esc'[31m'
  12. g.whiteon=esc'[32m'
  13. g.blueon=esc'[33m'
  14. g.normal=esc'[0m'
  15. g.boldon=esc'[1m'
  16. g.wback=esc'[42m'
  17. g.gback=esc'[40m'
  18.  
  19. call random(,,time("s")) /*Seed random number generator*/
  20. call open(outwin,'con:450/14/200/238/Progress window')
  21. call open(win,'raw:30/14/400/239/NumberMind')
  22. signal on break_c
  23.  
  24. call title
  25. call writeln(win," I have picked a series of four random numbers"g.cr" between 0 and 7 for you to guess.")
  26. call writeln(win,g.cr" You have 10 turns in which to find it.")
  27. call writech(win,g.whiteon g.cr" Press any key to continue. "g.normal)
  28. call readch(win,1)
  29.  
  30.  
  31. /*================*/
  32. /* Main game loop */
  33. /*================*/
  34.  
  35. stoppit=0
  36. do while ~stoppit
  37.     progstring=numpick() /*Program picks its numbers*/
  38.     turn=0;call title
  39.     call grid /*Initialise display*/
  40.     keepgoing=1;success=0;turn=1
  41.     do while keepgoing
  42.         numstring=guess(turn) /*You pick your numbers*/
  43.         keepgoing=workitout(progstring,numstring)
  44.     end
  45.     call title
  46.     if success then do
  47.         call cpos(win,5,18,g.boldon g.whiteon||esc"[41m Got it! "g.normal g.cr)
  48.         do a=1 to 4;writech(win,g.cr);end
  49.     end
  50.     else do
  51.         call writeln(win,g.cr" I'm sorry, but you haven't succeeded."g.cr)
  52.         call writeln(win," The number was"g.boldon progstring||g.normal"."g.cr)
  53.         call writeln(win," Better luck next time!"g.cr)
  54.     end
  55.     yn=choice(" Would you like to play again? (Y/N) ")
  56.     if upper(yn)='N' then stoppit=1
  57. end
  58. call title
  59. call ending
  60. exit
  61.  
  62. /*=============*/
  63. /* Subroutines */
  64. /*=============*/
  65.  
  66. /* Program picks its numbers */
  67. /* ========================= */
  68. numpick: procedure
  69. do a=1 to 8
  70.     no.a=a-1
  71. end
  72. no.9=7
  73. progstring=''
  74. do a=1 to 4
  75.     b=random(1,9)
  76.     progstring=progstring||no.b
  77. end
  78. return progstring
  79.  
  80. /* You pick your numbers */
  81. /* ===================== */
  82. guess: procedure expose g. turnrow.
  83. arg turn
  84. call title
  85. call writeln(win,g.boldon" Turn "turn g.normal)
  86. yn='y'
  87. do while upper(yn)='Y'
  88.     call writech(win,g.cr" Enter your guess here: ")
  89.     numstring=''
  90.     call cpos(outwin,turnrow.turn,7,g.blueon||g.wback||g.boldon)
  91.     do a=1 to 4
  92.         num=readch(win,1)
  93.         if num=0|num=1|num=2|num=3|num=4|num=5|num=6|num=7 then do
  94.             call writech(win,num)
  95.             numstring=numstring||num
  96.             call cpos(outwin,turnrow.turn,5+a*2,num" ")
  97.         end
  98.         else do
  99.             a=a-1
  100.         end
  101.     end
  102.     call cpos(outwin,turnrow.1+1,3,g.normal)
  103.     call writech(win,g.cr)
  104.     yn=choice(" Do you want to change your mind? (Y/N) ")
  105. end
  106. return numstring
  107.  
  108. /* Find matches */
  109. /* ============ */
  110. workitout: procedure expose turn g. success turnrow.
  111. arg progstring,numstring
  112. black=0;white=0
  113. parse var progstring 1 p.1 2 p.2 3 p.3 4 p.4
  114. parse var numstring 1 n.1 2 n.2 3 n.3 4 n.4
  115.  
  116. /* Find right number, right place */
  117. do a=1 to 4
  118.     if p.a=n.a then do
  119.         black=black+1
  120.         n.a="*";p.a="#"
  121.     end
  122. end
  123.  
  124. /* Find right number, wrong place */
  125. do a=1 to 4
  126.     do b=1 to 4
  127.         if n.b=p.a then do
  128.             white=white+1
  129.             p.a="#";n.b="*"
  130.         end
  131.     end
  132. end
  133.  
  134. blobs=g.boldon||g.blackon||copies('a4'x,black)g.whiteon||copies('a4'x,white)g.normal
  135. call cpos(outwin,turnrow.turn,17,blobs)
  136. call cpos(outwin,turnrow.1+1,3,"")
  137. keepgoing=1
  138. if numstring=progstring then do
  139.     keepgoing=0;success=1
  140. end
  141. turn=turn+1
  142. if turn=11 then do
  143.     keepgoing=0
  144. end
  145. return keepgoing
  146.  
  147. /* Get Y/N input */
  148. /* ============= */
  149. choice: procedure expose g.
  150. parse arg text
  151. yn=""
  152. do until upper(yn)='Y'|upper(yn)='N'
  153.     call writech(win,text)
  154.     yn=readch(win,1)
  155.     call writech(win,yn g.cr)
  156.     if upper(yn)~='Y'&upper(yn)~='N'then call writeln(win," Please type Y or N.")
  157. end
  158. return yn
  159.  
  160. /* Initialise grid */
  161. /* =============== */
  162. grid: procedure expose g. turnrow.
  163. call writech(outwin,g.cls)
  164. call cpos(outwin,1,2,g.boldon||g.wback||g.blueon"      NUMBERMIND    "g.normal)
  165. row=3
  166. do a=10 to 1 by -1
  167.     if a=10 then turn$=a; else turn$=" "a
  168.     turn$=turn$"  "g.boldon||g.blueon||g.wback" # # # # "g.gback||g.normal
  169.     call cpos(outwin,row,2,turn$)
  170.     turnrow.a=row
  171.     row=row+1
  172. end
  173. call cpos(outwin,row+1,3,g.boldon'a4'x||g.normal" - right number")
  174. call cpos(outwin,row+2,7,"right place")
  175. call cpos(outwin,row+3,3,g.boldon||g.whiteon'a4'x||g.normal" - right number")
  176. call cpos(outwin,row+4,7,"wrong place")
  177. call cpos(outwin,turnrow.1+1,3,"")
  178. return
  179.  
  180. /* Position cursor */
  181. /* =============== */
  182. cpos: procedure
  183. parse arg window,row,col,text
  184. call Writech(window,'9b'x||row'3B'x||col'48'x)
  185. call Writech(window,text)
  186. return
  187.  
  188. /* Clear screen */
  189. /* ============ */
  190. title:
  191. call writech(win,g.cls)
  192. call cpos(win,1,18,g.boldon||g.wback" NUMBERMIND "g.gback)
  193. if turn=0 then call cpos(win,2,15,g.whiteon'a9'x"1996 John Filsak")
  194. call writech(win,g.normal g.cr g.cr)
  195. return
  196.  
  197. /* Closing courtesies */
  198. /* ================== */
  199. ending:
  200. call close(outwin)
  201. call writeln(win,g.cr" Thank you for playing NumberMind."g.cr)
  202. call writeln(win,g.blueon||g.boldon" NumberMind was written for your enjoyment by")
  203. call writech(win,g.blackon" John Filsak. ")
  204. do a=1 to 3750;end
  205. return
  206.  
  207. /* Trap Ctrl-C */
  208. /* =========== */
  209. break_c:
  210. call title
  211. call writeln(win," Program halted.")
  212. call ending
  213. exit
  214.