This is the 28th day of my participation in the August Text Challenge.More challenges in August

Not much else to say, first on the renderingsWe all know that 2048 is 4The square of 4, for the convenience of the array table below we generate 5An array of 5, using only subscripts from 1 to 4.

Initialization starts with two random targets

// Program initialization
void init(a)
{
	score = 0;
	memset(a, 0.sizeof(a));
	int x1 = rand() % 4 + 1, y1 = rand() % 4 + 1, x2 = rand() % 4 + 1, y2 = rand() % 4 + 1;	// Randomly generate two initial points
	a[x1][y1] = a[x2][y2] = 2;				// The initial point is initialized to 2
}
Copy the code

The drawing operation is to print out all the arrays. If the number is 0, the blank characters will be printed. If the number is not 0, the number will be printed.

void drawmap(a)
{
	system("cls");
	
	for (int i = 1; i <= 4; i++)
	{
		for (int j = 1; j <= 4; j++)
		{
			if (a[i][j])			// If there is no number in the position, it is not drawn
			{
				if(a[i][j]>1000)
					cout<<a[i][j]<<"";
				else if(a[i][j]>100)
					cout<<a[i][j]<<"";
				else if(a[i][j]>10)
					cout<<""<<a[i][j]<<"";
				else
					cout<<""<<a[i][j]<<"";
			}
			else
			{
				cout<<"";
			}
		}
		cout<<"\n"; }}Copy the code

And then the move, I’m using the input character control here, wSAD, up, down, left, right.

// Player actions
void move(a)
{
	memcpy(b, a, sizeof(a));			// Back up a to B
	memset(mov, false.sizeof(mov));	// initialize mov to false (all points not moved)
	char c;
	cin>>c;

	if(c=='w') / / up
	{
		for (j = 1; j <= 4; j++)
			for (i = 2; i <= 4; i++)
			{
				if(! a[i][j])continue;
				int k = i;
				while(! a[k -1][j] && k >= 2)
				{
					a[k - 1][j] = a[k][j];
					a[k][j] = 0;
					k--;
				}
				if (a[k][j] == a[k - 1][j] && ! mov[k -1][j])
				{
					a[k - 1][j] = 2 * a[k][j];
					a[k][j] = 0;
					mov[k - 1][j] = true;
					score += a[k - 1][j]; }}}else if(c=='s') / / down
	{
		/ /...
	} 
	else if(c=='a') / / to the left
	{
		/ /...
	} 
	else if(c=='d') {}}Copy the code

And judging the end of the game and the victory of the game

// Determine the end of the game, for any position, the position is empty or surrounded by the position of the number equal to the position of the number, it can continue to move (the game can continue), otherwise the game is over

// Judge victory, if any of the positions reach 2048, then victory

bool gameover(a)
{

	for (i = 1; i <= 4; i++)
		for ( j = 1; j <= 4; j++)
			if(! a[i][j] || a[i][j] == a[i +1][j] || a[i][j] == a[i - 1][j] || a[i][j] == a[i][j + 1] || a[i][j] == a[i][j - 1])return false;
	
	return true;
}


bool win(a)
{
	for ( i = 1; i <= 4; i++)
		for ( j = 1; j <= 4; j++)
			if (a[i][j] == 256)return true;
	return false;
}
Copy the code

In fact, this 2048 small game is not particularly difficult, the main difficulty lies in the algorithm. As long as you understand the idea of the algorithm, it is very easy to make a complete game.

The interface is not the most important thing in making a game, and any game engine can make the interface even more important. If you want to learn game programming, I’m here. Public id: poetic code. I’ll show you.