2048: Difference between revisions

Content added Content deleted
m (syntax highlighting fixup automation)
m (Automated syntax highlighting fixup (second round - minor fixes))
Line 55: Line 55:
=={{header|AArch64 Assembly}}==
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
<syntaxhighlight lang=AArch64 Assembly>
<syntaxhighlight lang="aarch64 assembly">
/* ARM assembly AARCH64 Raspberry PI 3B */
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program 2048_64.s */
/* program 2048_64.s */
Line 1,109: Line 1,109:
=={{header|Ada}}==
=={{header|Ada}}==
{{works with|GNAT}}
{{works with|GNAT}}
<syntaxhighlight lang=Ada>with Ada.Text_IO; use Ada.Text_IO;
<syntaxhighlight lang="ada">with Ada.Text_IO; use Ada.Text_IO;
with System.Random_Numbers;
with System.Random_Numbers;
procedure Play_2048 is
procedure Play_2048 is
Line 1,300: Line 1,300:


=={{header|ALGOL 68}}==
=={{header|ALGOL 68}}==
<syntaxhighlight lang=algol68>
<syntaxhighlight lang="algol68">
main:(
main:(
INT side = 4;
INT side = 4;
Line 1,448: Line 1,448:
VERSION 1: "Hopper" flavour.
VERSION 1: "Hopper" flavour.
</pre>
</pre>
<syntaxhighlight lang=Amazing Hopper>
<syntaxhighlight lang="amazing hopper">
#context-free select Position of aleatory tail
#context-free select Position of aleatory tail
#context-free show Table
#context-free show Table
Line 1,740: Line 1,740:
VERSION 2: "Hopper-BASIC" flavour.
VERSION 2: "Hopper-BASIC" flavour.
</pre>
</pre>
<syntaxhighlight lang=Amazing Hopper>
<syntaxhighlight lang="amazing hopper">


// Definicion de "contextos"
// Definicion de "contextos"
Line 2,032: Line 2,032:


=={{header|Applesoft BASIC}}==
=={{header|Applesoft BASIC}}==
<syntaxhighlight lang=Applesoft>PRINT "Game 2048"
<syntaxhighlight lang="applesoft">PRINT "Game 2048"


10 REM ************
10 REM ************
Line 2,242: Line 2,242:
=={{header|ARM Assembly}}==
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
{{works with|as|Raspberry Pi}}
<syntaxhighlight lang=ARM Assembly>
<syntaxhighlight lang="arm assembly">
/* ARM assembly Raspberry PI */
/* ARM assembly Raspberry PI */
/* program 2048.s */
/* program 2048.s */
Line 3,157: Line 3,157:


=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==
<syntaxhighlight lang=AutoHotkey>Grid := [], s := 16, w := h := S * 4.5
<syntaxhighlight lang="autohotkey">Grid := [], s := 16, w := h := S * 4.5
Gui, font, s%s%
Gui, font, s%s%
Gui, add, text, y1
Gui, add, text, y1
Line 3,383: Line 3,383:


=={{header|Batch File}}==
=={{header|Batch File}}==
<syntaxhighlight lang=dos>:: 2048 Game Task from RosettaCode
<syntaxhighlight lang="dos">:: 2048 Game Task from RosettaCode
:: Batch File Implementation v2.0.1
:: Batch File Implementation v2.0.1


Line 3,578: Line 3,578:


Yo solo lo transcribo.
Yo solo lo transcribo.
<syntaxhighlight lang=qbasic>
<syntaxhighlight lang="qbasic">
SCREEN 13
SCREEN 13
PALETTE 1, pColor(35, 33, 31)
PALETTE 1, pColor(35, 33, 31)
Line 3,938: Line 3,938:
=={{header|BBC BASIC}}==
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
{{works with|BBC BASIC for Windows}}
<syntaxhighlight lang=bbcbasic> SIZE = 4 : MAX = SIZE-1
<syntaxhighlight lang="bbcbasic"> SIZE = 4 : MAX = SIZE-1
Won% = FALSE : Lost% = FALSE
Won% = FALSE : Lost% = FALSE
@% = 5
@% = 5
Line 4,043: Line 4,043:
===Version 1===
===Version 1===
Supports limited colours through vt100 escape codes. Requires a posix machine for <tt>termios.h</tt> and <tt>unistd.h</tt> headers. Provides simplistic animations when moving and merging blocks.
Supports limited colours through vt100 escape codes. Requires a posix machine for <tt>termios.h</tt> and <tt>unistd.h</tt> headers. Provides simplistic animations when moving and merging blocks.
<syntaxhighlight lang=c>
<syntaxhighlight lang="c">
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
Line 4,340: Line 4,340:




<syntaxhighlight lang=c>
<syntaxhighlight lang="c">


#include <stdio.h>
#include <stdio.h>
Line 4,686: Line 4,686:
=={{header|C sharp|C#}}==
=={{header|C sharp|C#}}==
{{trans|C++}}
{{trans|C++}}
<syntaxhighlight lang=csharp>using System;
<syntaxhighlight lang="csharp">using System;


namespace g2048_csharp
namespace g2048_csharp
Line 5,100: Line 5,100:


=={{header|C++}}==
=={{header|C++}}==
<syntaxhighlight lang=cpp>
<syntaxhighlight lang="cpp">
#include <time.h>
#include <time.h>
#include <iostream>
#include <iostream>
Line 5,313: Line 5,313:


=={{header|Clojure}}==
=={{header|Clojure}}==
<syntaxhighlight lang=clojure>
<syntaxhighlight lang="clojure">
(ns 2048
(ns 2048
(:require [clojure.string :as str]))
(:require [clojure.string :as str]))
Line 5,447: Line 5,447:
=={{header|Common Lisp}}==
=={{header|Common Lisp}}==
Depends on Windows msvcrt.dll for _getch. Depends on quicklisp. Use arrow keys to make moves and press "Q" to quit. Tested with SBCL.
Depends on Windows msvcrt.dll for _getch. Depends on quicklisp. Use arrow keys to make moves and press "Q" to quit. Tested with SBCL.
<syntaxhighlight lang=lisp>(ql:quickload '(cffi alexandria))
<syntaxhighlight lang="lisp">(ql:quickload '(cffi alexandria))


(defpackage :2048-lisp
(defpackage :2048-lisp
Line 5,675: Line 5,675:
=={{header|D}}==
=={{header|D}}==
{{trans|C++}}
{{trans|C++}}
<syntaxhighlight lang=d>import std.stdio, std.string, std.random;
<syntaxhighlight lang="d">import std.stdio, std.string, std.random;
import core.stdc.stdlib: exit;
import core.stdc.stdlib: exit;


Line 5,872: Line 5,872:
{{libheader| Velthuis.Console}}Thanks for Rudy Velthuis [https://github.com/rvelthuis/Consoles].
{{libheader| Velthuis.Console}}Thanks for Rudy Velthuis [https://github.com/rvelthuis/Consoles].
{{Trans|C++}}
{{Trans|C++}}
<syntaxhighlight lang=Delphi>
<syntaxhighlight lang="delphi">
program Game2048;
program Game2048;


Line 6,298: Line 6,298:
=={{header|Elixir}}==
=={{header|Elixir}}==
{{works with|Elixir|1.3}}
{{works with|Elixir|1.3}}
<syntaxhighlight lang=elixir>defmodule Game2048 do
<syntaxhighlight lang="elixir">defmodule Game2048 do
@size 4
@size 4
@range 0..@size-1
@range 0..@size-1
Line 6,444: Line 6,444:
{{works with|Elm 0.18.0}}
{{works with|Elm 0.18.0}}
Try online [https://ellie-app.com/3ZMMpYsbfcMa1/3]
Try online [https://ellie-app.com/3ZMMpYsbfcMa1/3]
<syntaxhighlight lang=Elm>module Main exposing (..)
<syntaxhighlight lang="elm">module Main exposing (..)


import Html exposing (Html, div, p, text, button, span, h2)
import Html exposing (Html, div, p, text, button, span, h2)
Line 6,852: Line 6,852:
The following code can be executed as is using F# Interactive from system command line ("fsi.exe 2048.fsx") but not from Visual Studio F# Interactive window due to the way it access keyboard (the System.Console.ReadKey() function).
The following code can be executed as is using F# Interactive from system command line ("fsi.exe 2048.fsx") but not from Visual Studio F# Interactive window due to the way it access keyboard (the System.Console.ReadKey() function).


<syntaxhighlight lang=fsharp>
<syntaxhighlight lang="fsharp">
// the board is represented with a list of 16 integers
// the board is represented with a list of 16 integers
let empty = List.init 16 (fun _ -> 0)
let empty = List.init 16 (fun _ -> 0)
Line 6,927: Line 6,927:




<syntaxhighlight lang=Factor>
<syntaxhighlight lang="factor">
USE: accessors
USE: accessors
FROM: arrays => <array> array ;
FROM: arrays => <array> array ;
Line 7,256: Line 7,256:
Just like my implementation of [[15 Puzzle Game#Forth|15 Puzzle Game]], this uses Vim's h/j/k/l for movement.
Just like my implementation of [[15 Puzzle Game#Forth|15 Puzzle Game]], this uses Vim's h/j/k/l for movement.


<syntaxhighlight lang=forth>\ in Forth, you do many things on your own. This word is used to define 2D arrays
<syntaxhighlight lang="forth">\ in Forth, you do many things on your own. This word is used to define 2D arrays
: 2D-ARRAY ( height width )
: 2D-ARRAY ( height width )
CREATE DUP ,
CREATE DUP ,
Line 7,517: Line 7,517:


===Source===
===Source===
The initial attempt at showing the board relied rather heavily on FORMAT tricks, in particular the use of the <''n''> facility whereby the value of an integer expression can be inserted into a format statement's coding on-the-fly, as in the following. <syntaxhighlight lang=Fortran>
The initial attempt at showing the board relied rather heavily on FORMAT tricks, in particular the use of the <''n''> facility whereby the value of an integer expression can be inserted into a format statement's coding on-the-fly, as in the following. <syntaxhighlight lang="fortran">
WRITE (MSG,1) !Roll forth a top/bottom boundary. No corner characters (etc.), damnit.
WRITE (MSG,1) !Roll forth a top/bottom boundary. No corner characters (etc.), damnit.
1 FORMAT ("|",<NC>(<W>("-"),"|")) !Heavy reliance on runtime values in NC and W. But see FORMAT 22.
1 FORMAT ("|",<NC>(<W>("-"),"|")) !Heavy reliance on runtime values in NC and W. But see FORMAT 22.
Line 7,525: Line 7,525:
4 FORMAT ("|",<NC - 1>(<W>("-"),"+"),<W>("-"),"|") !With internal + rather than |.</syntaxhighlight>
4 FORMAT ("|",<NC - 1>(<W>("-"),"+"),<W>("-"),"|") !With internal + rather than |.</syntaxhighlight>
This sort of thing is not necessarily accepted by all compilers, so instead the next stage was to convert to using complicated WRITE statements. If one regarded the various sizes (the values of NR, NC, W in the source) as truly fixed, literal constants could be used throughout. This would however mean that they would appear without explanation, and if one eventually attempted to recode with different values, mistakes would be likely. Thus below, FORMAT 3 has <code> (<NC>(A1,I<W>),A1)</code> and if the <> scheme were unavailable, you'd have to use <code>(4(A1,I6),A1)</code> instead, not too troublesome a change. Or, the text of the format sequence could be written to a CHARACTER variable, as demonstrated in [[Multiplication_tables#Traditional_approach]]. Yet another approach might be <code>(666(A1,I6))</code> which relies on the addendum <code>A1</code> happening to be the same as the start of the <code>(A1,I6)</code> pair, but there is still the appearance of the literal constant six instead of <W>, and if there were to be any change to be made, it would have to be remembered...
This sort of thing is not necessarily accepted by all compilers, so instead the next stage was to convert to using complicated WRITE statements. If one regarded the various sizes (the values of NR, NC, W in the source) as truly fixed, literal constants could be used throughout. This would however mean that they would appear without explanation, and if one eventually attempted to recode with different values, mistakes would be likely. Thus below, FORMAT 3 has <code> (<NC>(A1,I<W>),A1)</code> and if the <> scheme were unavailable, you'd have to use <code>(4(A1,I6),A1)</code> instead, not too troublesome a change. Or, the text of the format sequence could be written to a CHARACTER variable, as demonstrated in [[Multiplication_tables#Traditional_approach]]. Yet another approach might be <code>(666(A1,I6))</code> which relies on the addendum <code>A1</code> happening to be the same as the start of the <code>(A1,I6)</code> pair, but there is still the appearance of the literal constant six instead of <W>, and if there were to be any change to be made, it would have to be remembered...
<syntaxhighlight lang=Fortran> SUBROUTINE SHOW(NR,NC,BOARD) !Mess about.
<syntaxhighlight lang="fortran"> SUBROUTINE SHOW(NR,NC,BOARD) !Mess about.
INTEGER NR,NC !Number of rows and columns.
INTEGER NR,NC !Number of rows and columns.
INTEGER BOARD(NR,NC) !The board. Actual storage is transposed!
INTEGER BOARD(NR,NC) !The board. Actual storage is transposed!
Line 7,771: Line 7,771:
=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
Based On MichD's original code (https://github.com/michd/2048-qbasic)
Based On MichD's original code (https://github.com/michd/2048-qbasic)
<syntaxhighlight lang=freebasic>#define EXTCHAR Chr(255)
<syntaxhighlight lang="freebasic">#define EXTCHAR Chr(255)


'--- Declaration of global variables ---
'--- Declaration of global variables ---
Line 8,086: Line 8,086:
=={{header|Go}}==
=={{header|Go}}==


<syntaxhighlight lang=Go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 8,358: Line 8,358:


=={{header|Haskell}}==
=={{header|Haskell}}==
<syntaxhighlight lang=haskell>import System.IO
<syntaxhighlight lang="haskell">import System.IO
import Data.List
import Data.List
import Data.Maybe
import Data.Maybe
Line 8,494: Line 8,494:
=={{header|J}}==
=={{header|J}}==
'''Solution'''
'''Solution'''
<syntaxhighlight lang=j>NB. 2048.ijs script
<syntaxhighlight lang="j">NB. 2048.ijs script
NB. =========================================================
NB. =========================================================
NB. 2048 game engine
NB. 2048 game engine
Line 8,601: Line 8,601:
)</syntaxhighlight>
)</syntaxhighlight>
'''Usage'''
'''Usage'''
<syntaxhighlight lang=j> grd=: g2048Con ''
<syntaxhighlight lang="j"> grd=: g2048Con ''


Score is 0
Score is 0
Line 8,625: Line 8,625:
[[File:game_2048_java2.png|300px|thumb|right]]
[[File:game_2048_java2.png|300px|thumb|right]]
{{works with|Java|8}}
{{works with|Java|8}}
<syntaxhighlight lang=java>import java.awt.*;
<syntaxhighlight lang="java">import java.awt.*;
import java.awt.event.*;
import java.awt.event.*;
import java.util.Random;
import java.util.Random;
Line 8,926: Line 8,926:
=={{header|JavaScript}}==
=={{header|JavaScript}}==
Uses the P5.js library.
Uses the P5.js library.
<syntaxhighlight lang=JavaScript>
<syntaxhighlight lang="javascript">
/* Tile object: */
/* Tile object: */


Line 9,110: Line 9,110:
=={{header|Julia}}==
=={{header|Julia}}==
Uses the Gtk toolkit. Includes scoring, a choice of board size and toolbar buttons for Undo and New Game.
Uses the Gtk toolkit. Includes scoring, a choice of board size and toolbar buttons for Undo and New Game.
<syntaxhighlight lang=julia>using Gtk.ShortNames
<syntaxhighlight lang="julia">using Gtk.ShortNames


@enum Direction2048 Right Left Up Down
@enum Direction2048 Right Left Up Down
Line 9,286: Line 9,286:
Stateless with focus on clarity rather than conciseness.
Stateless with focus on clarity rather than conciseness.


<syntaxhighlight lang=scala>import java.io.BufferedReader
<syntaxhighlight lang="scala">import java.io.BufferedReader
import java.io.InputStreamReader
import java.io.InputStreamReader


Line 9,476: Line 9,476:
Takes input on stdin using the words "left", "right", "up", "down".
Takes input on stdin using the words "left", "right", "up", "down".


<syntaxhighlight lang=Latitude>
<syntaxhighlight lang="latitude">
use 'format import '[format].
use 'format import '[format].
use 'random.
use 'random.
Line 9,719: Line 9,719:
=={{header|Lua}}==
=={{header|Lua}}==
Sadly, ANSI C doesn't have low-level keyboard input, so neither does vanilla Lua, so the input is a bit cumbersome (wasd PLUS enter).
Sadly, ANSI C doesn't have low-level keyboard input, so neither does vanilla Lua, so the input is a bit cumbersome (wasd PLUS enter).
<syntaxhighlight lang=lua>-- 2048 for Lua 5.1-5.4, 12/3/2020 db
<syntaxhighlight lang="lua">-- 2048 for Lua 5.1-5.4, 12/3/2020 db
local unpack = unpack or table.unpack -- for 5.3 +/- compatibility
local unpack = unpack or table.unpack -- for 5.3 +/- compatibility
game = {
game = {
Line 9,808: Line 9,808:


=={{header|M2000 Interpreter}}==
=={{header|M2000 Interpreter}}==
<syntaxhighlight lang=M2000 Interpreter>
<syntaxhighlight lang="m2000 interpreter">
Module Game2048 {
Module Game2048 {
\\ 10% 4 and 90% 2
\\ 10% 4 and 90% 2
Line 9,964: Line 9,964:


Next is the main body of code:
Next is the main body of code:
<syntaxhighlight lang=Maple>
<syntaxhighlight lang="maple">
macro(SP=DocumentTools:-SetProperty, GP=DocumentTools:-GetProperty);
macro(SP=DocumentTools:-SetProperty, GP=DocumentTools:-GetProperty);
G := module()
G := module()
Line 10,188: Line 10,188:


=={{header|Mathematica}}/{{header|Wolfram Language}}==
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang=Mathematica>SetOptions[InputNotebook[],NotebookEventActions->{
<syntaxhighlight lang="mathematica">SetOptions[InputNotebook[],NotebookEventActions->{
"LeftArrowKeyDown":>(stat=Coalesce[stat];AddNew[]),
"LeftArrowKeyDown":>(stat=Coalesce[stat];AddNew[]),
"RightArrowKeyDown":>(stat=Reverse/@Coalesce[Reverse/@stat];AddNew[]),
"RightArrowKeyDown":>(stat=Reverse/@Coalesce[Reverse/@stat];AddNew[]),
Line 10,226: Line 10,226:
=={{header|MATLAB}}==
=={{header|MATLAB}}==


<syntaxhighlight lang=MATLAB>function field = puzzle2048(field)
<syntaxhighlight lang="matlab">function field = puzzle2048(field)


if nargin < 1 || isempty(field)
if nargin < 1 || isempty(field)
Line 10,360: Line 10,360:


You can start with an empty 4 x 4 board and save the last state of the playing field with:
You can start with an empty 4 x 4 board and save the last state of the playing field with:
<syntaxhighlight lang=MATLAB>field = puzzle2048();</syntaxhighlight>
<syntaxhighlight lang="matlab">field = puzzle2048();</syntaxhighlight>


Or you start from a saved playing field:
Or you start from a saved playing field:
<syntaxhighlight lang=MATLAB>field = puzzle2048(savedField);</syntaxhighlight>
<syntaxhighlight lang="matlab">field = puzzle2048(savedField);</syntaxhighlight>


=={{header|Nim}}==
=={{header|Nim}}==
Line 10,369: Line 10,369:
{{works with|Nim Compiler|0.19.4}}
{{works with|Nim Compiler|0.19.4}}


<syntaxhighlight lang=nim>import random, strutils, terminal
<syntaxhighlight lang="nim">import random, strutils, terminal


const
const
Line 10,557: Line 10,557:
=={{header|OCaml}}==
=={{header|OCaml}}==


<syntaxhighlight lang=ocaml>
<syntaxhighlight lang="ocaml">
let list_make x v =
let list_make x v =
let rec aux acc i =
let rec aux acc i =
Line 10,746: Line 10,746:


=={{header|Pascal}}==
=={{header|Pascal}}==
<syntaxhighlight lang=Pascal>
<syntaxhighlight lang="pascal">
program game2048;
program game2048;
uses Crt;
uses Crt;
Line 11,112: Line 11,112:


=={{header|Perl}}==
=={{header|Perl}}==
<syntaxhighlight lang=perl>#!/usr/bin/perl
<syntaxhighlight lang="perl">#!/usr/bin/perl


use strict; # https://rosettacode.org/wiki/2048
use strict; # https://rosettacode.org/wiki/2048
Line 11,206: Line 11,206:
Now I just got figure out how to win...
Now I just got figure out how to win...
You can run this online [http://phix.x10.mx/p2js/2048.htm here].
You can run this online [http://phix.x10.mx/p2js/2048.htm here].
<!--<syntaxhighlight lang=Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">-- demo\rosetta\2048.exw</span>
<span style="color: #000080;font-style:italic;">-- demo\rosetta\2048.exw</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
Line 11,477: Line 11,477:
=={{header|PHP}}==
=={{header|PHP}}==
Works from PHP5 and upwards in CLI mode.
Works from PHP5 and upwards in CLI mode.
<syntaxhighlight lang=PHP>
<syntaxhighlight lang="php">
<?php
<?php


Line 11,708: Line 11,708:


=={{header|PicoLisp}}==
=={{header|PicoLisp}}==
<syntaxhighlight lang=PicoLisp>(load "@lib/simul.l")
<syntaxhighlight lang="picolisp">(load "@lib/simul.l")


(symbols 'simul 'pico)
(symbols 'simul 'pico)
Line 11,846: Line 11,846:
=={{header|Pony}}==
=={{header|Pony}}==
{{works with|ponyc|0.10.0}}
{{works with|ponyc|0.10.0}}
<syntaxhighlight lang=pony>
<syntaxhighlight lang="pony">
use "term"
use "term"
use "random"
use "random"
Line 12,117: Line 12,117:
=={{header|Prolog}}==
=={{header|Prolog}}==
Works with swi-prolog, any version.
Works with swi-prolog, any version.
<syntaxhighlight lang=Prolog>/* -------------------------------------------------------------
<syntaxhighlight lang="prolog">/* -------------------------------------------------------------
Entry point, just create a blank grid and enter a 'game loop'
Entry point, just create a blank grid and enter a 'game loop'
-------------------------------------------------------------*/
-------------------------------------------------------------*/
Line 12,295: Line 12,295:
=={{header|Python}}==
=={{header|Python}}==
===Python: Original, with output===
===Python: Original, with output===
<syntaxhighlight lang=python>#!/usr/bin/env python3
<syntaxhighlight lang="python">#!/usr/bin/env python3


import curses
import curses
Line 12,499: Line 12,499:


===Python: using tkinter===
===Python: using tkinter===
<syntaxhighlight lang=python>#!/usr/bin/env python3
<syntaxhighlight lang="python">#!/usr/bin/env python3


''' Python 3.6.5 code using Tkinter graphical user interface.
''' Python 3.6.5 code using Tkinter graphical user interface.
Line 12,796: Line 12,796:


=={{header|QB64}}==
=={{header|QB64}}==
<syntaxhighlight lang=QB64>
<syntaxhighlight lang="qb64">
_DEFINE A-Z AS _INTEGER64
_DEFINE A-Z AS _INTEGER64
DIM SHARED Grid(0 TO 5, 0 TO 5) AS INTEGER
DIM SHARED Grid(0 TO 5, 0 TO 5) AS INTEGER
Line 13,088: Line 13,088:
=={{header|R}}==
=={{header|R}}==
orginal R package : https://github.com/ThinkRstat/r2048
orginal R package : https://github.com/ThinkRstat/r2048
<syntaxhighlight lang=R>
<syntaxhighlight lang="r">
GD <- function(vec) {
GD <- function(vec) {
c(vec[vec != 0], vec[vec == 0])
c(vec[vec != 0], vec[vec == 0])
Line 13,258: Line 13,258:
Play the RacketScript fork online here: http://rapture.twistedplane.com:8080/#example/2048-game
Play the RacketScript fork online here: http://rapture.twistedplane.com:8080/#example/2048-game


<syntaxhighlight lang=Racket>
<syntaxhighlight lang="racket">
;; LICENSE: See License file LICENSE (MIT license)
;; LICENSE: See License file LICENSE (MIT license)
;;
;;
Line 14,069: Line 14,069:
Uses termios to set the terminal options, so only compatible with POSIX terminals. This version does not include a specific "win" or "lose" condition. (though it would be trivial to add them.) You can continue to play this even after getting a 2048 tile; and if there is no valid move you can make, you can't do anything but quit.
Uses termios to set the terminal options, so only compatible with POSIX terminals. This version does not include a specific "win" or "lose" condition. (though it would be trivial to add them.) You can continue to play this even after getting a 2048 tile; and if there is no valid move you can make, you can't do anything but quit.
{{works with|Rakudo|2018.05}}
{{works with|Rakudo|2018.05}}
<syntaxhighlight lang=perl6>use Term::termios;
<syntaxhighlight lang="raku" line>use Term::termios;


constant $saved = Term::termios.new(fd => 1).getattr;
constant $saved = Term::termios.new(fd => 1).getattr;
Line 14,208: Line 14,208:
{{works with|Red|0.6.4}}
{{works with|Red|0.6.4}}


<syntaxhighlight lang=Red>Red [Needs: 'View]
<syntaxhighlight lang="red">Red [Needs: 'View]


random/seed now
random/seed now
Line 14,315: Line 14,315:
::* &nbsp; displays a message if a winning move was entered.
::* &nbsp; displays a message if a winning move was entered.
::* &nbsp; displays the game board as a grid &nbsp; (with boxes).
::* &nbsp; displays the game board as a grid &nbsp; (with boxes).
<syntaxhighlight lang=rexx>/*REXX program lets a user play the 2048 game on an NxN grid (default is 4x4 grid).*/
<syntaxhighlight lang="rexx">/*REXX program lets a user play the 2048 game on an NxN grid (default is 4x4 grid).*/
parse arg N win seed . /*obtain optional arguments from the CL*/
parse arg N win seed . /*obtain optional arguments from the CL*/
if N=='' | N=="," then N= 4 /*Not specified? Then use the default.*/
if N=='' | N=="," then N= 4 /*Not specified? Then use the default.*/
Line 14,573: Line 14,573:


=={{header|Ring}}==
=={{header|Ring}}==
<syntaxhighlight lang=ring>
<syntaxhighlight lang="ring">
# Project : 2048 Game
# Project : 2048 Game


Line 15,014: Line 15,014:
=={{header|Ruby}}==
=={{header|Ruby}}==
inspired by the Raku version
inspired by the Raku version
<syntaxhighlight lang=ruby>
<syntaxhighlight lang="ruby">
#!/usr/bin/ruby
#!/usr/bin/ruby


Line 15,219: Line 15,219:
A simple implementation in rust. The user has to input an endline since i did not find a way to read a key press
A simple implementation in rust. The user has to input an endline since i did not find a way to read a key press
{{libheader|rand}}
{{libheader|rand}}
<syntaxhighlight lang=rust>
<syntaxhighlight lang="rust">
use std::io::{self,BufRead};
use std::io::{self,BufRead};
extern crate rand;
extern crate rand;
Line 15,407: Line 15,407:


=={{header|Scala}}==
=={{header|Scala}}==
<syntaxhighlight lang=scala>import java.awt.event.{KeyAdapter, KeyEvent, MouseAdapter, MouseEvent}
<syntaxhighlight lang="scala">import java.awt.event.{KeyAdapter, KeyEvent, MouseAdapter, MouseEvent}
import java.awt.{BorderLayout, Color, Dimension, Font, Graphics2D, Graphics, RenderingHints}
import java.awt.{BorderLayout, Color, Dimension, Font, Graphics2D, Graphics, RenderingHints}
import java.util.Random
import java.util.Random
Line 15,611: Line 15,611:
STD_CONSOLE works also always the same, independent from the operating system or terminal/console.
STD_CONSOLE works also always the same, independent from the operating system or terminal/console.


<syntaxhighlight lang=seed7>$ include "seed7_05.s7i";
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i";
include "console.s7i";
include "console.s7i";
include "keybd.s7i";
include "keybd.s7i";
Line 15,804: Line 15,804:
=={{header|Tcl}}==
=={{header|Tcl}}==
===Text mode===
===Text mode===
<syntaxhighlight lang=tcl>
<syntaxhighlight lang="tcl">
# A minimal implementation of the game 2048 in Tcl.
# A minimal implementation of the game 2048 in Tcl.
# For a maintained version with expanded functionality see
# For a maintained version with expanded functionality see
Line 16,088: Line 16,088:
=={{header|Visual Basic .NET}}==
=={{header|Visual Basic .NET}}==
{{trans|C#}}
{{trans|C#}}
<syntaxhighlight lang=vbnet>Friend Class Tile
<syntaxhighlight lang="vbnet">Friend Class Tile
Public Sub New()
Public Sub New()
Me.Value = 0
Me.Value = 0
Line 16,352: Line 16,352:
{{libheader|Wren-fmt}}
{{libheader|Wren-fmt}}
{{libheader|Wren-str}}
{{libheader|Wren-str}}
<syntaxhighlight lang=ecmascript>import "/dynamic" for Enum, Struct
<syntaxhighlight lang="ecmascript">import "/dynamic" for Enum, Struct
import "random" for Random
import "random" for Random
import "/ioutil" for Input
import "/ioutil" for Input
Line 16,587: Line 16,587:


=={{header|XPL0}}==
=={{header|XPL0}}==
<syntaxhighlight lang=XPL0>include c:\cxpl\codes; \intrinsic 'code' declarations
<syntaxhighlight lang="xpl0">include c:\cxpl\codes; \intrinsic 'code' declarations
int Box(16), Moved;
int Box(16), Moved;