How to resolve the algorithm 2048 step by step in the Mathematica/Wolfram Language programming language
How to resolve the algorithm 2048 step by step in the Mathematica/Wolfram Language programming language
Table of Contents
Problem Statement
Implement a 2D sliding block puzzle game where blocks with numbers are combined to add their values.
The name comes from the popular open-source implementation of this game mechanic, 2048.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm 2048 step by step in the Mathematica/Wolfram Language programming language
Wolfram Language Program for Visualizing 2D Matrices
This program written in Wolfram Language allows you to create and visualize 2D matrices and manipulate them using arrow keys. Here's a detailed explanation of the code:
1. Event Actions for Notebook:
SetOptions[InputNotebook[], NotebookEventActions->{
"LeftArrowKeyDown":>(stat=Coalesce[stat];AddNew[]),
"RightArrowKeyDown":>(stat=Reverse/@Coalesce[Reverse/@stat];AddNew[]),
"UpArrowKeyDown":>(stat=Coalesce[stat\[Transpose]]\[Transpose];AddNew[]),
"DownArrowKeyDown":>(stat=(Reverse/@(Coalesce[Reverse/@(stat\[Transpose])]))\[Transpose];AddNew[])
}
];
This code sets event actions for the notebook. When the left arrow key is pressed, it calls the AddNew
function; the right arrow key calls AddNew
after reversing the matrix; the up arrow key transposes the matrix and calls AddNew
; and the down arrow key transposes the matrix, reverses it, and calls AddNew
.
2. Initialization:
n=4;
bgcolor=GrayLevel[0.84];
colorfunc=Blend[{{0,Gray},{1/2,Red},{1,Blend[{Yellow,Orange}]}},#]&;
n
is set to 4, which defines the size of the square matrix (n×n).bgcolor
is the background color of the matrix.colorfunc
is a function that assigns colors to matrix elements based on their values.
3. Functions:
a. AddNew
:
AddNew[]:=(stat=AddRandomNumber[stat])
Adds a random number (either 2 or 4) to a random empty position in the matrix stat
.
b. PrintStat
:
PrintStat[stat_]:=Module[{gr1,gr2,gr3,dr=0.2,cols,nstat=stat,positions},
This function is responsible for visualizing the matrix. It creates graphics objects gr1
(background rectangle), gr2
(colored rectangles representing matrix elements), and gr3
(text labels for non-zero elements).
c. Coalesce
and SubCoalesce
:
Coalesce[stat_]:=SubCoalesce/@stat
SubCoalesce[statlist_]:=...
These functions combine adjacent equal elements in the matrix. Coalesce
calls SubCoalesce
on each row of the matrix. SubCoalesce
iterates over the row and combines consecutive equal elements, updating the values in the row.
d. AddRandomNumber
:
AddRandomNumber[stat_,n_:2]:=With[{pos=Position[stat,0,{2}]},If[Length[pos]>0,ReplacePart[stat,RandomChoice[pos]->n],stat]]
This function adds a random number (either 2 or 4) to a random empty position in the matrix stat
. It takes an optional parameter n
to specify the number to add (default value is 2).
4. Main Execution:
stat=Nest[AddRandomNumber[#,RandomChoice[{2,4}]]&,ConstantArray[0,{n,n}],4];
Dynamic[PrintStat@stat]
stat
is initialized as a 4×4 matrix of zeros.Nest
appliesAddRandomNumber
to the matrix 4 times, adding random numbers to empty positions.Dynamic
displays an interactive visualization of the matrix that updates in real-time as the matrix changes.
Source code in the wolfram programming language
SetOptions[InputNotebook[],NotebookEventActions->{
"LeftArrowKeyDown":>(stat=Coalesce[stat];AddNew[]),
"RightArrowKeyDown":>(stat=Reverse/@Coalesce[Reverse/@stat];AddNew[]),
"UpArrowKeyDown":>(stat=Coalesce[stat\[Transpose]]\[Transpose];AddNew[]),
"DownArrowKeyDown":>(stat=(Reverse/@(Coalesce[Reverse/@(stat\[Transpose])]))\[Transpose];AddNew[])
}
];
n=4;
bgcolor=GrayLevel[0.84];
colorfunc=Blend[{{0,Gray},{1/2,Red},{1,Blend[{Yellow,Orange}]}},#]&;
ClearAll[AddNew,PrintStat,Coalesce,SubCoalesce,AddRandomNumber]
AddNew[]:=(stat=AddRandomNumber[stat])
PrintStat[stat_]:=Module[{gr1,gr2,gr3,dr=0.2,cols,nstat=stat,positions},
gr1={bgcolor,Rectangle[-dr{1,1},n+dr{1,1},RoundingRadius->dr]};
cols=Map[If[#==0,0,Log2[#]]&,nstat,{2}];
cols=Map[If[#==0,Lighter@bgcolor,colorfunc[#/Max[cols]]]&,cols,{2}];
positions=Table[{i,n-j+1},{j,n},{i,n}];
gr2=MapThread[{#2,Rectangle[#3-{1,1}(1-dr/3),#3-{1,1}dr/3,RoundingRadius->dr/2]}&,{stat,cols,positions},2];
gr3=MapThread[If[#1>0,Style[Text[#1,#2-0.5{1,1}],20,White],{}]&,{stat,positions},2];
Graphics[{gr1,gr2,gr3},PlotRange->{{-0.5,n+0.5},{-0.5,n+0.5}},ImageSize->500]
]
Coalesce[stat_]:=SubCoalesce/@stat
SubCoalesce[statlist_]:=Module[{st=statlist,n=Length[statlist],pairs},
st=Split[DeleteCases[st,0]];
st=Partition[#,2,2,1,{}]&/@st;
st=Map[If[Length[#]==2,Total[#],#]&,st,{2}];
Join[Flatten[st],ConstantArray[0,n-Length[Flatten[st]]]]
]
AddRandomNumber[stat_,n_:2]:=With[{pos=Position[stat,0,{2}]},If[Length[pos]>0,ReplacePart[stat,RandomChoice[pos]->n],stat]]
stat=Nest[AddRandomNumber[#,RandomChoice[{2,4}]]&,ConstantArray[0,{n,n}],4];
Dynamic[PrintStat@stat]
You may also check:How to resolve the algorithm String case step by step in the GAP programming language
You may also check:How to resolve the algorithm Narcissistic decimal number step by step in the J programming language
You may also check:How to resolve the algorithm Exceptions step by step in the Slate programming language
You may also check:How to resolve the algorithm Parsing/RPN calculator algorithm step by step in the C++ programming language
You may also check:How to resolve the algorithm Josephus problem step by step in the Forth programming language