Anonymous user
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:
:::* the size of the array (grid) [the default is '''3''', the maximum is '''26''']
:::* the number of bits (for the target) to be
<br>Programming note: none of the command line parameters ('''N''' and '''u''') 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
if u=='' | u=="," then u
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'
cols=space(col@, 0);
@.=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)
@.r.c=1 ; !.r.c=1
end /*while*/ /* [↑] keep going 'til U bits set.*/
oz=z /*
call show 1, '
do random(1,2); call flip 'R',random(1,N) /*flip a row of bits. */
end /*random*/
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. */
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
hdr: aaa=arg(1); if oo==1 then aaa=translate(aaa, "╔═║", '┌─│'); say aaa; return
terr: if ok then say '───────── ***error***: illegal' arg(1); ok=0; return
/*──────────────────────────────────────────────────────────────────────────────────────*/
flip:
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. */
if
end
if _ \== '' then call terr 'too many args entered:'
if
if
if isLet(?) & (a<1 | a>N | length(?)>1) then call terr 'column: '
if isLet(?)
if
if
▲ if isInt(?) then ?=?/1 /*normalize number*/
if ok then leave /*No errors? Leave*/▼
end /*forever*/ /*end of da checks*/
return ? /*return response.*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
show: $=0; _=; parse arg tell,tx,o
if tell then do; say; say ' ' subword(col@, 1, N) " column
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;
else do; z=z || @.r.c;
end /*c*/ /*··· and sum ONs.*/
if tx\=='' then tar.r=_ tx /*build da target?*/
if tell then
end /*r*/ /*show a grid row.*/
▲'''output''' when using the default input of: <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
2 ║ 0 0 0
3 ║
a b c column letter
row ┌───────
1
2
3
─────────Please enter a row number or a column letter, or Quit:
─────────bit array after play: 1
a b c column letter
row ┌───────
1
2
3
─────────Please enter a row number or a column letter, or Quit:
─────────bit array after play: 2
a b c column letter
row ┌───────
1
2
3
─────────Please enter a row number or a column letter, or Quit:
a b c column letter
row ┌───────
1
2
3
─────────Congrats! You did it in 3 tries.
</pre>
<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>
|