RosettaCodeData/Task/Flipping-bits-game/APL/flipping-bits-game.apl
2023-07-01 13:44:08 -04:00

68 lines
2 KiB
APL
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/local/bin/apl -s --
rb FlipRow ix ⍝ Flip a row
rb
r[ix;]~r[ix;]
rb FlipCol ix ⍝ Flip a column
rb
r[;ix]~r[;ix]
rRandFlip b;ix ⍝ Flip a random row or column
ix?b
(2|?2)/col
rb FlipRow ix 0
col: rb FlipCol ix
sttl ShowBoard b;d ⍝ Add row, column indices and title to board
s'-'(' ',⎕UCS 48+d),(⎕UCS 64+db)b
s((2s)ttl)s
bMkBoard n ⍝ Generate random board
b(?(n,n)2)-1
Game;n;board;goal;moves;swaps;in;tgt
⍝⍝ Initialize
⎕RL(2*32)|×/⎕TS ⍝ random seed from time
(5⎕ARG)/usage ⍝ check argument
(~'0123456789'.n5⎕ARG)/usage
((3>n)8<nn)/usage
boardgoalMkBoard n ⍝ Make a random board of the right size
swaps4+?16 ⍝ 5 to 20 swaps
board(RandFlipswaps)board
moves0
'*** Flip the bits! ***'
'----------------------'
⍝⍝ Print game state
state: ''
'Swaps:',moves,' Goal:',swaps
''
('Board'ShowBoard board),' ',' ',' ',' ','Goal'ShowBoard goal
⍝⍝ Handle move
'Press line or column to flip, or Q to quit: '
read: in321⎕FIO[41]1
(in=⎕UCS'q')/0
((97in)(tgtin-96)n)/col
((49in)(tgtin-48)n)/row
read
col: ⎕UCS in boardboard FlipCol tgt check
row: ⎕UCS in boardboard FlipRow tgt
⍝⍝ Check if player won
check: (boardgoal)/win
movesmoves+1
state
win: 'You win!'
0
usage: 'Usage:',⎕ARG[4],'[3..8]; number is board size.'
Game
)OFF