C++

C language 2048 small game


This article shares the specific code of C language 2048 game for your reference. The specific content is as follows

The slider has color and the highest score (using the file)

#include <stdio.h>
#include <stdlib.h>
#include <conio.h> /* use getch() function */
#include <time.h>
#include <windows.h> /* use Sleep() function */

void Menu();// The menu
void Rule_of_game();// The rules of the game
void Begin(int max); // start
void Table();// print 4 x 4 grid
int Random_number1();// produce 2 or 4 The random number
int Random_number2();// produce 0 , 1 , 2 , 3 The random number
int Get(int *p_cnt, int score);// Input order
void Move();// Keep on 1 Secondary checkerboard layout
int If_move();// Decide whether to move or not
int Over();// Judge whether it's over
void Establish_game();// Create a file to save the highest score
void Write_max(int score);// to 2048 The game - The highest record is written as the highest score
int Read_max();// Read the file where the highest score was recorded
void Establish_file();// Create archive file
void Write_file(int *p_cnt, int scort);// The archive
int Read_file(int *p_cnt);// Read the file
void again();// Whether to continue the game
int color(int c);// Change the color
void box_color(int x);// Different values correspond to different colors

int a[4][4] = {0};//  Define global 2 Dimensional array
int A[4][4]; // Keep on 1 The step chess
char *name_max = "2048 The game - The previous record ";
char *name_file = "[ blank ]-2048 The game ";

int main() {
 system("mode con cols=90 lines=30");
 system("title 2048 Super big games "); // Program title
 system("color F0");//F  Represents the background color, 0 Foreground color
 Menu();
 system("pause"); // Pause and enter any key to continue
 system("cls"); // Clear the screen
 again(); // Whether to continue the game
 return 0;
}

void Menu() // The menu
{
 printf("\t\t  �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �  \n");
 printf("\t\t �   Welcome to use by [ blank ] To make the 2048 Super big games   � \n");
 printf("\t\t  �  ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─  �  \n");
 printf("\t\t Please enter the options:   � \n");
 printf("\t\t �   ┌ ─ ─ ─ ┐   � \n");
 printf("\t\t �   │  1  │   The rules of the game   � \n");
 printf("\t\t �   └ ─ ─ ─ ┘   � \n");
 printf("\t\t �   ┌ ─ ─ ─ ┐   � \n");
 printf("\t\t �   │  2  │   Start the game   � \n");
 printf("\t\t �   └ ─ ─ ─ ┘   � \n");
 printf("\t\t �   ┌ ─ ─ ─ ┐   � \n");
 printf("\t\t �   │  3  │   To continue the game   � \n");
 printf("\t\t �   └ ─ ─ ─ ┘   � \n");
 printf("\t\t �   ┌ ─ ─ ─ ┐   � \n");
 printf("\t\t �   │  4  │   Quit the game   � \n");
 printf("\t\t �   └ ─ ─ ─ ┘   � \n");
 printf("\t\t  �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �  \n");
 int x = 1, max = 0;
 while(x){
 switch(getch()){
 case '1':
 x = 0;
 Rule_of_game();// The rules of the game
 Menu();
 break;
 case '2':
 x = 0;
 system("cls");// Clear the screen
 Establish_game(name_file);// Create a new read file
 Establish_game(name_max);// Create a new file that holds the highest record
 Begin(max); // Start the game
 break;
 case '3':
 x = 0;
 system("cls");// Clear the screen
 max = Read_max();// Read the previous record
 Begin(max); // Start the game
 break;
 case '4':
 exit(0);
 default:
 printf(" Illegal option entered, please select again! \n");
 }
 }
}

void Rule_of_game()
{
 system("cls");// Clear the screen
 printf("  �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �  \n");
 printf(" The game is played by pressing the button W , S , A , D (Case insensitive) 4 The keys control the slider up, down, left and right.  �  \n");
 printf(" As the slider moves, if there are adjacent and equal Numbers of the slider will be added and the corresponding score will be obtained.   � \n");
 printf(" The game ends when all positions on the board have Numbers that cannot be combined. This game has not been set up successfully.   � \n");
 printf(" Input during the game I To save and exit the game, enter O Is read file, input P Quit the game.   � \n");
 printf(" The "Start game" will clear the record and save. The "Continue game" will save the previous record and read the file.   � \n");
 printf("  �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �  \n");
 system("pause");// suspended
 system("cls");// Clear the screen
}

void Begin(int max)
{
 int score = 0;
 int sign = 1;
 int h, l, cnt = 0;
 int *p_cnt = &cnt;
 while(1) {
 printf("\t\t  �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �  \n");
 printf("\t\t �   Welcome to use by [ blank ] To make the 2048 Super big games   � \n");
 printf("\t\t  �  ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─  �  \n");
 printf("\t\t  �  mobile:   ┌ ─ ─ ─ ┐   Archive:   Read the file:   Exit:   � \n");
 printf("\t\t �   │  W  │   � \n");
 printf("\t\t �   ┌ ─ ─ ─ ┼ ─ ─ ─ ┼ ─ ─ ─ ┐   ┌ ─ ─ ─ ┐   ┌ ─ ─ ─ ┐   ┌ ─ ─ ─ ┐   � \n");
 printf("\t\t �   │  A  │  S  │  D  │   │  I  │   │  O  │   │  P  │   � \n");
 printf("\t\t �   └ ─ ─ ─ ┴ ─ ─ ─ ┴ ─ ─ ─ ┘   └ ─ ─ ─ ┘   └ ─ ─ ─ ┘   └ ─ ─ ─ ┘   � \n");
 printf("\t\t  �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �  \n");
 while(1){
 if(sign == 1){
 h = Random_number2();
 l = Random_number2();
 if(a[h][l] == 0){
 a[h][l] = Random_number1();
 cnt ++;
 break;
 }
 }
 else{
 break;
 }

 }
 Table();// Printed board
 printf("\t\t Number of steps taken: %d\t Current score: %d\n\t\t Highest record: %d\n", cnt-1, score, max);
 Move();// Keep the board
 score += Get(p_cnt, score);// score
 sign = If_move();// Determine if the board has changed
 if(Over() == 0){
 if(max < score){// To break the record
 Establish_game(name_max);// Clear previous records
 Write_max(score);// Save this record
 printf("\t\t Congratulations on breaking the record! \n");
 }
 break;
 }
 system("cls"); // Clear the screen
 }
}

void Table()
{
 int h, l;
 for(h = 0; h < 4; h ++){
 if(h == 0){
 printf("\t\t\t ┏ ━ ━ ━ ━ ━ ━ ┳ ━ ━ ━ ━ ━ ━ ┳ ━ ━ ━ ━ ━ ━ ┳ ━ ━ ━ ━ ━ ━ ┓ \n");
 }
 else {
 printf("\t\t\t ┣ ━ ━ ━ ━ ━ ━ ╋ ━ ━ ━ ━ ━ ━ ╋ ━ ━ ━ ━ ━ ━ ╋ ━ ━ ━ ━ ━ ━ ┫ \n");
 }
 printf("\t\t\t ┃   ┃   ┃   ┃   ┃ \n");
 for(l = 0; l < 4; l ++){
 if(a[h][l] == 0){// if 2 If the position of the dimension array corresponding to the checkerboard is zero, no data is put in
 if(l == 0){
 printf("\t\t\t ┃  ");
 }
 else {
 printf(" ┃  ");
 }
 }
 else{// Otherwise put in 2 Dimensional array corresponding data
 if(l == 0){
 printf("\t\t\t ┃ ");
 box_color(a[h][l]);// Judge the value of the slider and give the corresponding color
 printf("%5d ", a[h][l]);
 color(0);// Restore white background
 }
 else {
 printf(" ┃ ");
 box_color(a[h][l]);// Judge the value of the slider and give the corresponding color
 printf("%5d ", a[h][l]);
 color(0);// Restore white background
 }
 }
 }
 printf(" ┃ \n");
 printf("\t\t\t ┃   ┃   ┃   ┃   ┃ \n");
 }
 printf("\t\t\t ┗ ━ ━ ━ ━ ━ ━ ┻ ━ ━ ━ ━ ━ ━ ┻ ━ ━ ━ ━ ━ ━ ┻ ━ ━ ━ ━ ━ ━ ┛ \n");
}

int Random_number1()/* Randomly generated 2 or 4*/
{
 int s;
 srand( time(NULL) );
 s = ((unsigned)rand() % 2);
 s = s == 0 ? 2 : 4;
 return s;
}

int Random_number2()/* Randomly generated 2 The subscript of a dimension array */
{
 int x;
 x = ((unsigned)rand() % 4);
 return x;
}

int Get(int *p_cnt, int score)
{
 int h, l, t, sum = 0;
 char ch;
 ch = getch();// The input character
 switch (ch){
 /* The slider moves up */
 case 'w':
 case 'W':
 /* Upward aggregation */
 for(l = 0; l < 4; l ++){
 for(t = 0; t < 4; t ++){
 for(h = 3; h > 0; h --){
 if(a[h-1][l] == 0){
 a[h-1][l] = a[h][l];
 a[h][l] = 0;
 }
 }
 }
 }
 /* In the vertical direction, add up Numbers that are adjacent and identical */
 for(l = 0; l < 4; l ++){
 for(h = 0; h < 3; h ++){
 if(a[h][l] == a[h+1][l]){
 a[h][l] += a[h+1][l];
 a[h+1][l] = 0;
 sum += a[h][l];
 }
 }
 }
 /* Upward aggregation */
 for(l = 0; l < 4; l ++){
 for(t = 0; t < 4; t ++){
 for(h = 3; h > 0; h --){
 if(a[h-1][l] == 0){
 a[h-1][l] = a[h][l];
 a[h][l] = 0;
 }
 }
 }
 }
 break;
 case 's':
 case 'S':
 for(l = 0; l < 4; l ++){
 for(t = 0; t < 4; t ++){
 for(h = 0; h < 3; h ++){
 if(a[h+1][l] == 0){
 a[h+1][l] = a[h][l];
 a[h][l] = 0;
 }
 }
 }
 }
 for(l = 0; l < 4; l ++){
 for(h = 3; h > 0; h --){
 if(a[h][l] == a[h-1][l]){
 a[h][l] += a[h-1][l];
 a[h-1][l] = 0;
 sum += a[h][l];
 }
 }
 }
 for(l = 0; l < 4; l ++){
 for(t = 0; t < 4; t ++){
 for(h = 0; h < 3; h ++){
 if(a[h+1][l] == 0){
 a[h+1][l] = a[h][l];
 a[h][l] = 0;
 }
 }
 }
 }
 break;
 case 'a':
 case 'A':
 for(h = 0; h < 4; h ++){
 for(t = 0; t < 4; t ++){
 for(l = 3; l > 0; l --){
 if(a[h][l-1] == 0){
 a[h][l-1] = a[h][l];
 a[h][l] = 0;
 }
 }
 }
 }
 for(h = 0; h < 4; h ++){
 for(l = 0; l < 3; l ++){
 if(a[h][l] == a[h][l+1]){
 a[h][l] += a[h][l+1];
 a[h][l+1] = 0;
 sum += a[h][l];
 }
 }
 }
 for(h = 0; h < 4; h ++){
 for(t = 0; t < 4; t ++){
 for(l = 3; l > 0; l --){
 if(a[h][l-1] == 0){
 a[h][l-1] = a[h][l];
 a[h][l] = 0;
 }
 }
 }
 }
 break;
 case 'd':
 case 'D':
 for(h = 0; h < 4; h ++){
 for(t = 0; t < 4; t ++){
 for(l = 0; l < 3; l ++){
 if(a[h][l+1] == 0){
 a[h][l+1] = a[h][l];
 a[h][l] = 0;
 }
 }
 }
 }
 for(h = 0; h < 4; h ++){
 for(l = 3; l > 0; l --){
 if(a[h][l] == a[h][l-1]){
 a[h][l] += a[h][l-1];
 a[h][l-1] = 0;
 sum += a[h][l];
 }
 }
 }
 for(h = 0; h < 4; h ++){
 for(t = 0; t < 4; t ++){
 for(l = 0; l < 3; l ++){
 if(a[h][l+1] == 0){
 a[h][l+1] = a[h][l];
 a[h][l] = 0;
 }
 }
 }
 }
 break;
 case 'i':
 case 'I': // Save the current game layout
 Establish_file();// Create a file for the archive
 Write_file(p_cnt, score);// Call archive function
 break;
 case 'o':
 case 'O':
 sum = Read_file(p_cnt);// Read the file
 break;
 case 'p':
 case 'P':
 exit(0);// Exit the program
 default:
 break;
 }

 return sum;
}

void Move()
{
 int h, l;
 for(h = 0; h < 4; h ++){
 for(l = 0; l < 4; l ++){
 A[h][l] = a[h][l];
 }
 }
}

int If_move()
{
 int h, l, sign = 0;
 for(h = 0; h < 4; h ++){
 for(l = 0; l < 4; l ++){
 if(a[h][l] != A[h][l]){// The condition box is not entered, indicating that the slider has not moved
 sign = 1;
 goto out;
 }
 }
 }
 out:
 return sign;
}

int Over()
{
 int over = 0;
 int h, l;
 for(h = 0; h < 4; h ++){
 for(l = 0; l < 4; l ++){
 if(a[h][l] == 0){// The space
 over = 1;// The game continue
 return 1;
 }
 }
 }
 if(over == 0){// There is no space
 for(h = 0; h < 3; h ++){
 for(l = 0; l < 3; l ++){
 if(a[h][l] == a[h][l+1]){
 over = 1;// The game continue
 break;
 }
 else if(a[h][l] == a[h+1][l]){
 over = 1;// The game continue
 break;
 }
 }
 if(over == 1){
 break;
 }
 }
 }
 return over;// Game over
}

void Establish_game()// Create a file
{
 int h;
 if(h = fopen(name_max, "wb") == NULL){ // function fopen() return NULL Fails to create the file
 printf("\t\t File creation failed! The program will exit automatically  . . .\n");
 Sleep(3000);// sleep 3000 Milliseconds, that is, stay 3 seconds
 exit(0); // Exit the program
 }
}

void Write_max(int score)// Written to the file
{
 /*FILE *fp Is a pointer to a file */
 FILE *fp=fopen(name_max,"at+");// at+ Read/write opens or creates 1 A text file; Allow reading and writing.
 if(fp==NULL) // function fopen() return NULL Fails to create the file
 {
 printf("\t\t Failed to save the highest score! The program will exit automatically  . . .");
 Sleep(3000);// sleep 3000 Milliseconds, that is, stay 3 seconds
 exit(0); // Exit the program
 }
 else{
 fprintf(fp, "%d", score);// will score The value of the write *pf Point to the file
 }
 fclose(fp);// Shut down *pf File to point to
}

int Read_max()// Read the file
{
 int max_score = 0;
 FILE *fp=fopen(name_max,"at+");// at+ Read/write opens or creates 1 A text file; Allow reading and writing.
 if(fp==NULL)
 {
 printf("\t\t Read failed! The program will exit automatically  . . .");
 Sleep(3000);// sleep 3000 Milliseconds, that is, stay 3 seconds
 exit(0); // Exit the program
 }
 else{
 fscanf(fp, "%d", &max_score);// from *pf Points to a value in the file max_score
 }
 fclose(fp);// Shut down *pf File to point to

 return max_score;// Returns the highest recorded value
}

void Establish_file()// Create a file
{
 int h;
 if(h = fopen(name_file, "wb") == NULL){ // function fopen() return NULL Fails to create the file
 printf("\t\t File creation failed! The program will exit automatically  . . .\n");
 Sleep(3000);// sleep 3000 Milliseconds, that is, stay 3 seconds
 exit(0); // Exit the program
 }
}

void Write_file(int *p_cnt, int score)
{
 int h, l;
 FILE *fp=fopen(name_file,"at+");// at+ Read/write opens or creates 1 A text file; Allow reading and writing.
 if(fp==NULL)
 {
 printf("\t\t Archive failed! The program will exit automatically  . . .\n");
 Sleep(3000);// sleep 3000 Milliseconds, that is, stay 3 seconds
 exit(0);
 }
 else{
 for(h = 0; h < 4; h ++){
 for(l = 0; l < 4; l ++){
 fprintf(fp, "%d ", a[h][l]);
 }
 }
 }
 fprintf(fp, "%d ", score);
 fprintf(fp, "%d ", *p_cnt-1);
 printf("\t\t Archive successful! The game will exit automatically  . . .\n");
 Sleep(3000); // sleep 3000 Milliseconds, that is, stay 3 seconds
 exit(0);
 fclose(fp);// Close the file
}

int Read_file(int *p_cnt)
{
 int h, l, score = 0;
 FILE *fp=fopen(name_file,"at+");// at+ Read/write opens or creates 1 A text file; Allow reading and writing.
 if(fp==NULL)
 {
 printf("\t\t Failed to read file! The program will exit automatically  . . .\n");
 Sleep(3000); // sleep 3000 Milliseconds, that is, stay 3 seconds
 exit(0);
 }
 else{
 for(h = 0; h < 4; h ++){
 for(l = 0; l < 4; l ++){
 fscanf(fp, "%d ", &a[h][l]);
 }
 }
 }
 fscanf(fp, "%d ", &score);
 fscanf(fp, "%d ", p_cnt);
 fclose(fp); // Close the file

 return score;
}

void again()// Whether to continue the game
{
 printf("\t\t  �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �  \n");
 printf("\t\t �   You have failed   � \n");
 printf("\t\t  �  ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─  �  \n");
 printf("\t\t Whether or not to continue:   � \n");
 printf("\t\t �   ┌ ─ ─ ─ ┐   � \n");
 printf("\t\t �   │  1  │   Help me up, I can come again 1 Bureau!   � \n");
 printf("\t\t �   └ ─ ─ ─ ┘   � \n");
 printf("\t\t �   ┌ ─ ─ ─ ┐   � \n");
 printf("\t\t �   │  2  │   No, no, I want to study!   � \n");
 printf("\t\t �   └ ─ ─ ─ ┘   � \n");
 printf("\t\t  �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �  \n");
 if(_getch() == '1'){
 system("cls");// Clear the screen
 int h, l;
 for(h = 0; h < 4; h ++){
 for(l = 0; l < 4; l ++){
 a[h][l] = 0;
 A[h][l] = 0;
 }
 }
 Menu();// Back to the menu
 }
 else {
 printf("\t\t Game over! \n");
 }
}

int color(int c)
{
 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
 BACKGROUND_INTENSITY |
 BACKGROUND_BLUE |
 BACKGROUND_RED |
 BACKGROUND_GREEN |
 c);
 return 0;
}

void box_color(int x)
{
 switch(x){
 case 2:
 color(8);
 break;
 case 4:
 color(0);
 break;
 case 8:
 color(2);
 break;
 case 16:
 color(6);
 break;
 case 32:
 color(10);
 break;
 case 64:
 color(3);
 break;
 case 128:
 color(9);
 break;
 case 256:
 color(11);
 break;
 case 512:
 color(5);
 break;
 case 1024:
 color(13);
 break;
 case 2048:
 color(12);
 break;
 default :
 color(4);
 break;
 }
}