Compare commits

...

3 Commits

Author SHA1 Message Date
Rampoina 82a6de5fa2 Improve comments 2023-01-03 22:17:28 +01:00
Rampoina 7665f7a1d7 - 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
2023-01-03 22:14:03 +01:00
Rampoina e96808ab9c Fix Readme 2023-01-03 20:57:12 +01:00
2 changed files with 23 additions and 22 deletions

View File

@ -1,36 +1,37 @@
#!/usr/bin/env BQN
# 2048 game
# The game is controlled with the vim movement keys:
# h: left, j: down, k: up, l: right
# it needs a VT-100 compatible terminal
# h: left, j: down, k: up, l: right, q: quit
# needs a VT-100 compatible terminal
Merge{ # Merges a row of tiles to the right
moved4 0/𝕩 # remove empty tiles to move the tiles as far as possible
m0˜(¬`=˝˘)2moved # which tiles to merge
r(){tm m1m t𝕨𝕩,(¯1𝕨)𝕩+¯1𝕨@}˜´moved # Merge tiles
4/r # Resize the result
}
Step>·Merge¨<˘ # Merges each row of the board
UpStep() # Each direction merges the board
DownStep # by rotating it to the correct orientation, merging the rows and reversing the rotation
Merge{𝕩 0/ m<`=«𝕩 4(¬»m)/𝕩×1+m} # Merge a single row to the right
StepMerge˘ # Merges each row of the board
UpStep() # Each direction merges the board
DownStep # by rotating it to the correct orientation, merging the rows and reversing the rotation
RightStep
LeftStep(˘)
Spawn{i(•rand.Range)(0= / )𝕩 2˙(i) 𝕩} # Spawns a 2 at a random empty position
Spawn{i•rand.Range(0= / )𝕩 (•rand.Range91/24)(i) 𝕩} # Spawns a 2 or a 4 (10% chance) at a random empty position
LoseLeftRightDownUp # Losing condition, no moves change the board
Win´·˝2048= # Winning condition, 2048!
boardSpawn 440
e@+27
•term.RawMode 1 #
•term.RawMode 1
Quit{•Out e"[?12l"e"[?25h" •Exit 𝕩} # Restores the terminal and exits
•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"
•Show board
key•term.CharB @ # Read key
{𝕤•Out e"[?12l"e"[?25h"•Exit 0}(key='q')@ # Quit key?
move(key="hjkl")/LeftDownUpRight # Get movement function from the key
{𝕤boardSpawnMove 𝕩}(Move) board # Generate the next board if the move is valid
{𝕤•Out "You win!" •Exit 0}Win board
{𝕤•Out "You lose!" •Exit 1}Lose board
}•_While_{𝕤¬Lose board}@
•Out e"[?12l"e"[?25h" # Restore the cursor
key"hjklq"? # Valid key?
{𝕤•Out e"[?12l"e"[?25h" Quit 0}(key='q')@ # Quit key?
move(key="hjkl")/LeftDownUpRight # Get movement function from the key
{𝕤boardSpawnMove 𝕩}(Move) board # Generate the next board if the move is valid
•Out e"[H" # Cursor to origin
•Show board
{𝕤•Out "You win!" Quit 0}Win board
{𝕤•Out "You lose!" Quit 1}Lose board
; @
}•_While_{𝕤1}@

View File

@ -11,7 +11,7 @@ Vim style movement keys
## Running
If you already have BQN and you have it on your path just run `./soko.bqn`
If you already have BQN and you have it on your path just run `./2048.bqn`
Otherwise: