With VC++6.0 console to achieve 2048 small game procedures
- 2020-04-02 02:57:22
- OfStack
First of all, thanks to the hero for his selfless sharing. After learning this program carefully, I will gain a lot. Try to add some comments
The source program is from open source China, the original author is liu di (Sir & # 63;)
The address is http://www.oschina.net/code/snippet_593413_46040
Geek_monkey read the program on March 5, 2015, and learned a lot
For the convenience of myself, as well as for more beginners to read, I have tried to write notes for reference
I am a C language beginner, if there is an error hope correction. Light spray
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <time.h>
int x[4][4],y[4][4],z=0,o=0;//Z represents the number of non-zero Numbers in the current matrix. A z of 16 indicates that the Numbers are full and that we have typed
//O represents the value of the maximum number, which in this case is set to 1024
typedef int row[4]; //Row represents an integer array with four elements
row *p=x, *q=y;//P is an integer pointer array with four groups of four elements. P = x [1] [1] = * (x + 1) < br / >
void show()//Displays the function
{
int i,j;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(p[i][j]==0)
{
printf("- ");//A bar
is displayed when no number is placed (i.e., when the number is 0)
}
else
{
printf("%-4d ",p[i][j]);//Displays an integer with a bit width of 4,- to indicate left alignment
}
}
printf("nn");
}
printf("nn");
}
void over()//Swap the 2 - dimensional array to
{
int i,j;
row *r;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
{
q[i][3-j]=p[i][j];
}
r=p,p=q,q=r;
}
void left()//Rotate the 2-dimensional array 90 ° counterclockwise
{
int i,j;
row *r;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
{
q[3-j][i]=p[i][j];
}
r=p,p=q,q=r;
}
void right()//Rotate the 2-dimensional array 90 ° clockwise
{
int i,j;
row *r;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
{
q[j][3-i]=p[i][j];
}
r=p,p=q,q=r;
}
void inc()//This function is used to place a random 2 or 4
in the position of the 0 number
{
int i,j,k;
for(;;)
{
k=rand()%16,i=k/4,j=k%4;//This is to make sure that I,j is less than or equal to 4, and that it doesn't go out of the two-dimensional array
if(p[i][j]==0)break;//Make sure that p[I][j] is blank before placing 2 or 4
}
if(rand()%2) //Random 2 or 4, theoretically rand()%2, 0,1, the odds are 50/50
{
p[i][j]=4;
}
else
{
p[i][j]=2;
}
z++;
}
void merge(char c)
{
int i,j,k,t;
switch(c)//Note: there is no default in this stitch, and there is no processing statement for pressing the right direction key. That is, pressing the right arrow key skips the switch
{
case 'H'://Detected that the up key
was pressed
right();//Rotate 90 degrees clockwise
break;
case 'K'://Left < br / >
over();//I'm going to replace this with
break;
case 'P'://The < br / >
left(); //Inverse 90 < br / >
break;
}
//The switch statement at the top converts the matrix transformation, the operation that pushes a number up, left, or down, to the operation that pushes it to the right. < br / >
//The function of the loop below is to push the Numbers of each row to the right, and merge them into the same size. < br / >
for(i=0;i<4;i++)//Detect
line by line
{
for(j=k=3;j>=0 && p[i][j]==0;j--);//I start at the right of row I, and I look to the left for non-zero elements. In other words, when p[I][j] is not 0, end this for statement
if(j<0)continue;//After the right push operation below, the leftmost value of line I is not 0, indicating that the operation is over, break out of the for loop of I, and perform the right push operation of line I +1
t=p[i][j],p[i][j]=0,p[i][k]=t;//P[I][j] is a number to the left of P[I][k]. The value of j here is derived from the previous statement, and the t-pass ensures that this p[I][k] is not 0
for(j--;j>=0;j--)
{
t=p[i][j];
if(t!=0)//If p[I][j] is not 0, it is checked to see if it is the same
as p[I][k] on the right
{
p[i][j]=0;
if(p[i][k]==t)
{
z--,p[i][k]+=t;//The same is doubled, and the number of non-zero digits is reduced by one
o=(t==512);//T = 512 means the maximum value is 1024, at which time o==1, the game ends with victory
}
else
{
k--,p[i][k]=t;//Without stopping, the value of p[I][j] is assigned to p[I][k], that is, the data moves one bit to the right
}
}
}
}
switch(c)
{
case 'H'://Press the up key and rotate the matrix 90 ° counterclockwise. This operation is the opposite of the previous switch
left();
break;
case 'K'://Switch left and right again to
over();
break;
case 'P':
right();
break;
}
inc();
}
int main()
{
char a,b;
srand(time(NULL));
inc();
inc();//Place two initial values
show();
while(z<16 && !o)//Game end condition, z==16 or o==1
{
a=getch();
if(a==-32)//The first byte of the direction key is -32. Char is unsigned. Why -32
{
b=getch();
if(b==72||b==75||b==77||b==80)
{
merge(b);
show();
}
}
}
if(o)
{
printf("congratulations!");
}
else
{
printf("sorry, you failed!");
}
getch();
return 0;
}
/*
The special key is two bytes, the first byte means that the press is a special key (a normal key is one byte), the first byte Two bytes is the key ASCII Code,
When the "normal key" is pressed, it's low 8 The number of digits that holds the character ASCII Code.
For special bonds, low 8 Bit is 0 . Special keys include arrow keys, function keys, etc. high 8 A bit byte holds the scan code for the key
#define KEY_LEFT 75 K Left < br / >
#define KEY_RIGHT 77 M right
#define KEY_UP 72 H on
#define KEY_DOWN 80 P The < br / >
*/
Above is the content of this article to share, I hope to help you learn VC++.