#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
void Start ();
void Qingchu ();
void restart ();
void Chexiao ();
void random ();
void print ();
void up (int tp[]);
void down (int tp[]);
void left (int tp[]);
void right (int tp[]);
void Which_key ();
void Magic (int c);
void judge ();
int cube[16] = {0};
int main (void)
{
Srand ((unsigned) time (0));
Which_key ();
System ("pause");
return 0;
}
void Which_key ()//accepts user-entered characters to perform corresponding functions.
{
Start ();
Do
{
int haha = _getch ();
switch (haha)
{
Case ' 1 ': Qingchu (); Break Clears 2 in the array.
Case ' 2 ': restart (); Break
Case ' 3 ': Chexiao (); Break This feature is in development.
Case ' W ':
Case ' a ':
Case ' s ':
Case ' d ': magic (haha); Break
Case ' Q ': Exit (1);
}
}while (1);
}
void start ()//Start the game, generating two random 2 or 4.
{
Random ();
Random ();
Print ();
}
void restart ()//This should be the simplest, simply assign the array to 0 and then call the start function.
{
int i;
for (i = 0; i <; Cube[i] = 0,i++);
Start ();
}
void Chexiao ()//This feature is under development.
{
;
}
void Qingchu ()
{
int I, j = 0;
for (i = 0; i <; i++)
if (cube[i] = = 2) cube[i] = 0;
for (i = 0; i <; i++)
if (cube[i] = = 0) J = j + 1;
if (j = =) Start (); If there is only 2 in the matrix, then performing the purge is equivalent to restarting the game.
else print (); If the conversion is performed then the converted matrix is output.
}
void random ()//randomly generates 2 or 4 in a blank place;
{
int temp[16] = {0}, I, j = 0; The idea is to create a temporary array to hold the subscript for those elements with a value of 0,
for (i = 0; i < i++)//Then a random function is used to read one of the subscripts, with a random assignment of 2 or 4. (The probability of 2 is 0.1.) )
if (cube[i] = = 0) temp[j++] = i;
if (j = = 0) judge ();
Else
{
i = rand ()% J;
if (rand ()% >=) cube[temp[i]]= 2;
else Cube[temp[i]] = 4;
}
}
void print ()//two-dimensional array after printing operation;
{
int i;
System ("CLS");
Puts ("|*-*-* *-*-*-* 2 0 4 8 *-*-*-*-*-*|");
Puts ("|\t clear (1) restart (2) undo (3) \ t |");
Puts ("|*-*-* *-*-*-*-*-*-*-* *-*-*-*-*-*-*|");
Puts ("Game instructions: \ n \x01 move up or down on the corresponding keyboard w, S, A, D. \ n \
\X01 exit, press the Q key. \ \x01 Please convert the input method to the English state and then play the game! \ n ");
printf ("-----------------------------");
for (i = 0; i <; i++)
{
if (i = = 0) printf ("\n|");
if (i% 4 = = 0 && i! = 0) printf ("\ n-----------------------------\n|");
if (cube[i] = = 0) printf ("%-4c |", ");
else printf ("%-4d |", Cube[i]);
}
printf ("\ n-----------------------------\ n");
}
void up (int tp[])
{
int a[4][4], I, j, k = 0, temp[4] = {0};
int copy[16];
for (i = 0; i <; Copy[i] = Tp[i], i++); The above two lines of code are used to determine whether to add a random number after the up operation.
for (i = 0; i < 4; i++)
for (j = 0; J < 4; J + +)
A[I][J] = tp[k++]; Converts a one-dimensional array to a two-dimensional array.
for (j = 0; J < 4; J + +)
{
k = 0;
for (i = 0; i < 4; i++)
if (a[i][j]! = 0) temp[k++] = A[i][j]; Assigns an element in a column that is not 0 in order to a temporary array.
for (k = 0; k < 3; k++)
{
if (temp[k] = = 0) break;
if (temp[k] = = temp[k + 1])
{
if (k = = 0) {temp[0] + = temp[1]; temp[1] = temp[2]; temp[2] = temp[3]; temp[3] = 0;}
if (k = = 1) {temp[1] + = temp[2]; temp[2] = temp[3]; temp[3] = 0;}
if (k = = 2) {temp[2] + = temp[3]; temp[3] = 0;}
}
}//This for loop adds the elements of the temporary array and moves 0 to the tail of the array.
k = 0;
for (i = 0; i < 4; A[i][j] = temp[k++], i++); Assigns the elements in the temporary array to the original column.
for (k = 0; k < 4; Temp[k] = 0, k++); Re-assigns the temporary array to a value of 0.
}
k = 0;
for (i = 0; i < 4; i++)
for (j = 0; J < 4; J + +)
tp[k++] = A[i][j]; Converts a two-dimensional array to the original one-dimensional array.
for (i = 0; i <; i++)
{
if (copy[i]! = Tp[i]) break;
}
if (i! =) random (); Determines whether to add random numbers. The decision is based on the addition of random numbers if the array does not change after the up operation is performed.
}
void down (int tp[])
{
int a[4][4], I, j, k = 0, temp[4] = {0};
int copy[16];
for (i = 0; i <; Copy[i] = Tp[i], i++);
for (i = 0; i < 4; i++)
for (j = 0; J < 4; J + +)
A[I][J] = tp[k++];
for (j = 0; J < 4; J + +)
{
k = 0;
for (i = 3; I >=0; i--)
if (a[i][j]! = 0) temp[k++] = A[i][j];
for (k = 0; k < 3; k++)
{
if (temp[k] = = 0) break;
if (temp[k] = = temp[k + 1])
{
if (k = = 0) {temp[0] + = temp[1]; temp[1] = temp[2]; temp[2] = temp[3]; temp[3] = 0;}
if (k = = 1) {temp[1] + = temp[2]; temp[2] = temp[3]; temp[3] = 0;}
if (k = = 2) {temp[2] + = temp[3]; temp[3] = 0;}
}
}
k = 0;
for (i = 3; I >=0; a[i][j] = temp[k++], i--);
for (k = 0; k < 4; Temp[k] = 0, k++);
}
k = 0;
for (i = 0; i < 4; i++)
for (j = 0; J < 4; J + +)
tp[k++] = A[i][j];
for (i = 0; i <; i++)
{
if (copy[i]! = Tp[i]) break;
}
if (i! =) random ();
}
void left (int tp[])
{
int a[4][4], I, j, k = 0, temp[4] = {0};
int copy[16];
for (i = 0; i <; Copy[i] = Tp[i], i++);
for (i = 0; i < 4; i++)
for (j = 0; J < 4; J + +)
A[I][J] = tp[k++];
for (i = 0; i < 4; i++)
{
k = 0;
for (j = 0; J <4; J + +)
if (a[i][j]! = 0) temp[k++] = A[i][j];
for (k = 0; k < 3; k++)
{
if (temp[k] = = 0) break;
if (temp[k] = = temp[k + 1])
{
if (k = = 0) {temp[0] + = temp[1]; temp[1] = temp[2]; temp[2] = temp[3]; temp[3] = 0;}
if (k = = 1) {temp[1] + = temp[2]; temp[2] = temp[3]; temp[3] = 0;}
if (k = = 2) {temp[2] + = temp[3]; temp[3] = 0;}
}
}
k = 0;
for (j = 0; j < 4; A[i][j] = temp[k++], j + +);
for (k = 0; k < 4; Temp[k] = 0, k++);
}
k = 0;
for (i = 0; i < 4; i++)
for (j = 0; J < 4; J + +)
tp[k++] = A[i][j];
for (i = 0; i <; i++)
{
if (copy[i]! = Tp[i]) break;
}
if (i! =) random ();
}
void right (int tp[])
{
int a[4][4], I, j, k = 0, temp[4] = {0};
int copy[16];
for (i = 0; i <; Copy[i] = Tp[i], i++);
for (i = 0; i < 4; i++)
for (j = 0; J < 4; J + +)
A[I][J] = tp[k++];
for (i = 0; i < 4; i++)
{
k = 0;
for (j = 3; J >= 0; j--)
if (a[i][j]! = 0) temp[k++] = A[i][j];
for (k = 0; k < 3; k++)
{
if (temp[k] = = 0) break;
if (temp[k] = = temp[k + 1])
{
if (k = = 0) {temp[0] + = temp[1]; temp[1] = temp[2]; temp[2] = temp[3]; temp[3] = 0;}
if (k = = 1) {temp[1] + = temp[2]; temp[2] = temp[3]; temp[3] = 0;}
if (k = = 2) {temp[2] + = temp[3]; temp[3] = 0;}
}
}
k = 0;
for (j = 3; J >= 0; A[i][j] = temp[k++], j--);
for (k = 0; k < 4; Temp[k] = 0, k++);
}
k = 0;
for (i = 0; i < 4; i++)
for (j = 0; J < 4; J + +)
tp[k++] = A[i][j];
for (i = 0; i <; i++)
{
if (copy[i]! = Tp[i]) break;
}
if (i! =) random ();
}
void Magic (int c)
{
if (c = = 119) up (cube);
if (c = =) down (cube);
if (c = =) left (cube);
if (c = =) right (cube); Determines the action performed based on the character entered.
Print ();
int I, j = 0;
for (i = 0; i <; i++)
{
if (cube[i] = = 2048)
{
printf ("Congratulations, your IQ has reached 2048!! Beat 99.99% of users worldwide! \n\t Please press any key to continue ... ");
GetChar ();
}//Determines whether the elements in the array have 2048, and if so, prints the relevant content.
if (cube[i]! = 0) j + +;
}
if (j = =) judge (); If the elements in the array are not zero, then you can tell if the game will continue.
}
void Judge ()
{
int I, j = 0;
for (i = 0; i <; i++)
{
if (cube[i] = = cube[i + 1] && i! = 3 && I! = 7 && I! = one) j = 1; When the adjacent elements are equal and not 3,7,11, you can do so.
if (cube[i] = = cube[i + 4] && i <) j = 1; The elements with a difference of 4 are equal, and the maximum maximum subscript is less than 16 o'clock, and the game can be played.
}
if (j = = 0) printf ("Don't bother, it's GAME over!! \n\t Please press the active key to start or exit again! ");
}
This article from "12135147" blog, declined reprint!
C language implementation of the 2048 small games