2048: Difference between revisions

7,974 bytes added ,  16 days ago
m
→‎{{header|FutureBasic}}: Remove FutureBasic 'Output' label
m (Automated syntax highlighting fixup (second round - minor fixes))
m (→‎{{header|FutureBasic}}: Remove FutureBasic 'Output' label)
 
(18 intermediate revisions by 8 users not shown)
Line 11:
:*   All tiles move as far as possible in that direction, some move more than others.
:*   Two adjacent tiles (in that direction only) with matching numbers combine into one bearing the sum of those numbers.
:*   A move is valid when at least one tile can be moved,   if onlyincluding by combination.
:*   A new tile with the value of   '''2'''   is spawned at the end of each turn at a randomly chosen empty square   (if there is one).
:*   Adding a new tile on a blank space.   Most of the time,   a new   '''2'''   is to be added,   andbut occasionally   ('''10%''' of the time),   a   '''4'''.
:*   To win,   the player must create a tile with the number   '''2048'''.
:*   The player loses if no valid moves are possible.
 
Line 22:
 
;Requirements:
* &nbsp; "Non-greedy" movement. &nbsp; <br>&nbsp; The tiles that were created by combining other tiles should not be combined again during the same turn (move). &nbsp; <br>&nbsp; That is to say, &nbsp; that moving the tile row of:
 
<big><big> [2][2][2][2] </big></big>
Line 34:
<big><big> .........[8] </big></big>
 
* &nbsp; "Move direction priority". &nbsp; <br>&nbsp; If more than one variant of combining is possible, &nbsp; move direction shall indicate which combination will take effect. <br>&nbsp; For example, moving the tile row of:
 
<big><big> ...[2][2][2] </big></big>
Line 48:
 
 
* &nbsp; Check for valid moves. &nbsp; The player shouldn't be able to skipgain theirnew turntile by trying a move that doesn't change the board.
* &nbsp; Check for a &nbsp;win condition.
* &nbsp; Check for a lose condition.
<br><br>
Line 2,030:
Return
</syntaxhighlight>
{{out}}
<pre>
Se invoca como:
 
rxvt -g 79x38 -fn "xft:FantasqueSansMono-Regular:pixelsize=25" -e hopper bas/2048.bas
 
</pre>
[[File:Captura_de_pantalla_de_2022-10-07_14-43-23.png]]
 
=={{header|Applesoft BASIC}}==
Line 3,574 ⟶ 3,582:
 
=={{header|BASIC}}==
==={{works withheader|QBasic}}===
El código es de MichD (https://github.com/michd/2048-qbasic)
 
Line 3,934 ⟶ 3,942:
END SUB
</syntaxhighlight>
 
 
=={{header|BBC BASIC}}==
Line 4,039 ⟶ 4,046:
--------------------
You lost :-(</pre>
 
=={{header|BQN}}==
<syntaxhighlight lang="bqn">
#!/usr/bin/env BQN
# 2048 game
# The game is controlled with the vim movement keys:
# h: left, j: down, k: up, l: right, q: quit
# needs a VT-100 compatible terminal
 
Merge←{𝕩 0⊸≠⊸/↩ ⋄ m←<`=⟜«𝕩 ⋄ 4↑(¬»m)/𝕩×1+m}⌾⌽ # Merge a single row to the right
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
Right←Step # and reversing the rotation
Left←Step⌾(⌽˘)
# Spawns a 2 or a 4 (10% chance) at a random empty position
Spawn←{i←•rand.Range∘≠⊸⊑(0⊸= /○⥊ ↕∘≢)𝕩 ⋄ (•rand.Range∘≠⊸⊑9‿1/2‿4)⌾(i⊸⊑) 𝕩}
Lose←Left∘Right∘Down∘Up⊸≡ # Losing condition, no moves change the board
Win←∨´·∨˝2048⊸= # Winning condition, 2048!
 
Quit←{•Out e∾"[?12l"∾e∾"[?25h" ⋄ •Exit 𝕩} # Restores the terminal and exits
Display←{ # Displays the board, score and controls
•Out e∾"[H"∾e∾"[2J" # Cursor to origin and clear screen
•Out "Controls: h: left, j: down, k: up, l: right, q: quit"
•Show 𝕩
•Out "score: "∾•Repr ⌈´⌈˝ 𝕩
}
 
board←Spawn 4‿4⥊0
e←@+27 # Escape character for the ANSI escape codes
•term.RawMode 1
•Out e∾"[?25l"∾e∾"[2J"∾e∾"[H" # Cursor to origin, hide it and clear screen
{𝕤⋄
Display board
{𝕤⋄•Out "You win!" ⋄ Quit 0}⍟Win board
{𝕤⋄•Out "You lose!"⋄ Quit 1}⍟Lose board
key←•term.CharB @ # Read key
⊑key∊"hjklq"? # Valid key?
{𝕤⋄ Quit 0}⍟(key='q')@ # Quit key?
move←⊑(key="hjkl")/Left‿Down‿Up‿Right # Get movement function from the key
{𝕤⋄board↩Spawn∘Move 𝕩}⍟(Move⊸≢) board # Generate the next board if the move is valid
; @
}•_While_{𝕤⋄1}@
</syntaxhighlight>
 
=={{header|C}}==
Line 5,679 ⟶ 5,730:
 
struct G2048 {
public void gameLoop() /*@safe @nogc*/ {
addTile;
while (true) {
Line 5,693 ⟶ 5,744:
 
private:
static struct Tile {
uint val = 0;
bool blocked = false;
Line 5,705 ⟶ 5,756:
uint score = 0;
 
void drawBoard() const /*@safe @nogc*/ {
writeln("SCORE: ", score, "\n");
foreach (immutable y; 0 .. side) {
write("+------+------+------+------+\n| ");
foreach (immutable x; 0 .. side) {
if (board[x][y].val)
writef("%4d", board[x][y].val);
Line 5,718 ⟶ 5,769:
writeln;
}
writeln("+------+------+------+------+\n".writeln);
}
 
void waitKey() /*@safe*/ {
moved = false;
write("(W)Up (S)Down (A)Left (D)Right (Q)Quit: ".write);
immutableauto c = readln.strip.toLower;
 
switch (c) {
Line 5,735 ⟶ 5,786:
}
 
foreach (immutable y; 0 .. side)
foreach (immutable x; 0 .. side)
board[x][y].blocked = false;
}
Line 5,745 ⟶ 5,796:
}
 
void addTile() /*nothrow*/ @safe /*@nogc*/ {
foreach (immutable y; 0 .. side) {
foreach (immutable x; 0 .. side) {
if (!board[x][y].val) {
uint a, b;
Line 5,764 ⟶ 5,815:
}
 
bool canMove() const pure nothrow @safe @nogc {
foreach (immutable y; 0 .. side)
foreach (immutable x; 0 .. side)
if (!board[x][y].val)
return true;
 
foreach (immutable y; 0 .. side) {
foreach (immutable x; 0 .. side) {
if (testAdd(x + 1, y, board[x][y].val) ||
testAdd(x - 1, y, board[x][y].val) ||
Line 5,782 ⟶ 5,833:
}
 
bool testAdd(in uint x, in uint y, in uint v) const pure nothrow @safe @nogc {
if (x > 3 || y > 3)
return false;
Line 5,788 ⟶ 5,839:
}
 
void moveVertically(in uint x, in uint y, in uint d) pure nothrow @safe @nogc {
if (board[x][y + d].val && board[x][y + d].val == board[x][y].val &&
!board[x][y].blocked && !board[x][y + d].blocked) {
Line 5,811 ⟶ 5,862:
}
 
void moveHorizontally(in uint x, in uint y, in uint d) pure nothrow @safe @nogc {
if (board[x + d][y].val && board[x + d][y].val == board[x][y].val &&
!board[x][y].blocked && !board[x + d][y].blocked) {
Line 5,834 ⟶ 5,885:
}
 
void move(in moveDir d) pure nothrow @safe @nogc {
final switch (d) with(moveDir) {
case up:
foreach (immutable x; 0 .. side)
foreach (immutable y; 1 .. side)
if (board[x][y].val)
moveVertically(x, y, -1);
break;
case down:
foreach (immutable x; 0 .. side)
foreach_reverse (immutable y; 0 .. 3)
if (board[x][y].val)
moveVertically(x, y, 1);
break;
case left:
foreach (immutable y; 0 .. side)
foreach (immutable x; 1 .. side)
if (board[x][y].val)
moveHorizontally(x, y, -1);
break;
case right:
foreach (immutable y; 0 .. side)
foreach_reverse (immutable x; 0 .. 3)
if (board[x][y].val)
moveHorizontally(x, y, 1);
Line 5,868 ⟶ 5,919:
}</syntaxhighlight>
The output is the same as the C++ version.
 
=={{header|Delphi}}==
{{libheader| System.SysUtils}}
Line 8,083 ⟶ 8,135:
Loop</syntaxhighlight>
 
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
begin enum 123
_lf
_rt
_dn
_up
_new
_end
end enum
str63 bd
colorref color(11)
byte zs
 
void local fn initialize
subclass window 1, @"2048",(0,0,438,438)
fn WindowSetBackgroundColor( 1, fn ColorBlack )
color(0) = fn ColorDarkGray
color(1) = fn ColorGray
color(2) = fn ColorLightGray
color(3) = fn ColorBlue
color(4) = fn ColorBrown
color(5) = fn ColorCyan
color(6) = fn ColorGreen
color(7) = fn ColorMagenta
color(8) = fn ColorOrange
color(9) = fn ColorPurple
color(10) = fn ColorYellow
color(11) = fn ColorRed
end fn
 
void local fn drawBoard
int x, y,r = 1, add
cls
for y = 320 to 20 step -100
for x = 20 to 320 step 100
rect fill (x,y,98,98),color( bd[r] )
select bd[r]
case < 4 : add = 40
case < 7 : add = 30
case < 10 : add = 20
case else : add = 6
end select
if bd[r] then print %(x+add, y+25)2^bd[r]
r++
next
next
end fn
 
local fn finish( won as bool )
CFStringRef s = @"GAME OVER"
CGRect r = fn windowContentRect( 1 )
r.origin.y += r.size.height - 20
r.size.height = 100
window 2,,r,NSwindowStyleMaskBorderless
if won
fn windowSetBackgroundColor( 2, color(11) )
s = @"CONGRATULATIONS—YOU DID IT!!"
text,24,fn ColorBlack,,NSTextAlignmentCenter
else
fn windowSetBackgroundColor( 2, fn ColorBlack )
text,24,fn ColorWhite,,NSTextAlignmentCenter
end if
print s
button _new,,,@"New Game", (229,20,100,32)
button _end,,,@"Quit", (109,20,100,32)
end fn
 
void local fn newGame
int r
text @"Arial bold", 36, fn ColorBlack, fn ColorClear
bd = chr$(0)
for r = 1 to 4
bd += bd
next
bd[rnd(16)] ++
do
r = rnd(16)
until bd[r] == 0
bd[r]++
zs = 14
fn drawBoard
end fn
 
local fn play( st as short, rd as short, cd as short )
short a, b, c, t, moved = 0
for a = st to st + rd * 3 step rd
// SHIFT
t = a
for b = a to a + cd * 3 step cd
if bd[b]
if t <> b then swap bd[t], bd[b] : moved ++
t += cd
end if
next
// MERGE
for b = a to a + cd * 2 step cd
if bd[b] > 0 && bd[b] == bd[b+cd]
bd[b]++ : c = b + cd
// FILL IN
while c <> a+cd*3
bd[c] = bd[c+cd] : c += cd
wend
bd[c] = 0
// CHECK FOR WIN
if bd[b] == 11 then fn drawBoard : fn finish( yes ) : exit fn
zs ++ : moved ++
end if
next
next
fn drawBoard
if moved == 0 then exit fn
// GROW
b = 0 : c = rnd(zs)
while c
b ++
if bd[b] == 0 then c--
wend
if rnd(10) - 1 then bd[b]++ else bd[b] = 2
zs--
timerbegin 0.25
fn drawBoard
timerend
if zs then exit fn
// IS GAME OVER?
for a = 1 to 12
if bd[a] == bd[a+4] then exit fn
next
for a = 1 to 13 step 4
if bd[a] == bd[a+1] || bd[a+1] == bd[a+2] || bd[a+2] == bd[a+3]¬
then exit fn
next
fn finish( no )
end fn
 
local fn doDialog(ev as long,tag as long, wnd as long)
select ev
case _windowKeyDown : if window() == 2 then exit fn
select fn EventKeyCode
case _up : fn play(13, 1, -4)
case _dn : fn play( 1, 1, 4)
case _lf : fn play( 1, 4, 1)
case _rt : fn play( 4, 4, -1)
case else : exit fn
end select
DialogEventSetBool(yes)
case _btnClick : window close 2
if tag == _end then end
fn newGame
case _windowWillClose : if wnd == 1 then end
end select
end fn
 
fn initialize
fn newGame
on dialog fn doDialog
 
handleevents
</syntaxhighlight>
[[File:FutureBasic 2048.png]]
 
=={{header|Go}}==
Line 9,281 ⟶ 9,499:
const boardsize = 4
app2048(boardsize)
</syntaxhighlight>
 
=={{header|Koka}}==
Based on F#
<syntaxhighlight lang="koka">
import std/num/random
import std/os/readline
 
val empty = list(0, 15).map(fn(_) 0)
fun win(l)
l.any(fn(x) x == 2048)
 
fun stack(l)
match l
Cons(0, tl) -> tl.stack ++ [0]
Cons(hd, tl) -> Cons(hd, tl.stack)
Nil -> Nil
 
fun join(l: list<int>)
match l
Cons(a, Cons(b, c)) | a == b -> Cons((a + b), c.join) ++ [0]
Cons(a, b) -> Cons(a, b.join)
Nil -> Nil
 
fun hit(l)
l.stack.join
 
fun hitBack(l)
l.reverse.hit.reverse
 
fun splitBy(l: list<a>, i: int): div list<list<a>>
val (a, b) = l.split(i)
match b
Cons -> Cons(a, b.splitBy(i))
Nil -> Cons(a, Nil)
 
fun transpose(l: list<list<a>>): <exn> list<list<a>>
match l
Cons(Cons(a, b), c) -> Cons(Cons(a, c.map(fn(x) x.head.unjust)), transpose(Cons(b, c.map(fn(x) x.tail)).unsafe-decreasing))
Cons(Nil, b) -> transpose(b)
Nil -> Nil
 
fun rows(l)
l.splitBy(4)
 
fun left(l)
l.rows.map(hit).concat
 
fun right(l)
l.rows.map(hitBack).concat
 
fun up(l)
l.rows.transpose.map(hit).transpose.concat
 
fun down(l)
l.rows.transpose.map(hitBack).transpose.concat
 
fun (==)(l1: list<int>, l2: list<int>): bool
match l1
Cons(a, b) -> match l2
Cons(c, d) -> a == c && b == d
Nil -> False
Nil -> match l2
Cons -> False
Nil -> True
 
fun lose(l)
l.left == l && l.right == l && l.up == l && l.down == l
 
fun numZeros(l: list<int>): int
l.filter(fn(x) x == 0).length
 
fun insert(l: list<int>, what: int, toWhere: int)
match l
Cons(0, tail) | tail.numZeros == toWhere -> Cons(what, tail)
Cons(head, tail) -> Cons(head, tail.insert(what, toWhere))
Nil -> Nil
 
fun spawnOn(l)
val newTileValue = if random-int() % 10 == 0 then 4 else 2
val newPosition = random-int() % (l.numZeros - 1)
l.insert(newTileValue, newPosition)
 
fun show-board(l: list<int>): div string
"\n" ++ l.rows.map(fn(r) r.map(fn(x) x.show.pad-left(4)).join("")).intersperse("\n").join("") ++ "\n"
 
fun quit(l)
[]
 
fun quitted(l)
l.is-nil
 
fun dispatch(c)
match c
'i' -> up
'j' -> left
'k' -> down
'l' -> right
'q' -> quit
_ ->
println("Unknown command: keys are ijkl to move, q to quit")
id
 
fun key()
readline().head-char.default('_')
 
fun turn(l)
l.show-board.println
val next = dispatch(key())(l)
if !(next == l) && next.quitted.not then
spawnOn(next)
else
next
 
fun play(state)
if state.win || state.lose || state.quitted then
if state.quitted then
"You quit!".println
else if state.win then
"You won!".println
else
"You lost!".println
else
"play".println
play(turn(state))
 
fun main()
print("ijkl to move, q to quit\n")
val initial = empty.spawnOn
println("Starting game...")
play(initial)
 
</syntaxhighlight>
 
Line 16,352 ⟶ 16,702:
{{libheader|Wren-fmt}}
{{libheader|Wren-str}}
<syntaxhighlight lang="ecmascriptwren">import "./dynamic" for Enum, Struct
import "random" for Random
import "./ioutil" for Input
import "./fmt" for Fmt
import "./str" for Str
 
var MoveDirection = Enum.create("MoveDirection", ["up", "down", "left", "right"])
408

edits