Anonymous user
Flipping bits game: Difference between revisions
→{{header|REXX}}: redid the displaying of the grid and added more error checks.
(example 3 by 3) |
(→{{header|REXX}}: redid the displaying of the grid and added more error checks.) |
||
Line 1,737:
=={{header|REXX}}==
This REXX version allows the specification (on the invocation line) for:
:::* the size of the array (grid) [default is 3]
:::* the number of bits (for the target) to be set [default is 3]
:::* the characters which are used for the '''on''' and '''off''' [defaults are '''1''' and '''0''']
<lang rexx>/*REXX program presents a "flipping bit" puzzle,
parse arg N u on off .; tries=0 /*get optional arguments. */
if N=='' | N==',' then N=3 /*Size given? Then use default.*/
Line 1,748 ⟶ 1,747:
if on =='' then on =1 /*character used for "on". */
if off=='' then off=0 /*character used for "off". */
col@='a b c d e f g h i j k l m n o p' /*letters to be used for col id. */
@.=off; !.=off /*set both arrays to "off" chars.*/
do while show(0)<u /* [↓] turn "on" U bits.*/
r=random(1,N); c=random(1,N)
@.r.c=on ; !.r.c=on /*set (both) row & column to ON*/
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.*/
Line 1,769:
end /*until···*/
call show 1, ' ◄───your
say '─────────Congrats! You did it in' tries "tries."
exit tries /*stick a fork in it, we're done.*/
Line 1,776:
do c=1 for N while x=='R'; if @.#.c==on then @.#.c=off; else @.#.c=on; end
do r=1 for N while x=='C'; if @.r.#==on then @.r.#=off; else @.r.#=on; end
/*──────────────────────────────────PROMPTER subroutine─────────────────*/
prompt: if tries\==0 then say '─────────bit array after
!='─────────Please enter a row
call show 1, ' ◄───your
do forever; ok=1; say; say !; pull ?
if abbrev('QUIT',?,1)
call show 1,' ◄───your array'
if \isInt(?) & \isLet(?) then call terr 'illegal row/col: ' ?
▲ else num=num/1 /*normalize the #.*/
if
if
if isLet(?) then if a<1 | a>N then call terr 'illegal column: ' ?
if isLet(?) then ?='C'pos(?,cols)
if ok then leave /*No errors? Leave*/
end /*forever*/
Line 1,796 ⟶ 1,799:
return ? /*return response.*/
/*──────────────────────────────────SHOW subroutine─────────────────────*/
show:
if tell
do r=1 for N
do c=1 for N
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*/
if tx\=='' then tar.r=_ tx
if tell then say ' 'r ' ║'_ tx; _= /*show array?*/
end /*r*/
if tell then say /*blank line.*/
return $ /*$=# bits ON*/
/*──────────────────────────────────one-liner subroutines───────────────*/
isInt: return datatype(arg(1),'W')
terr: if ok then say '***error!***:' arg(1); ok=0; return</lang>▼
isLet: return datatype(arg(1),'M')
'''output''' when using the default input of: <tt> 3 </tt>
<br>Note that the user's input is also shown
<br>Also note that the 2<sup>nd</sup> answer was a blank (or nothing), which caused the program to re-show the target array.
<pre>
a b c column letter
0 0 1 ◄───target▼
row ╔═══════
0 0 0 ◄───target▼
1 ║ 0 1 1 ◄───target
3 ║ 1 0 0 ◄───target
a b c column letter
row ╔═══════
1 ║ 1 0 1 ◄───your array
2 ║ 0 0 1 ◄───your array
3 ║ 0 1 0 ◄───your array
2
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:
a b c column letter
row ╔═══════
1 ║ 0 1 1 ◄───target
▲ 2 ║ 0 0 0 ◄───target
3 ║ 1 0 0 ◄───target
▲─────────Please enter a row or col number (as r1 or c3), or Quit:
▲─────────bit array after move: 1
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
b
─────────bit array after
a b c column letter
row ╔═══════
1 ║ 1 1 1 ◄───your array
2 ║ 1 0 0 ◄───your array
3 ║ 0 0 0 ◄───your array
─────────Please enter a row
a
a b c column letter
row ╔═══════
1 ║ 0 1 1 ◄───your array
2 ║ 0 0 0 ◄───your array
3 ║ 1 0 0 ◄───your array
─────────Congrats! You did it in 3 tries.
|