Using vc++6.0 console to achieve 2048 games program _c language

Source: Internet
Author: User
Tags rand

First of all thanks to this warrior selfless sharing, carefully learn the program after a lot of harvest, try to add some comments

Source program is from open source China to see, the original author is Liu Di (sir?)
Address is http://www.oschina.net/code/snippet_593413_46040
Geek_monkey on March 5, 2015 to read the program, benefited
To facilitate myself, as well as more beginners to read, I tried to write a note for reference
I am a C language beginner, if there is a mistake wish to correct me. Light Spray

Copy Code code as follows:

#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 0 digits in the current matrix, and Z is 16, which means the numbers are full, and this bureau loses.
o represents the maximum number of values, this example sets the 1024
typedef int ROW[4]; Row represents an integer array with 4 elements
Row *p=x, *q=y;//p is an array of integer pointers, with 4 groups and 4 elements per group. p[1]=x[1]=* (x+1)
void Show ()//Display function
{
int i,j;
for (i=0;i<4;i++)
{
for (j=0;j<4;j++)
{
if (p[i][j]==0)
{
printf ("-");//no digits (that is, the number is 0 o'clock) display a small horizontal bar
}
Else
{
printf ("%-4d", P[i][j]);//Display an integer with a bit width of 4,--representing left-aligned
}
}
printf ("\ n \ nthe");
}
printf ("\ n \ nthe");
}
void over ()//To swap the 2-D array
{
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 ()/turns the 2-D array counterclockwise at 90°
{
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 () turns the 2-d array clockwise by 90°
{
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//The function is to place a random 2 or 4 on a 0 digit position.
{
int i,j,k;
for (;;)
{
K=rand ()%16,i=k/4,j=k%4;//This operation ensures that the i,j is less than or equal to 4 and is not placed outside the two-dimensional array
if (p[i][j]==0) break;//confirm P[i][j] is blank, then can place 2 or 4
}
if (rand ()%2)///randomly generated 2 or 4, theoretically the result of rand ()%2, 0, 1, the odds are half the percentage
{
p[i][j]=4;
}
Else
{
p[i][j]=2;
}
z++;
}
void merge (char c)
{
int i,j,k,t;
Switch (c)//note, this stitch has no default and no processing statements to press the RIGHT ARROW key. That is, by pressing the RIGHT ARROW key, the switch is skipped
{
Case ' H '://press the UP ARROW key
Right ()/clockwise rotate 90 degrees
Break
Case ' K '://Left
Over ()/around symmetrical exchange
Break
Case ' P '://Down
Left (); Inverse 90
Break
}
Above this switch statement is to convert the matrix transformation, the number to the upper, left or bottom of the operation into the right action.
The function of the following circular statement is to push the numbers of each row to the right and the numbers to merge.
for (i=0;i<4;i++)//line to detect
{
For (j=k=3;j>=0 && p[i][j]==0;j--), start on the right side of line I and find a non 0 element to the left. Which is to let P[i][j] not be 0, end this for statement
if (j<0) continue;//through the bottom right push operation, the leftmost value of line I is also 0, indicating that this line operation is over, jumping out of I for loop, performing the right push of i+1 row
T=P[I][J],P[I][J]=0,P[I][K]=T;//P[I][J] is a number on the left side of P[i][k. The value of J here is found by the previous statement, which guarantees that the p[i][k is not 0 by T-value.
for (j--;j>=0;j--)
{
T=P[I][J];
if (t!=0)//If P[I][J] is not 0, detect if it is the same as the p[i][k on the right
{
p[i][j]=0;
if (p[i][k]==t)
{
Z--, p[i][k]+=t;//The same, doubling the number of non-0 digits and reducing one
o= (t==512)//t for 512 represents the maximum value is 1024, at this time O==1, the game ended with victory
}
Else
{
K--, p[i][k]=t;//The value of P[i][j] is assigned to P[i][k], that is, the data to the right to move a
}
}
}
}
Switch (c)
{
Case ' H '://press, on the direction of the key, the matrix counterclockwise rotation 90 °. This operation is just the opposite of the previous switch.
Left ();
Break
Case ' K '://Around again swap
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 ();
The first byte of the IF (A==-32)/arrow key is -32.char, why is this-32 not yet figured out
{
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;
}
/*
Special keys are two bytes, the first byte is a special key (the normal key is a byte), the second byte is the key of the ASCII code,
When the "normal key" is pressed, its low 8 digits hold the ASCII code of the character.
For special keys, the lower 8 digits are 0. Special keys include arrow keys, function keys, and so on. High 8-bit byte to store the scan code of the key
#define Key_left K Left
#define Key_right M Right
#define KEY_UP on H.
#define UNDER Key_down P
*/

The above is the content of this article to share, I hope to learn VC + + can help.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.