Flipping bits game: Difference between revisions
Content added Content deleted
SqrtNegInf (talk | contribs) m (→{{header|Perl 6}}: fixed scramble routine) |
m (→{{header|REXX}}: simplified the program, added identifiers of the user inputs.) |
||
Line 2,674: | Line 2,674: | ||
:::* the size of the array (grid) [default is 3, maximum is 26] |
:::* the size of the array (grid) [default is 3, maximum is 26] |
||
:::* the number of bits (for the target) to be set to '''on''' [default is size of the grid] |
:::* the number of bits (for the target) to be set to '''on''' [default is size of the grid] |
||
:::* the characters which are used for the '''on''' and '''off''' (bits) [defaults are '''1''' and '''0'''] |
|||
<br>Programming note: none of the command line parameters are checked for errors (so as to make the program simpler). |
<br>Programming note: none of the command line parameters are checked for errors (so as to make the program simpler). |
||
<lang rexx>/*REXX program presents a "flipping bit" puzzle. The user can solve via it via C.L. */ |
<lang rexx>/*REXX program presents a "flipping bit" puzzle. The user can solve via it via C.L. */ |
||
parse arg N u |
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 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.*/ |
if u=='' | u=="," then u =N /*the number of bits initialized to ON.*/ |
||
if on =='' then on =1 /*the character used for "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' /*for the column id.*/ |
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' /*for the column id.*/ |
||
cols=space(col@,0); upper cols |
cols=space(col@, 0); 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).*/ |
tries=0 /*number of player's attempts (so far).*/ |
||
do while show(0) < u /* [↓] turn "on" U number of bits.*/ |
do while show(0) < u /* [↓] turn "on" U number of bits.*/ |
||
r=random(1,N); c=random(1,N) |
r=random(1, N); c=random(1, N) /*get a random row and column. */ |
||
@.r.c= |
@.r.c=1 ; !.r.c=1 /*set (both) row and 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. */ |
||
Line 2,697: | Line 2,694: | ||
end /*random···*/ /* [↑] just perform 1 or 2 times. */ |
end /*random···*/ /* [↑] just perform 1 or 2 times. */ |
||
if z==oz then call flip 'R',random(1,N) |
if z==oz then call flip 'R', random(1, N) /*ensure it's not target we're flipping*/ |
||
do until z==oz /*prompt until they get it right. */ |
do until z==oz /*prompt until they get it right. */ |
||
call prompt /*get a row or column number from C.L. */ |
call prompt /*get a row or column number from C.L. */ |
||
call flip left(?,1),substr(?,2) |
call flip left(?, 1), substr(?, 2) /*flip a user selected row or column. */ |
||
call show 0 /*get image (Z) of the updated array. */ |
call show 0 /*get image (Z) of the updated array. */ |
||
end /*until···*/ |
end /*until···*/ |
||
Line 2,710: | Line 2,707: | ||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
halt: say 'program was halted!'; exit /*the REXX program was halted by user. */ |
halt: say 'program was halted!'; exit /*the REXX program was halted by user. */ |
||
isInt: return datatype(arg(1),'W') |
isInt: return datatype(arg(1), 'W') /*returns 1 if arg is an integer.*/ |
||
isLet: return datatype(arg(1),'M') |
isLet: return datatype(arg(1), 'M') /*returns 1 if arg is a letter. */ |
||
terr: if ok then say '***error!***: illegal' arg(1); ok=0; return |
terr: if ok then say '***error!***: illegal' arg(1); ok=0; return |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
flip: parse arg x,# /*X is R or C; #: is which one.*/ |
flip: parse arg x,# /*X is R or C; #: is which one.*/ |
||
do c=1 for N while x=='R'; |
do c=1 for N while x=='R'; @.#.c=\@.#.c; end /*c*/ |
||
do r=1 for N while x=='C'; |
do r=1 for N while x=='C'; @.r.#=\@.r.#; end /*r*/ |
||
return /* [↑] the bits can be ON or OFF. */ |
return /* [↑] the bits can be ON or OFF. */ |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
Line 2,725: | Line 2,722: | ||
do forever; ok=1; say; say !; pull ? _ . 1 all /*prompt & get ans*/ |
do forever; ok=1; say; say !; pull ? _ . 1 all /*prompt & get ans*/ |
||
if abbrev('QUIT',?,1) |
if abbrev('QUIT', ?, 1) then do; say 'quitting···'; exit 0; end |
||
if ?=='' then do; call show 1," ◄───target",.; ok=0 |
if ?=='' then do; call show 1," ◄───target",.; ok=0 |
||
call show 1," ◄───your array" |
call show 1," ◄───your array" |
||
Line 2,734: | Line 2,731: | ||
if isLet(?) & (a<1 | a>N) then call terr 'column: ' ? |
if isLet(?) & (a<1 | a>N) then call terr 'column: ' ? |
||
if isLet(?) & length(?)>1 then call terr 'column: ' ? |
if isLet(?) & length(?)>1 then call terr 'column: ' ? |
||
if isLet(?) then ?='C'pos(?,cols) |
if isLet(?) then ?='C'pos(?, cols) |
||
if isInt(?) & (?<1 | ?>N) then call terr 'row: ' ? |
if isInt(?) & (?<1 | ?>N) then call terr 'row: ' ? |
||
if isInt(?) then ?=?/1 /*normalize number*/ |
if isInt(?) then ?=?/1 /*normalize number*/ |
||
Line 2,744: | Line 2,741: | ||
return ? /*return response.*/ |
return ? /*return response.*/ |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
show: $=0; |
show: $=0; _=; parse arg tell,tx,o /*$≡num of ON bits*/ |
||
if tell then do; say /*are we telling? */ |
if tell then do; say /*are we telling? */ |
||
say ' ' subword(col@,1,N) " column letter" |
say ' ' subword(col@,1,N) " column letter" |
||
say 'row ╔'copies('═',N+N+1) /*prepend col hdrs*/ |
say 'row ╔'copies('═',N+N+1) /*prepend col hdrs*/ |
||
end /* [↑] grid hdrs.*/ |
end /* [↑] grid hdrs.*/ |
||
⚫ | |||
do r=1 for N /*show grid rows.*/ |
do r=1 for N /*show grid rows.*/ |
||
do c=1 for N |
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 |
|||
else do; z=z || @.r.c; _=_ @.r.c; $=$+(@.r.c==on); end |
|||
end /*c*/ /*··· and sum ONs.*/ |
end /*c*/ /*··· and sum ONs.*/ |
||
if tx\=='' then tar.r=_ tx /*build da target?*/ |
if tx\=='' then tar.r=_ tx /*build da target?*/ |
||
if tell then say right(r,2) ' ║'_ tx; |
if tell then say right(r, 2) ' ║'_ tx; _= /*show the grid? */ |
||
end /*r*/ /*show a grid row.*/ |
end /*r*/ /*show a grid row.*/ |
||
if tell then say /*show blank line.*/ |
if tell then say /*show blank line.*/ |
||
return $ /* |
return $ /*num of on bits. */</lang> |
||
'''output''' when using the default input of: <tt> 3 </tt> |
'''output''' when using the default input of: <tt> 3 </tt> |
||
Line 2,766: | Line 2,762: | ||
Also note that the 2<sup>nd</sup> answer was a blank (or nothing), which caused the program to re-show the target array. |
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 style="height: |
<pre style="height:101ex"> |
||
a b c column letter |
a b c column letter |
||
row ╔═══════ |
row ╔═══════ |
||
Line 2,782: | Line 2,778: | ||
─────────Please enter a row number or column letter, or Quit: |
─────────Please enter a row number or column letter, or Quit: |
||
2 ◄■■■■■■■■■■■■■ user input |
|||
2 |
|||
─────────bit array after play: 1 |
─────────bit array after play: 1 |
||
Line 2,793: | Line 2,789: | ||
─────────Please enter a row number or column letter, or Quit: |
─────────Please enter a row number or column letter, or Quit: |
||
◄■■■■■■■■■■■■■ user input (a null) |
|||
a b c column letter |
a b c column letter |
||
Line 2,810: | Line 2,806: | ||
─────────Please enter a row number or column letter, or Quit: |
─────────Please enter a row number or column letter, or Quit: |
||
b ◄■■■■■■■■■■■■■ user input |
|||
b |
|||
─────────bit array after play: 2 |
─────────bit array after play: 2 |
||
Line 2,821: | Line 2,817: | ||
─────────Please enter a row number or column letter, or Quit: |
─────────Please enter a row number or column letter, or Quit: |
||
a ◄■■■■■■■■■■■■■ user input |
|||
a |
|||
a b c column letter |
a b c column letter |
||
Line 2,831: | Line 2,827: | ||
─────────Congrats! You did it in 3 tries. |
─────────Congrats! You did it in 3 tries. |
||
</pre> |
</pre> |
||
'''output''' when the following was used for input: <tt> 4 |
'''output''' when the following was used for input: <tt> 4 </tt> |
||
<pre> |
<pre> |
||
a b c d column letter |
a b c d column letter |
||
row ╔═════════ |
row ╔═════════ |
||
1 ║ |
1 ║ 1 1 0 0 ◄───target |
||
2 ║ |
2 ║ 0 1 0 1 ◄───target |
||
3 ║ |
3 ║ 0 0 0 0 ◄───target |
||
4 ║ |
4 ║ 0 0 0 0 ◄───target |
||
a b c d column letter |
a b c d column letter |
||
row ╔═════════ |
row ╔═════════ |
||
1 ║ |
1 ║ 0 1 0 1 ◄───your array |
||
2 ║ |
2 ║ 0 0 1 1 ◄───your array |
||
3 ║ |
3 ║ 1 0 0 1 ◄───your array |
||
4 ║ |
4 ║ 1 0 0 1 ◄───your array |
||
─────────Please enter a row number or column letter, or Quit: |
─────────Please enter a row number or column letter, or Quit: |
||
q ◄■■■■■■■■■■■■■ user input |
|||
q |
|||
</pre> |
</pre> |
||