2048 games 4X4C language, 2048 games 4x4c

Source: Internet
Author: User

2048 games 4X4C language, 2048 games 4x4c

Sorry, I don't know how to add attachments. I originally wanted to upload the source code (. c) file.

I haven't found it for a long time. I can only write the source program to a file and paste it here.

If you are interested, you can copy the following code and run it. The format needs to be adjusted by yourself.

You can also access Baidu cloud disk http://pan.baidu.com/s/1hsa675mto download the source file.

/X 2048 */

# Include <stdio. h>
# Include <stdlib. h>
# Include <conio. h>
# Include <time. h>

Void print (void);/* display the game interface */
Int add (void);/* process the array of intermediate variables */

Int code [4] [4] =
{
{0, 0, 0 },
{0, 0, 0 },
{0, 0, 0 },
{0, 0, 0 },
};/* 16 grids in the game */
Int temp [5];/* intermediate variable */
Int move = 0;/* number of moves */
Int score = 0;/* score */

 

Int main (void)
{
Int gameover = 0;/* determines whether the game is over, 1 ends, 0 continues */
Int I, j;
Int change = 1;/* determines whether the number in the grid changes, and 0 does not change */
Char input;
Srand (unsigned) time (NULL);/* set the starting point of a random number */
While (gameover = 0)
{
If (change> = 1)/* Add a new number only when the number changes */
{
Do
{
I = (unsigned) rand () % 4;
J = (unsigned) rand () % 4;
} While (code [I] [j]! = 0 );
If (unsigned) rand () % 4 = 0)
{
Code [I] [j] = 4;
}
Else
{
Code [I] [j] = 2;/* randomly select a space and fill in 2 or 4 */
}
Move ++;/* increase the number of times */
}
Print ();/* display */
Input = getch ();/* input direction */
Change = 0;
Switch (input)
{
Case '0':/* exit */
Printf ("Are you sure to exit? (Y/n )");
Input = getchar ();
If (input = 'y' | input = 'y ')
Exit (0 );
Break;

Case 'W ':

Case 'W':/* top */
For (j = 0; j <= 3; j ++)
{
For (I = 0; I <= 3; I ++)
{
Temp [I] = code [I] [j];/* move a column to an intermediate variable */
}
Temp [4] = 0;
Change + = add ();
For (I = 0; I <= 3; I ++)
{
Code [I] [j] = temp [I];/* move the processed intermediate variable back */
}
}
Break;

Case 'A ':

Case 'A':/* left */
For (I = 0; I <= 3; I ++)
{
For (j = 0; j <= 3; j ++)
{
Temp [j] = code [I] [j];/* move the number of rows to the intermediate variable */
}
Temp [4] = 0;
Change + = add ();
For (j = 0; j <= 3; j ++)
{
Code [I] [j] = temp [j];/* move the processed intermediate variable back */
}
}
Break;

Case's ':

Case's */
For (j = 0; j <= 3; j ++)
{
For (I = 0; I <= 3; I ++)
{
Temp [I] = code [3-i] [j];/* move a column to an intermediate variable */
}
Temp [4] = 0;
Change + = add ();
For (I = 0; I <= 3; I ++)
{
Code [3-i] [j] = temp [I];/* move the processed intermediate variable back */
}
}
Break;

Case 'D ':

Case 'D':/* right */
For (I = 0; I <= 3; I ++)
{
For (j = 0; j <= 3; j ++)
{
Temp [j] = code [I] [3-j];/* move the number of rows to the intermediate variable */
}
Temp [4] = 0;
Change + = add ();
For (j = 0; j <= 3; j ++)
{
Code [I] [3-j] = temp [j];/* move the processed intermediate variable back */
}
}
Break;
}
Gameover = 1;
For (I = 0; I <= 3; I ++)
For (j = 0; j <= 3; j ++)
If (code [I] [j] = 0)
Gameover = 0;/* the game ends when all the grids are filled */
}
Printf ("Game over! \ N ");
Getch ();

Return 0;
}

Void print (void)/* display game interface */
{
Int I, j;
System ("CLS");/* clear screen */
Printf ("2048 \ n ");
Printf ("W--UP A--LEFT S--DOWN D--RIGHT 0-EXIT \ n ");
Printf ("Score: % d Move: % d \ n", score, move );
Printf ("Made by lanshanxiao \ n ");
Printf ("| --------------------- | \ n");/* display horizontal separation line */
For (I = 0; I <= 3; I ++)
{
For (j = 0; j <= 3; j ++)
{
If (code [I] [j] = 0)
{
Printf ("|");/* 0 display space */
}
Else
{
Printf ("| % 5d", code [I] [j]);/* display numbers and separation lines */
}
}
Printf ("| \ n | --------------------- | \ n");/* display horizontal separation line */
}
}

Int add (void)/* process the array of intermediate variables */
{
Int I;
Int t = 0;
Int change = 0;/* determines whether the array is changed. The value 0 remains the same and the value 1 changes */
Do
{
For (I = 0; I <= 3; I ++)
{
If (temp [I] = 0)
{
If (temp [I]! = Temp [I + 1])
Change = 1;/* When a value after 0 is not 0, the group changes */
Temp [I] = temp [I + 1];
Temp [I + 1] = 0;
}
}/* Remove the intermediate 0 */
T ++;
} While (t <= 3);/* repeat multiple times */
For (I = 1; I <= 3; I ++)
{
If (temp [I] = temp [I-1])
{
If (temp [I]! = 0)
{
Change = 1;/* When two non-zero identical numbers are added, the group changes */
Score = score + temp [I];/* extra points */
}
Temp [I-1] = temp [I-1] * 2;
Temp [I] = 0;
}
}/* Add two adjacent identical numbers */

Return change;
}

Related Article

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.