Flipping bits game: Difference between revisions

Content added Content deleted
(example 3 by 3)
(→‎{{header|REXX}}: redid the displaying of the grid and added more error checks.)
Line 1,737: Line 1,737:


=={{header|REXX}}==
=={{header|REXX}}==
This REXX version allows the specification for:
This REXX version allows the specification (on the invocation line) for:
:::*   the size of the array (grid)   [default is 3]
:::*   the size of the array (grid)   [default is 3]
:::*   the number of bits (for the target) to be set   [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''']
:::*   the characters which are used for the '''on''' and '''off'''   [defaults are '''1''' and '''0''']
<lang rexx>/*REXX program presents a "flipping bit" puzzle to the user at the C.L. */
<lang rexx>/*REXX program presents a "flipping bit" puzzle, user can solve via C.L.*/
/*REXX program presents a "flipping bit" puzzle, user can solve via C.L.*/
parse arg N u on off .; tries=0 /*get optional arguments. */
parse arg N u on off .; tries=0 /*get optional arguments. */
if N=='' | N==',' then N=3 /*Size given? Then use default.*/
if N=='' | N==',' then N=3 /*Size given? Then use default.*/
Line 1,748: Line 1,747:
if on =='' then on =1 /*character used for "on". */
if on =='' then on =1 /*character used for "on". */
if off=='' then off=0 /*character used for "off". */
if off=='' then off=0 /*character used for "off". */
@.=off /*set the array to "off" chars.*/
cols='ABCDEFGHIJKLMNOP' /*letters to be used for columns.*/
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.*/
do while show(0)<u /* [↓] turn "on" U bits.*/
r=random(1,N); c=random(1,N); @.r.c=on /*set row,column to ON*/
r=random(1,N); c=random(1,N) /*get a random row and column.*/
@.r.c=on ; !.r.c=on /*set (both) row & column to ON*/
end /*while*/ /* [↑] keep going 'til U bits set*/
end /*while*/ /* [↑] keep going 'til U bits set*/

oz=z /*keep the original array string.*/
oz=z /*keep the original array string.*/
call show 1, ' ◄───target' /*show target for user to attain.*/
call show 1, ' ◄───target' /*show target for user to attain.*/
Line 1,769: Line 1,769:
end /*until···*/
end /*until···*/


call show 1 /*display the array to the screen*/
call show 1, ' ◄───your array' /*display the array to the screen*/
say '─────────Congrats! You did it in' tries "tries."
say '─────────Congrats! You did it in' tries "tries."
exit tries /*stick a fork in it, we're done.*/
exit tries /*stick a fork in it, we're done.*/
Line 1,776: Line 1,776:
do c=1 for N while x=='R'; if @.#.c==on then @.#.c=off; else @.#.c=on; end
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
do r=1 for N while x=='C'; if @.r.#==on then @.r.#=off; else @.r.#=on; end
return /* [↑] bits can be ON or OFF.*/
return
/*──────────────────────────────────PROMPTER subroutine─────────────────*/
/*──────────────────────────────────PROMPTER subroutine─────────────────*/
prompt: if tries\==0 then say '─────────bit array after move: ' tries
prompt: if tries\==0 then say '─────────bit array after play: ' tries
!='─────────Please enter a row or col number (as r1 or c3), or Quit:'
!='─────────Please enter a row number or column letter, or Quit:'
call show 1 /*display array. */
call show 1, ' ◄───your array' /*display the array to the screen*/


do forever; ok=1; say; say !; pull ?; ?=space(?,0) /*prompt & get ans*/
do forever; ok=1; say; say !; pull ? . /*prompt & get ans*/
if abbrev('QUIT',?,1) then exit 0 /*user wants out ?*/
if abbrev('QUIT',?,1) then exit 0 /*user wants out ?*/
parse var ? what 2 num /*parse Row/Col, #*/
if ?=='' then do; call show 1,' ◄───target',.; ok=0
call show 1,' ◄───your array'
if what\=='R' & what\=='C' then call terr 'first char not R or C'
if \datatype(num,W) then call terr 'row or col not numeric'
end
if \isInt(?) & \isLet(?) then call terr 'illegal row/col: ' ?
else num=num/1 /*normalize the #.*/
if num<1 | num>N then call terr 'row or col is out of range'
if isInt(?) then if ?<1 | ?>N then call terr 'illegal row: ' ?
if ok then leave /*No errors? Leave*/
if isInt(?) then ?='R'?
say; do row=1 for N; say tar.row; end /*row*/; say /*echo target*/
if isLet(?) then a=pos(?,cols)
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*/
end /*forever*/


Line 1,796: Line 1,799:
return ? /*return response.*/
return ? /*return response.*/
/*──────────────────────────────────SHOW subroutine─────────────────────*/
/*──────────────────────────────────SHOW subroutine─────────────────────*/
show: z=; $=0; _=; parse arg tell,tx; if tell then say /*blank line.*/
show: $=0; _=; z=; parse arg tell,tx,o /*$: on bits.*/
do r=1 for N
if tell then do
do c=1 for N; z=z||@.r.c; _=_ @.r.c; $=$+(@.r.c==on); end
say /*blank line.*/
if tx\=='' then tar.r=_ tx
say ' ' subword(col@,1,N) " column letter"
if tell then say _ tx; _= /*show array?*/
say 'row ╔'copies('═',N+N+1)
end /*r*/
end

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.*/
if tell then say /*blank line.*/
return $ /*$=# bits ON*/
return $ /*$=# bits ON*/
/*──────────────────────────────────one-liner subroutines───────────────*/
/*──────────────────────────────────TERR subroutine─────────────────────*/
isInt: return datatype(arg(1),'W')
terr: if ok then say '***error!***:' arg(1); ok=0; return</lang>
isLet: return datatype(arg(1),'M')
terr: if ok then say '***error!***:' arg(1); ok=0; return</lang>
'''output''' when using the default input of: &nbsp; <tt> 3 </tt>
'''output''' when using the default input of: &nbsp; <tt> 3 </tt>

<br>Note that the user's input is also shown &nbsp; (the '''r2''', '''c3''', and '''r3''' user responses).
<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>
<pre>
a b c column letter
0 0 1 ◄───target
row ╔═══════
0 0 0 ◄───target
0 1 1 ◄───target
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 ║ 0 0 1 ◄───your array
3 ║ 0 1 0 ◄───your array


─────────Please enter a row number or column letter, or Quit:
2
─────────bit array after play: 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 number or column letter, or Quit:


0 0 0
1 1 0
1 0 1


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:
r2
─────────bit array after move: 1


a b c column letter
0 0 0
row ╔═══════
0 0 1
1 ║ 1 0 1 ◄───your array
1 0 1
2 ║ 1 1 0 ◄───your array
3 ║ 0 1 0 ◄───your array




─────────Please enter a row or col number (as r1 or c3), or Quit:
─────────Please enter a row number or column letter, or Quit:
b
c3
─────────bit array after move: 2
─────────bit array after play: 2


a b c column letter
0 0 1
row ╔═══════
0 0 0
1 ║ 1 1 1 ◄───your array
1 0 0
2 ║ 1 0 0 ◄───your array
3 ║ 0 0 0 ◄───your array




─────────Please enter a row or col number (as r1 or c3), or Quit:
─────────Please enter a row number or column letter, or Quit:
a
r3


a b c column letter
0 0 1
row ╔═══════
0 0 0
1 ║ 0 1 1 ◄───your array
0 1 1
2 ║ 0 0 0 ◄───your array
3 ║ 1 0 0 ◄───your array


─────────Congrats! You did it in 3 tries.
─────────Congrats! You did it in 3 tries.