Using C language to realize small game -- 2048

Posted by mattnoble on Tue, 07 Jan 2020 18:08:47 +0100

I finished the little game without interface and the game of computer in my blog, Using C language to realize small game - three chess
And a little game full of memories of the same year Using C language to realize small game - minesweeping
Next, let's write another simple, interface free, small game, computer-operated game 2048

Although there is no interface, we need to display a frame, corresponding to the number of each position, to accept from the keyboard up and down left and right, to realize the up, down, left and right movement of the whole block, increase the integral, and judge whether there is still space, whether the game continues, etc,

#define Row 4 / / line
#define Col 4 / / column
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#include<windows.h>

void menu();
int game();
void PlayerGo(int arr[Row][Col], int row, int col);
void Display(int arr[Row][Col], int row, int col);
void Init(int arr[Row][Col], int row, int col, char f);
char IsWin(int arr[Row][Col], int row, int col);
void ComputerGo(int arr[Row][Col], int row, int col);
#endif // __GAME_H__
#include"game.h"
void menu()//menu
{
    system("color F9");
    printf("\n\n\n\n\t\t Welcome to 2048");
    Sleep(2000);
    system("cls");
    printf("\n\n\t****************************\n");
    printf("\t****   1.Start the game      *****\n");
    printf("\t****   0.Sign out          *****\n");
}
void Init(int arr[Row][Col], int rows, int cols, char f)//Array initialization
{
    memset(arr, f, rows*cols*16);
}
void Display(int arr[Row][Col], int row, int col)//display
{
    int i = 0;
    int j = 0;
    printf("\t\t\t");
    for (i = 0; i < row; i++)
    {
        printf(" ______");
    }
    for (i = 0; i < row; i++)
    {
        printf("\n\t\t\t|");
        for (j = 0; j < col; j++)
        {
            if (arr[i][j] == 0)
                printf("_    _|");
            else
                printf("_%4d_|", arr[i][j]);
        }
    }
}
void NumMoveup(int arr[Row][Col], int row, int col,int ud)
{
    int i = 0;
    int j = 0;
    for (i = row-1; i >0; i--)
    {
        for (j = 0; j < col; j++)
        {
            if (arr[i - ud][j] == 0)
            {
                arr[i - ud][j] = arr[i][j];
                arr[i][j] = 0;
            }
            else if (arr[i - ud][j] == arr[i][j])
            {
                arr[i - ud][j] = 2 * arr[i][j];
                arr[i][j] = 0;
            }
        }
    }
}
void NumMovedown(int arr[Row][Col], int row, int col, int ud)
{
    int i = 0;
    int j = 0;
    for (i = 0; i < row-1; i++)
    {
        for (j = 0; j < col; j++)
        {
            if (arr[i +ud][j] == 0)
            {
                arr[i +ud][j] = arr[i][j];
                arr[i][j] = 0;
            }
            else if (arr[i + ud][j] == arr[i][j])
            {
                arr[i + ud][j] = 2 * arr[i][j];
                arr[i][j] = 0;
            }
        }
    }
}
void NumMoveleft(int arr[Row][Col], int row, int col, int lr)
{
    int i = 0;
    int j = 0;
    for (i = 0; i < row; i++)
    {
        for (j = col-1; j >0; j--)
        {
            if (arr[i][j-lr] == 0)
            {
                arr[i][j-lr] = arr[i][j];
                arr[i][j] = 0;
            }
            else if (arr[i][j-lr] == arr[i][j])
            {
                arr[i][j-lr] = 2 * arr[i][j];
                arr[i][j] = 0;
            }
        }
    }
}
void NumMoveright(int arr[Row][Col], int row, int col, int lr)
{
    int i = 0;
    int j = 0;
    for (i = 0; i < row; i++)
    {
        for (j = 0; j < col-1; j++)
        {
            if (arr[i][j + lr] == 0)
            {
                arr[i][j + lr] = arr[i][j];
                arr[i][j] = 0;
            }
            else if (arr[i][j + lr] == arr[i][j])
            {
                arr[i][j + lr] = 2 * arr[i][j];
                arr[i][j] = 0;
            }
        }
    }
}
void  PlayerGo(int arr[Row][Col], int row, int col)
{
    int  n = 0;
    char a=0;
    while (1)
    {
        if(getchar()=='\n')
            printf("\n\n\t\t Please input up, down, left and right to correspond( WSAD): >");
        scanf("%c", &a);
        if (a == 'A' || a == 'a')n = 3;
        else if (a == 'S' || a == 's')n = 2;
        else if (a == 'd' || a == 'D')n = 4;
        else if (a == 'w' || a == 'W')n = 1;
        switch (n)
        {
        case 1: NumMoveup(arr,row,col,1); break;
        case 2:NumMovedown(arr,row,col,1); break;
        case 3:NumMoveleft(arr, row, col, 1); break;
        case 4:NumMoveright(arr, row, col, 1); break;
        default:printf("Input error, please input again\n"); n = 0; break;
        }
        if(n!=0)break;
    }
}
char IsWin(int arr[Row][Col], int row, int col)
{
    int i = 0; int j = 0;
    int count=0;
    for (i = 0; i < row; i++)
        for (j = 0; j < col; j++)
        {
            if (arr[i][j] == 2048)
                return 'Y';
            else if (arr[i][j] == 0)
                count++;
        }
    if (count == 0) return 'N';
    return '0';
}
void ComputerGo(int arr[Row][Col], int row, int col)
{
    int r = 0;
    int c = 0;
    //Printf ("\ n \ n \ t \ tfrom the computer: > \ n");
    while (1)
    {
        r = rand() % row;
        c = rand() % col;
        if (arr[r][c] == 0)
        {
            arr[r][c] = 2; 
            Display(arr, row, col);
            break;
        }
    }
}

Boundary to display the start interface and exit the interface. Just start int main

#include"game.h"
int game() //Game
{
    int show[Row][Col];
    Init(show, Row, Col,0);
    char fin = '0';
    while (1)
    {
        if(fin='0')
            ComputerGo(show, Row, Col);
        fin = IsWin(show, Row, Col);
        if (fin != '0')break;
        PlayerGo(show, Row, Col);
        fin = IsWin(show, Row, Col);
        if (fin != '0')break;
    }
    if (IsWin(show, Row, Col) != 'N')
    {
        system("cls");
        system("color F2");
        Display(show, Row, Col);
        printf("\n\n\n\t\t Congratulations, you won*★*:.☆( ̄▽ ̄)/$:*.°★* . \n");
    }
    else 
    printf("\n\n\n\t\t I'm sorry you lost.\n");
    system("pause");
    exit(0);
}
int main()
{
    srand((unsigned int)time(NULL));
    int input = 0;
    do {
        menu();
        printf("Please enter your choice:>");
        scanf("%d", &input);
        if (input == 1)
        {
            game(); 
        }
        else 
            if (input == 0) printf("\t Quit game\n");
    } while (input != 0);
    return 0;
}

The implementation of this game is relatively simple. There is no game like snake. It is so huge and suitable for novices who only have c language foundation. Have fun

Topics: C Windows