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:
 
=={{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, touser thecan usersolve at thevia 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. */
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". */
@.cols=off 'ABCDEFGHIJKLMNOP' /*set the arrayletters to be "off"used for charscolumns.*/
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 /*setget a random row,column toand ON column.*/
@.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 array' /*display the array to the screen*/
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
return else num=num/1* [↑] bits can be ON /*normalizeor the #OFF.*/
return
/*──────────────────────────────────PROMPTER subroutine─────────────────*/
prompt: if tries\==0 then say '─────────bit array after moveplay: ' tries
!='─────────Please enter a row or col number (as or r1 column orletter, c3), or Quit:'
call show 1, ' ◄───your array' /*display the array. to the screen*/
 
do forever; ok=1; say; say !; pull ?; ?=space(?,0). /*prompt & get ans*/
if abbrev('QUIT',?,1) then exit 0 /*user wants out ?*/
parse varif ?=='' what 2 num then do; call show 1,' ◄───target',.; /*parse Row/Col, #*/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<1isInt(?) | num>N then call terr 'row or col then isif out?<1 of| ?>N then call terr range'illegal row: ' ?
if okisInt(?) then leave then /*No errors? Leave*/='R'?
say;if isLet(?) do row=1 for N; say tar.row; end /*row*/; say /*echo target*/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*/
 
Line 1,796 ⟶ 1,799:
return ? /*return response.*/
/*──────────────────────────────────SHOW subroutine─────────────────────*/
show: z=; $=0; _=; z=; parse arg tell,tx;,o if tell then say /*blank$: on linebits.*/
if tell then do r=1 for N
do c=1 forsay N; z=z||@.r.c; _=_ @.r.c; $=$+(@.r.c==on); end /*blank line.*/
if tx\== say ' ' subword(col@,1,N) " then tar.r=_column txletter"
if tell then say _ tx; _= /*show'row array?*/╔'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.*/
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>
 
<br>Note that the user's input is also shown &nbsp; (the '''r2''', '''c3''', and '''r3''' user responses).
<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
2 ║ 0 0 10 ◄───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 or col number (as or r1 column orletter, c3), or Quit:
2
─────────bit array after moveplay: 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 or r1 column orletter, c3), or Quit:
b
c3
─────────bit array after moveplay: 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 or r1 column orletter, c3), 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.