Flipping bits game: Difference between revisions
Content added Content deleted
m (→{{header|Java}}: small changes) |
m (→{{header|REXX}}: changed/added comments and whitespace, changed indentations.) |
||
Line 2,391: | Line 2,391: | ||
:::* the characters which are used for the '''on''' and '''off''' (bits) [defaults are '''1''' and '''0'''] |
:::* 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 |
<lang rexx>/*REXX program presents a "flipping bit" puzzle. The user can solve via it via C.L. */ |
||
parse arg N u on off . /*get optional arguments |
parse arg N u on off . /*get optional arguments from the C.L. */ |
||
if N=='' | N== |
if N=='' | N=="," then N =3 /*Size given? Then use default of 3.*/ |
||
if u=='' | u== |
if u=='' | u=="," then u =N /*the number of bits initialized to ON.*/ |
||
if on =='' then on =1 /*character used for "on". */ |
if on =='' then on =1 /*the character used for "on". */ |
||
if off=='' then off=0 /* |
if off=='' then off=0 /* " " " " "off". */ |
||
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 |
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 /*letters to be used for columns.*/ |
cols=space(col@,0); upper cols /*letters to be used for the columns. */ |
||
@.=off; !.=off /*set both arrays to "off" |
@.=off; !.=off /*set both arrays to "off" characters.*/ |
||
tries=0 /* |
tries=0 /*number of player's attempts (so far).*/ |
||
do while show(0) < u /* [↓] turn "on" |
do while show(0) < u /* [↓] turn "on" U number of bits.*/ |
||
r=random(1,N); c=random(1,N) /*get a random row and column.*/ |
r=random(1,N); c=random(1,N) /*get a random row and column. */ |
||
@.r.c=on ; !.r.c=on /*set (both) row |
@.r.c=on ; !.r.c=on /*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. */ |
||
call show 1, ' ◄───target' /*show target for user to attain.*/ |
call show 1, ' ◄───target' /*show target for user to attain. */ |
||
do random(1,2); |
do random(1,2); call flip 'R',random(1,N) /*flip a row of bits. */ |
||
call flip 'C',random(1,N) /* " " column " " */ |
|||
end /*random···*/ |
end /*random···*/ /* [↑] just perform 1 or 2 times. */ |
||
if z==oz then call flip 'R',random(1,N) /*ensure it's not |
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 |
call prompt /*get a row or column number from C.L. */ |
||
call flip left(?,1),substr(?,2) /*flip a user selected row or |
call flip left(?,1),substr(?,2) /*flip a user selected row or column. */ |
||
call show 0 /*get image (Z) of updated array.*/ |
call show 0 /*get image (Z) of the updated array. */ |
||
end /*until···*/ |
end /*until···*/ |
||
call show 1, ' ◄───your array' /*display the array to the |
call show 1, ' ◄───your array' /*display the array to the terminal. */ |
||
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 all done. */ |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
|||
/*──────────────────────────────────FLIP subroutine────────────────────────*/ |
|||
halt: say 'program was halted!'; exit /*the REXX program was halted by user. */ |
|||
⚫ | |||
⚫ | |||
⚫ | |||
do r=1 for N while x=='C'; if @.r.#==on then @.r.#=off; else @.r.#=on; end |
|||
terr: if ok then say '***error!***: illegal' arg(1); ok=0; return |
|||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
|||
/*──────────────────────────────────PROMPTER subroutine─────────────────*/ |
|||
⚫ | |||
⚫ | |||
do c=1 for N while x=='R'; if @.#.c==on then @.#.c=off; else @.#.c=on; end |
|||
⚫ | |||
⚫ | |||
return /* [↑] the bits can be ON or OFF. */ |
|||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
|||
⚫ | |||
signal on halt /*another method for the player to quit*/ |
|||
⚫ | |||
call show 1, ' ◄───your array' /*display the array to the terminal. */ |
|||
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) then do; say 'quitting···'; exit 0; end |
if abbrev('QUIT',?,1) then do; say 'quitting···'; exit 0; end |
||
if ?=='' then do; call show 1, |
if ?=='' then do; call show 1," ◄───target",.; ok=0 |
||
call show 1, |
call show 1," ◄───your array" |
||
end /* [↑] reshow targ*/ |
end /* [↑] reshow targ*/ |
||
if _\=='' then call terr 'too many args entered:' all |
if _\=='' then call terr 'too many args entered:' all |
||
if \isInt(?) & \isLet(?) then call terr 'row/column: ' |
if \isInt(?) & \isLet(?) then call terr 'row/column: ' ? |
||
if isLet(?) then a=pos(?,cols) |
if isLet(?) then a=pos(?,cols) |
||
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*/ |
||
if isInt(?) then ?='R'? |
if isInt(?) then ?='R'? |
||
if ok then leave /*No errors? Leave*/ |
if ok then leave /*No errors? Leave*/ |
||
end /*forever*/ /* |
end /*forever*/ /*end of da checks*/ |
||
tries=tries+1 /*bump da counter.*/ |
tries=tries+1 /*bump da counter.*/ |
||
return ? /*return response.*/ |
return ? /*return response.*/ |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
|||
/*──────────────────────────────────SHOW subroutine─────────────────────*/ |
|||
show: $=0; _=; z=; parse arg tell,tx,o /*$: # of ON bits.*/ |
show: $=0; _=; z=; parse arg tell,tx,o /*$: # of ON bits.*/ |
||
if tell then do /*are we telling? */ |
if tell then do; say /*are we telling? */ |
||
say ' ' subword(col@,1,N) " column letter" |
|||
say 'row ╔'copies('═',N+N+1) /*prepend col 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 /*build grid cols.*/ |
do c=1 for N /*build grid cols.*/ |
||
if o==. then do; z=z || !.r.c; _=_ !.r.c; $=$+(!.r.c==on); end |
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 |
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; _= /*show the grid? */ |
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 $ /*$: # of ON bits.*/ |
return $ /*$: # of ON bits.*/</lang> |
||
⚫ | |||
/*──────────────────────────────────one-liner subroutines───────────────*/ |
|||
halt: say 'program was halted!'; exit /*the REXX program was halted.*/ |
|||
⚫ | |||
⚫ | |||
terr: if ok then say '***error!***: illegal' arg(1); ok=0; return</lang> |
|||
⚫ | |||
<br>Note that the user's input is also shown. |
<br>Note that the user's input is also shown. |
||
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:75ex"> |
<pre style="height:75ex"> |
||
a b c column letter |
a b c column letter |
||
Line 2,546: | Line 2,546: | ||
─────────Congrats! You did it in 3 tries. |
─────────Congrats! You did it in 3 tries. |
||
</pre> |
</pre> |
||
'''output''' when the following was used for input: 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 |