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 |
<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". */ |
||
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=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 |
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 |
|||
/*──────────────────────────────────PROMPTER subroutine─────────────────*/ |
/*──────────────────────────────────PROMPTER subroutine─────────────────*/ |
||
prompt: if tries\==0 then say '─────────bit array after |
prompt: if tries\==0 then say '─────────bit array after play: ' tries |
||
!='─────────Please enter a row |
!='─────────Please enter a row number or column letter, or Quit:' |
||
call show 1 |
call show 1, ' ◄───your array' /*display the array to the screen*/ |
||
do forever; ok=1; say; say !; pull ? |
do forever; ok=1; say; say !; pull ? . /*prompt & get ans*/ |
||
if abbrev('QUIT',?,1) |
if abbrev('QUIT',?,1) then exit 0 /*user wants out ?*/ |
||
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' |
|||
end |
|||
if \isInt(?) & \isLet(?) then call terr 'illegal row/col: ' ? |
|||
⚫ | |||
if |
if isInt(?) then if ?<1 | ?>N then call terr 'illegal row: ' ? |
||
if |
if isInt(?) then ?='R'? |
||
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: |
show: $=0; _=; z=; parse arg tell,tx,o /*$: on bits.*/ |
||
if tell then do |
|||
say /*blank line.*/ |
|||
say ' ' subword(col@,1,N) " column letter" |
|||
say 'row ╔'copies('═',N+N+1) |
|||
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') |
|||
⚫ | |||
isLet: return datatype(arg(1),'M') |
|||
⚫ | |||
'''output''' when using the default input of: <tt> 3 </tt> |
'''output''' when using the default input of: <tt> 3 </tt> |
||
<br>Note that the user's input is also shown |
<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 |
|||
⚫ | |||
row ╔═══════ |
|||
⚫ | |||
0 1 1 ◄───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: |
|||
0 0 0 |
|||
1 1 0 |
|||
1 0 1 |
|||
a b c column letter |
|||
row ╔═══════ |
|||
1 ║ 0 1 1 ◄───target |
|||
⚫ | |||
3 ║ 1 0 0 ◄───target |
|||
⚫ | |||
r2 |
|||
⚫ | |||
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 |
─────────Please enter a row number or column letter, or Quit: |
||
b |
|||
c3 |
|||
─────────bit array after |
─────────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 |
─────────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. |