Flipping bits game: Difference between revisions

→‎{{header|REXX}}: added/changed comments and whitespace, differentiated the target grid from the user's grid, used more consise IF statements.
m (→‎{{header|REXX}}: changed wording in the REXX section header.)
(→‎{{header|REXX}}: added/changed comments and whitespace, differentiated the target grid from the user's grid, used more consise IF statements.)
Line 3,471:
you won on turn #2</pre>
 
 
== Headline text ==
=={{header|REXX}}==
This REXX version allows the specification (on the invocation line) for:
:::* &nbsp; the size of the array (grid) &nbsp; [the default is '''3''', &nbsp; the maximum is '''26''']
:::* &nbsp; the number of bits (for the target) to be setflipped tois &nbsp;the '''on'''size of the grid &nbsp; [default(the iswhole sizerow ofor the grid]column)
<br>Programming note: &nbsp; none of the command line parameters &nbsp; ('''N''' &nbsp; and &nbsp; '''u''') &nbsp; are checked for errors (so as to make the
<br>program simpler).
<lang rexx>/*REXX program presents a "flipping bit" puzzle. The user can solve via it via C.L. */
parse arg N u . /*get optional arguments from the C.L. */
if N=='' | N=="," then N =3 /*Size given? Then use default of 3.*/
if u=='' | u=="," then u =N /*the number of bits initialized to ON.*/
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' /*literal for the column id.*/
cols=space(col@, 0); upper cols upper cols /*letters to be used for the columns. */
@.=0; !.=0 /*set both arrays to "off" characters.*/
tries=0 /*number of player's attempts (so far).*/
do while show(0) < u /* [↓] turn "on" U number of bits.*/
r=random(1, N); c=random(1, N) /*get a random row and column. */
@.r.c=1 ; !.r.c=1 /*set (both) row and column to ON. */
end /*while*/ /* [↑] keep going 'til U bits set.*/
oz=z /*keepsave the original array string. */
call show 1, ' ◄───target◄═══target═══╣', , 1 /*showdisplay the target for user to attain. */
do random(1,2); call flip 'R',random(1,N) /*flip a row of bits. */
 
do random(1,2); call flip 'RC',random(1,N) /*flip a " " column row" of" bits. */
end /*random*/ call flip 'C',random(1,N) /* " " column " " /* [↑] just perform 1 or 2 times. */
end /*random···*/ /* [↑] just perform 1 or 2 times. */
 
if z==oz then call flip 'R', random(1, N) /*ensure it's not target we're flipping*/
do until z==oz; call prompt /*prompt until they get it right. */
 
do until z==oz call flip left(?, 1), substr(?, 2) /*promptflip untila theyuser getselected itrow rightor column. */
call prompt /*get a row or column number from C.L. */
call flip left(?, 1), substr(?, 2) /*flip a user selected row or column. */
call show 0 /*get image (Z) of the updated array. */
end /*until···*/
 
call show 1, ' ◄───your array' /*display the array to the terminal. */
say '─────────Congrats! You did it in' tries "tries."
exit tries /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
halt: say 'program was halted! by user.'; exit /*the REXX program was halted by user. */
hdr: aaa=arg(1); if oo==1 then aaa=translate(aaa, "╔═║", '┌─│'); say aaa; return
isInt: return datatype(arg(1), 'W') /*returns 1 if arg is an integer.*/
isLetisInt: return datatype( arg(1), 'MW') /*returns 1 if arg is aan letterinteger. */
terrisLet: return ifdatatype( okarg(1), 'M') then say '***error!***: illegal' arg(1); /*returns 1 ok=0;if arg is a letter. return*/
terr: if ok then say '───────── ***error***: illegal' arg(1); ok=0; return
/*──────────────────────────────────────────────────────────────────────────────────────*/
flip: parse arg x,#; do c=1 for N while /*X is x=='R or C'; @.#.c = \@.#:.c; isend which one./*c*/
do cr=1 for N while x=='RC'; @.#r.c# = \@.#r.c#; end /*cr*/; return
do r=1 for N while x=='C'; @.r.#=\@.r.#; end /*r*/
return /* [↑] the bits can be ON or OFF. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
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 a column letter, or Quit:'
call show 1, ' ◄───your array' /*display the array to the terminal. */
ifdo isInt(?)forever until ok; ok=1; then ?=?/1 say; say !; pull ? _ . 1 /*normalize number*/aa
 
doif forever;abbrev('QUIT', ?, ok=1;) say; saythen !do; say pull'─────────quitting···'; ? _exit . 1 all /*prompt &0; get ans*/end
if abbrev(?=='QUIT', ?, 1) then do; say 'quitting···';call show exit1," 0; end◄═══target═══╣",.,1; ok=0
if ?=='' then do; call show 1," ◄───target",.;◄───your ok=0array"
end call show 1," ◄───your array" /* [↑] reshow targ*/
if _ \== '' then call terr 'too many args entered:' end /* [↑] reshow targ*/aa
if _\==''isInt(?) & \isLet(?) then call terr 'row/column: ' then call terr 'too many args entered:' all?
if \isInt(?) & \isLet(?) then call terr 'row/column: ' then a=pos(?, cols)
if isLet(?) & (a<1 | a>N | length(?)>1) then call terr 'column: ' then a=pos(?,cols)
if isLet(?) & (a<1 | a>N) then call terr 'column: ' then ?='C'pos(?, cols)
if isLet isInt(?) & length(?)><1 | ?>N) then call terr 'columnrow: ' ?
if isLet isInt(?) then ?='CR'pos || (?, cols/1) /*normalize number*/
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*/ /*end of da checks*/
return tries= tries + 1 /* [↑] the bits can be ON or OFF. /*bump da counter.*/
 
tries=tries+1 /*bump da counter.*/
return ? /*return response.*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
show: $=0; _=; parse arg tell,tx,o ,oo /*$≡num of ON bits*/
if tell then do; say; say ' ' subword(col@, 1, N) " column /*are we telling? */letter"
say ' call hdr 'row subword┌'copies(col@,1'─', N+N+1) " column letter" /*prepend col hdrs*/
say 'row ╔'copies('═',N+N+1) /*prepend col hdrs*/
end /* [↑] grid hdrs.*/
z= /* [↓] build grid.*/
do r=1 for N /*show grid rows.*/
do c=1 for N; if o==. then do; z=z || !.r.c; _=_ !.r.c; $=$ + !.r.c; end
else do; z=z || @.r.c; _=_ @.r.c; $=$ + @.r.c; end
end /*c*/ /*··· and sum ONs.*/
if tx\=='' then tar.r=_ tx /*build da target?*/
if tell then saycall hdr right(r, 2) ' '_ tx; _= /*show the grid? */
end /*r*/ /*show a grid row.*/
if oktell then say; then leave return $ /*Noshow errors?blank Leaveline?*/</lang>
'''{{out|output''' |text=&nbsp; when using the default input of: &nbsp; &nbsp; <tt> 3 </tt>}}
 
<br>Note that the user's input is also shown &nbsp; (annotated).
if tell then say /*show blank line.*/
return $ /*num of on bits. */</lang>
'''output''' &nbsp; when using the default input of: &nbsp; <tt> 3 </tt>
 
<br>Note that the user's input is also shown.
 
Also note that the 2<sup>nd</sup> answer was a blank (or nothing), which caused the program to re-show the target array.
Line 3,567 ⟶ 3,555:
a b c column letter
row ╔═══════
1 ║ 0 1 10 ◄───target◄═══target═══╣
2 ║ 0 0 0 ◄───target◄═══target═══╣
3 ║ 1 0 01 1 ◄───target◄═══target═══╣
 
 
a b c column letter
row ┌───────
row ╔═══════
1 1 0 10 ◄───your array
2 0 01 1 0 ◄───your array
3 0 1 0 ◄───your array
 
 
─────────Please enter a row number or a column letter, or Quit:
2b ◄■■■■■■■■■■■■■ user input
─────────bit array after play: 1
 
a b c column letter
row ┌───────
row ╔═══════
1 1 0 1 0 ◄───your array
2 1 10 0 ◄───your array
3 0 10 0 ◄───your array
 
 
─────────Please enter a row number or a column letter, or Quit:
a ◄■■■■■■■■■■■■■ user input (a null)
 
a b c column letter
row ╔═══════
1 ║ 0 1 1 ◄───target
2 ║ 0 0 0 ◄───target
3 ║ 1 0 0 ◄───target
 
 
a b c column letter
row ╔═══════
1 ║ 1 0 1 ◄───your array
2 ║ 1 1 0 ◄───your array
3 ║ 0 1 0 ◄───your array
 
 
─────────Please enter a row number or column letter, or Quit:
b ◄■■■■■■■■■■■■■ user input
─────────bit array after play: 2
 
a b c column letter
row ┌───────
row ╔═══════
1 10 1 10 ◄───your array
2 10 0 0 ◄───your array
3 01 0 0 ◄───your array
 
 
─────────Please enter a row number or a column letter, or Quit:
a3 ◄■■■■■■■■■■■■■ user input
 
a b c column letter
row ┌───────
row ╔═══════
1 0 1 10 ◄───your array
2 0 0 0 ◄───your array
3 ║ 1 0 01 1 ◄───your array
 
─────────Congrats! You did it in 3 tries.
</pre>
'''{{out|output''' |text=&nbsp; when using the following was used for input: &nbsp; &nbsp; <tt> 4 </tt>}}
<pre>
a b c d column letter
Line 3,649 ⟶ 3,621:
─────────Please enter a row number or column letter, or Quit:
q ◄■■■■■■■■■■■■■ user input
─────────quitting···
</pre>