Anonymous user
Flipping bits game: Difference between revisions
m
→{{header|REXX}}: changed/added comments and whitespace, changed indentations.
m (→{{header|Java}}: small changes) |
m (→{{header|REXX}}: changed/added comments and whitespace, changed indentations.) |
||
Line 2,391:
:::* the characters which are used for the '''on''' and '''off''' (bits) [defaults are '''1''' and '''0''']
<br>Programming note: none of the command line parameters are checked for errors (so as to make the program simpler).
<lang rexx>/*REXX program presents a "flipping bit" puzzle
parse arg N u on off . /*get optional arguments
if N=='' | N==
if u=='' | u==
if on =='' then on =1 /*the character used for "on". */
if off=='' then off=0 /*
col@= 'a b c d e f g h i j k l m n o p q r s t u v w x y z' /*for
cols=space(col@,0); upper cols /*letters to be used for the columns. */
@.=off; !.=off /*set both arrays to "off"
tries=0 /*
do while show(0) < u /* [↓] turn "on"
r=random(1,N); c=random(1,N) /*get a random row and column. */
@.r.c=on ; !.r.c=on /*set (both) row
end /*while*/ /* [↑] keep going 'til U bits set.*/
oz=z /*keep the original array string. */
call show 1, ' ◄───target' /*show target for user to attain. */
do random(1,2);
end /*random···*/
if z==oz then call flip 'R',random(1,N) /*ensure it's not
do until z==oz /*prompt until they get it right. */
call prompt /*get a row or column
call flip left(?,1),substr(?,2) /*flip a user selected row or
call show 0 /*get image (Z) of the updated array. */
end /*until···*/
call show 1, ' ◄───your array' /*display the array to the
say '─────────Congrats! You did it in' tries "tries."
exit tries /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
do c=1 for N while x=='R'; if @.#.c==on then @.#.c=off; else @.#.c=on; end▼
isLet: return datatype(arg(1),'M') /*returns 1 if arg is a letter. */▼
/*──────────────────────────────────────────────────────────────────────────────────────*/
prompt: if tries\==0 then say '─────────bit array after play: ' tries▼
!='─────────Please enter a row number or column letter, or Quit:'▼
/*──────────────────────────────────────────────────────────────────────────────────────*/
▲prompt: if tries\==0 then say '─────────bit array after play: ' tries
signal on halt /*another method for the player to quit*/
▲ !='─────────Please enter a row number or column letter, or Quit:'
call show 1, ' ◄───your array' /*display the array to the terminal. */
do forever; ok=1; say; say !; pull ? _ . 1 all /*prompt & get ans*/
if abbrev('QUIT',?,1) then do; say 'quitting···'; exit 0; end
if ?=='' then do; call show 1,
call show 1,
end /* [↑] reshow targ*/
if _\=='' then call terr 'too many args entered:' all
if \isInt(?) & \isLet(?) then call terr 'row/column: '
if isLet(?) then a=pos(?,cols)
if isLet(?) & (a<1 | a>N) then call terr 'column: '
if isLet(?) & length(?)>1 then call terr 'column: '
if isLet(?) then ?='C'pos(?,cols)
if isInt(?) & (?<1 | ?>N) then call terr 'row: '
if isInt(?) then ?=?/1 /*normalize number*/
if isInt(?) then ?='R'?
if ok then leave /*No errors? Leave*/
end /*forever*/ /*
tries=tries+1 /*bump da counter.*/
return ? /*return response.*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
show: $=0; _=; z=; parse arg tell,tx,o /*$: # of ON bits.*/
if tell then do; say /*are we telling? */
▲ end /* [↑] grid hdrs.*/
do r=1 for N /*show grid rows.*/
do c=1 for N /*build grid cols.*/
if o==. then do; z=z || !.r.c; _=_ !.r.c; $=$+(!.r.c==on); end
else do; z=z || @.r.c; _=_ @.r.c; $=$+(@.r.c==on); end
end /*c*/ /*··· and sum ONs.*/
if tx\=='' then tar.r=_ tx /*build da target?*/
if tell then say right(r,2) ' ║'_ tx; _= /*show the grid? */
end /*r*/ /*show a grid row.*/
if tell then say /*show blank line.*/
return $ /*$: # of ON bits.*/</lang>
'''output''' when using the default input of: <tt> 3 </tt>▼
▲isInt: return datatype(arg(1),'W') /*returns 1 if arg is an int. */
▲isLet: return datatype(arg(1),'M') /*returns 1 if arg is a letter*/
▲'''output''' when using the default input of: <tt> 3 </tt>
<br>Note that the user's input is also shown.
<pre style="height:75ex">
a b c column letter
Line 2,546:
─────────Congrats! You did it in 3 tries.
</pre>
'''output''' when the following was used for input: <tt> 4 , + - </tt>
<pre>
a b c d column letter
|