- Improve Merge function, thanks dzaima!
- Spawn now spawns a random 4 with a 10% chance - Fix crash when using an invalid key - Fix cursor not being restored properly
This commit is contained in:
parent
e96808ab9c
commit
7665f7a1d7
36
2048.bqn
36
2048.bqn
|
@ -3,34 +3,34 @@
|
||||||
# h: left, j: down, k: up, l: right
|
# h: left, j: down, k: up, l: right
|
||||||
# it needs a VT-100 compatible terminal
|
# it needs a VT-100 compatible terminal
|
||||||
|
|
||||||
Merge←{ # Merges a row of tiles to the right
|
Merge←{𝕩 0⊸≠⊸/↩ ⋄ m←<`=⟜«𝕩 ⋄ 4↑(¬»m)/𝕩×1+m}⌾⌽ # Merge a single row to the right
|
||||||
moved←4⊸↑⌾⌽ 0⊸≠⊸/𝕩 # remove empty tiles to move the tiles as far as possible
|
Step←Merge˘ # Merges each row of the board
|
||||||
m←⌽0∾˜(¬⊸∧`=˝˘)⌾⌽2↕moved # which tiles to merge
|
Up←Step⌾(⌽⍉∘⌽) # Each direction merges the board
|
||||||
r←(⟨∞⟩){t←⊑m ⋄ m↩1↓m ⋄ t◶⟨𝕨∾𝕩,(¯1↓𝕨)∾𝕩+¯1⊑𝕨⟩@}˜´moved # Merge tiles
|
Down←Step⌾⍉ # by rotating it to the correct orientation, merging the rows and reversing the rotation
|
||||||
4⊸↑⌾⌽⌽∞⊸≠⊸/r # Resize the result
|
|
||||||
}
|
|
||||||
Step←>·Merge¨<˘ # Merges each row of the board
|
|
||||||
Up←Step⌾(⌽⍉∘⌽) # Each direction merges the board
|
|
||||||
Down←Step⌾⍉ # by rotating it to the correct orientation, merging the rows and reversing the rotation
|
|
||||||
Right←Step
|
Right←Step
|
||||||
Left←Step⌾(⌽˘)
|
Left←Step⌾(⌽˘)
|
||||||
Spawn←{i←(•rand.Range∘≠⊑⊢)(0⊸= /○⥊ ↕∘≢)𝕩 ⋄ 2˙⌾(i⊸⊑) 𝕩} # Spawns a 2 at a random empty position
|
Spawn←{i←•rand.Range∘≠⊸⊑(0⊸= /○⥊ ↕∘≢)𝕩 ⋄ (•rand.Range∘≠⊸⊑9‿1/2‿4)⌾(i⊸⊑) 𝕩} # Spawns a 2 or a 4 (10% chance) at a random empty position
|
||||||
Lose←Left∘Right∘Down∘Up⊸≡ # Losing condition, no moves change the board
|
Lose←Left∘Right∘Down∘Up⊸≡ # Losing condition, no moves change the board
|
||||||
Win←∨´·∨˝2048⊸= # Winning condition, 2048!
|
Win←∨´·∨˝2048⊸= # Winning condition, 2048!
|
||||||
|
|
||||||
board←Spawn 4‿4⥊0
|
board←Spawn 4‿4⥊0
|
||||||
e←@+27
|
e←@+27
|
||||||
•term.RawMode 1 #
|
•term.RawMode 1
|
||||||
|
Quit←{•Out e∾"[?12l"∾e∾"[?25h" ⋄ •Exit 𝕩}
|
||||||
•Out e∾"[?25l"∾e∾"[2J"∾e∾"[H" # Cursor to origin, hide it and clear screen
|
•Out e∾"[?25l"∾e∾"[2J"∾e∾"[H" # Cursor to origin, hide it and clear screen
|
||||||
{𝕤⋄
|
{𝕤⋄
|
||||||
•Out e∾"[H" # Cursor to origin
|
•Out e∾"[H"∾e∾"[2J" # Cursor to origin
|
||||||
•Out "Controls: h: left, j: down, k: up, l: right, q: quit"
|
•Out "Controls: h: left, j: down, k: up, l: right, q: quit"
|
||||||
•Show board
|
•Show board
|
||||||
|
|
||||||
key←•term.CharB @ # Read key
|
key←•term.CharB @ # Read key
|
||||||
{𝕤⋄•Out e∾"[?12l"∾e∾"[?25h"⋄•Exit 0}⍟(key='q')@ # Quit key?
|
⊑key∊"hjklq"?
|
||||||
move←⊑(key="hjkl")/Left‿Down‿Up‿Right # Get movement function from the key
|
{𝕤⋄•Out e∾"[?12l"∾e∾"[?25h"⋄ Quit 0}⍟(key='q')@ # Quit key?
|
||||||
{𝕤⋄board↩Spawn∘Move 𝕩}⍟(Move⊸≢) board # Generate the next board if the move is valid
|
move←⊑(key="hjkl")/Left‿Down‿Up‿Right # Get movement function from the key
|
||||||
{𝕤⋄•Out "You win!" ⋄ •Exit 0}⍟Win board
|
{𝕤⋄board↩Spawn∘Move 𝕩}⍟(Move⊸≢) board # Generate the next board if the move is valid
|
||||||
{𝕤⋄•Out "You lose!" ⋄ •Exit 1}⍟Lose board
|
•Out e∾"[H" # Cursor to origin
|
||||||
|
•Show board
|
||||||
|
{𝕤⋄•Out "You win!" ⋄ Quit 0}⍟Win board
|
||||||
|
{𝕤⋄•Out "You lose!"⋄ Quit 1}⍟Lose board
|
||||||
|
; @
|
||||||
}•_While_{𝕤⋄¬Lose board}@
|
}•_While_{𝕤⋄¬Lose board}@
|
||||||
•Out e∾"[?12l"∾e∾"[?25h" # Restore the cursor
|
|
||||||
|
|
Loading…
Reference in New Issue