# Specific ideas and implementation of 2048 game programming (Update 2)

Specific ideas and implementation of 2048 game programming

(updating...)

# 1, Game background

• Simple game:

• Try screenshot: # 2, Game flow chart

• The essence of the game is to move, merge and check a 4 * 4 two-dimensional matrix according to the values entered by the player's keyboard (or others).  # 3, Specific function realization

• Compiling environment: Dev C + + and EGE graphics library (search online and have a detailed installation process)

• Programming language: C++

• Rich graphics are only the mapping of data, and programming language is only the tool of ideological instantiation. The key lies in data processing

• Environmental preparation:

• Define data:

• 4 * 4 matrix
• Number of spaces
• direction
```#include <iostream>
using namespace std;

int grid;// 4 * 4 matrix
int EmptyBlock = 16;//Number of spaces
int dir;//direction

int main()
{

return 0;
}
```
• Debug function (initial value is assigned to grid for convenience)

1. Print function - PrintGrid()

! One thing to note: when traversing a two-dimensional array, the graph is (j, i) and the array value is grid/[i/]/[j /]

If you don't pay attention to this, you will overturn the cart in the later code!!!

[external chain picture transfer failed. The source station may have anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-0jyim40o-1635691298108)( https://cdn.jsdelivr.net/gh/Qi-China/Picture-PicX @Master / 2048 / print function description (. 4kgd084mupi0.png)]

2. Calculate space function - CalculateEmpty()

```#include <iostream>
using namespace std;

int grid= {{0,1,2,3},
{0,1,2,3},
{0,1,2,3},
{0,1,2,3},};// 4 * 4 matrix
int EmptyBlock = 16;//Number of spaces
int dir;//direction

//1. Print function - PrintGrid()
void PrintGrid()
{
for(int i=0; i<4; i++)//that 's ok
{
for(int j=0; j<4; j++)//column
{
cout << grid[i][j] << "\t";
}
cout << endl;
}
cout<< endl;
}
//2. Calculate space function - CalculateEmpty()
int CalculateEmpty()
{
int cnt = 0;
for(int i=0; i<4; i++)
for(int j=0; j<4; j++)
if(grid[i][j]==0)	cnt++;
return cnt;
}

int main()
{
PrintGrid();
cout << "EmptyBlock = " << CalculateEmpty() << endl;
return 0;
}
``` • Implementation of each module

1. According to the flow chart, it can be divided into the following modules:

• 1 - operate the grid according to the value of dir

• 2 - keyboard input

• 3 - add values at random locations

• 4 - game end detection

2. Implementation details

• 1 - operate the grid according to the value of dir

• (1) Traverse the grid according to the value of dir

Suppose we want to slide the following left matrix to the left (dir=0), and the matrix will look like the right

```{{0,1,2,3},			{{1,2,3,0},
{0,1,2,3},			 {1,2,3,0},
{0,1,2,3},			 {1,2,3,0},
{0,1,2,3},};		 {1,2,3,0},};
```

The starting point of our traversal (x0,y0) is (0,0). Similarly, the starting points of upward sliding, left sliding and downward sliding traversal are (0,0), (3,0), (0,3) respectively

Store the values of x0 and Y0 separately. In addition, we define two groups of deflection arrays (see the figure for specific explanation):

```static int x0 = {0, 0, 3, 0};
static int y0 = {0, 0, 0, 3};
static int firstOffset  = {{1,0},{0,1},{-1,0},{0,-1}};
static int secondOffset = {{0,1},{1,0},{0,1} ,{1,0}};
``` The specific code is as follows (follow the previous code):

```   #include <iostream>
using namespace std;

int grid = {
{0,1,2,3},
{0,1,2,3},
{0,1,2,3},
{0,1,2,3}
};
int EmptyBlock = 16;

void PrintGrid()
{
for(int i=0; i<4; i++)
{
for(int j=0; j<4; j++)
cout << grid[i][j] << " ";
cout << endl;
}
cout << endl;
}

int CalculateEmpty()
{
int cnt = 0;
for(int i=0; i<4; i++)
for(int j=0; j<4; j++)
if(grid[i][j]==0)   cnt++;
return cnt;
}

int dir;
static int x0 = {0, 0, 3, 0};
static int y0 = {0, 0, 0, 3};
static int firstOffset  = {{1,0},{0,1},{-1,0},{0,-1}};
static int secondOffset = {{0,1},{1,0},{0,1} ,{1,0}};

void Move(int dir)
{
int tx, ty;
for(int i=0; i<4; i++)
{
tx = x0[dir] + i*secondOffset[dir];
ty = y0[dir] + i*secondOffset[dir];

for(int j=0; j<4; j++)
{
cout << "(" << tx << ", " << ty << ")" << "\t";

tx += firstOffset[dir];
ty += firstOffset[dir];
}
cout << endl;
}
}

int main(){
PrintGrid();
cout<< "EmptyBlock = " << CalculateEmpty() << endl;
Move(0);
return 0;
}
``` ```  The above is the running result. You can also try to dir Change to 1, 2 and 3, respectively corresponding to the traversal order of the corresponding direction

- (2)During traversal, the corresponding operation is carried out by comparing the front and back pointers

In the second cycle above( tx,ty)Traversal representation of values grid Operations such as moving and merging can be logically reduced to one dimension

We'll build another one test Program to illustrate the merge movement in a one-dimensional array:

```c++
#include <iostream>
using namespace std;

int arr = {0,1,1,2,3,3,1,1,0,0};
void Print()
{
for(int i=0; i<10; i++)
cout << arr[i] << " ";
cout << endl;
}

void Merge()
{
int left, right;
for(left=0,right=1; right<10; right++)
{
if(arr[right]!=0)//A non empty lattice was found
{
if(arr[left]==0)//left is a space, and the right value moves forward to a space
{
arr[left] = arr[right];
arr[right] = 0;
}
else if(arr[left]==arr[right])//Two identical, merge
{
arr[left] *= 2;
arr[right] = 0;
left++;
}
else if(left+1 != right)//The two numbers are different. There is a space in the middle. Move to the next space in left
{
arr[left+1] = arr[right];
arr[right] = 0;
left++;
}
else//Finally, the two numbers are different, adjacent and do not move
{
left++;
}
}
}
}

int main()
{
Print();
Merge();
Print();
return 0;
}
```
```

# 5, Summary

Tags: C++ Mini Program

Posted on Sun, 31 Oct 2021 10:24:16 -0400 by zzz