Flipping bits game: Difference between revisions
Content added Content deleted
(→{{header|Go}}: , gofmt, cleanup; mark as still needing improvement) |
m (→{{header|REXX}}: addec/changed comments, add another error check for too many arguments.) |
||
Line 1,849: | Line 1,849: | ||
<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, user can solve via C.L.*/ |
<lang rexx>/*REXX program presents a "flipping bit" puzzle, user can solve via C.L.*/ |
||
parse arg N u on off . |
parse arg N u on off . /*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.*/ |
||
if u=='' | u==',' then u=N /*number of bits initialized ON.*/ |
if u=='' | u==',' then u=N /*number of bits initialized ON.*/ |
||
Line 1,855: | Line 1,855: | ||
if off=='' then off=0 /*character used for "off". */ |
if off=='' then off=0 /*character used for "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 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 col id.*/ |
||
cols=space(col@,0); |
cols=space(col@,0); upper cols /*letters to be used for columns.*/ |
||
@.=off; !.=off /*set both arrays to "off" chars.*/ |
@.=off; !.=off /*set both arrays to "off" chars.*/ |
||
tries=0 /*# of player's attempts used. */ |
tries=0 /*# of player's attempts used. */ |
||
do while show(0)< |
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 |
@.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.*/ |
||
Line 1,880: | Line 1,880: | ||
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.*/ |
||
/*──────────────────────────────────FLIP |
/*──────────────────────────────────FLIP subroutine────────────────────────*/ |
||
flip: parse arg x,# /* |
flip: parse arg x,# /*X is R or C, # is which one.*/ |
||
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 /* [↑] the bits can be ON or OFF*/ |
||
/*──────────────────────────────────PROMPTER subroutine─────────────────*/ |
/*──────────────────────────────────PROMPTER subroutine─────────────────*/ |
||
prompt: if tries\==0 then say '─────────bit array after play: ' tries |
prompt: if tries\==0 then say '─────────bit array after play: ' tries |
||
Line 1,891: | Line 1,891: | ||
call show 1, ' ◄───your array' /*display the array to the screen*/ |
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 ? _ . 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,' ◄───target',.; ok=0 |
if ?=='' then do; call show 1,' ◄───target',.; ok=0 |
||
call show 1,' ◄───your array' |
call show 1,' ◄───your array' |
||
end |
end /* [↑] reshow targ*/ |
||
if \ |
if _\=='' then call terr 'too many args entered:' all |
||
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*/ /*endeth da checks*/ |
||
tries=tries+1 /*bump |
tries=tries+1 /*bump da counter.*/ |
||
return ? /*return response.*/ |
return ? /*return response.*/ |
||
/*──────────────────────────────────SHOW subroutine─────────────────────*/ |
/*──────────────────────────────────SHOW subroutine─────────────────────*/ |
||
show: $=0; _=; z=; parse arg tell,tx,o |
show: $=0; _=; z=; parse arg tell,tx,o /*$: # of ON bits.*/ |
||
if tell then do |
if tell then do /*are we telling? */ |
||
say |
say /*show blank line.*/ |
||
say ' ' subword(col@,1,N) " column letter" |
say ' ' subword(col@,1,N) " column letter" |
||
say 'row ╔'copies('═',N+N+1) |
say 'row ╔'copies('═',N+N+1) /*prepend col hdrs*/ |
||
end |
end /* [↑] grid hdrs.*/ |
||
do r=1 for N |
do r=1 for N /*show grid rows.*/ |
||
do c=1 for N |
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*/ |
|||
if tx\=='' then tar.r=_ tx |
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*/ |
end /*r*/ /*show a grid row.*/ |
||
if tell then say |
if tell then say /*show blank line.*/ |
||
return $ |
return $ /*$: # of ON bits.*/ |
||
/*──────────────────────────────────one-liner subroutines───────────────*/ |
/*──────────────────────────────────one-liner subroutines───────────────*/ |
||
halt: say 'program was halted!'; exit /*the REXX program was halted.*/ |
halt: say 'program was halted!'; exit /*the REXX program was halted.*/ |