废话少说,直接奉上代码:

main.c

复制代码 代码如下:
#include"2048.h"
int main()
{
    start_game();
    return 0;
}

2048.h

复制代码 代码如下:
#ifndef _2048_H_
#define _2048_H_
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<termios.h>
//#include<unstd.h>
//#include<time/sys.h>
#define LINE 21
#define ROW   22
#define ARR_L 4
#define ARR_R 4
#define NUM_COLOR 32
#define BACK 49
#define BOLD 31
static int line_location=0;
static int row_location=0;
static int arr[4][4]={0};
static char tmp[5]="\0";
static int end_flag=0;
static int score=0;
static int print_appear_flag=0;
static char start_back0[LINE][ROW]={
    "@@@@@@@@@@@@@@@@@@@@@",
    "@    @    @    @    @",
    "@    @    @    @    @",
    "@    @    @    @    @",
    "@@@@@@@@@@@@@@@@@@@@@",
    "@    @    @    @    @",
    "@    @    @    @    @",
    "@    @    @    @    @",
    "@@@@@@@@@@@@@@@@@@@@@",
    "@    @    @    @    @",
    "@    @    @    @    @",
    "@    @    @    @    @",
    "@@@@@@@@@@@@@@@@@@@@@",
    "@    @    @    @    @",
    "@    @    @    @    @",
    "@    @    @    @    @",
    "@@@@@@@@@@@@@@@@@@@@@",
    "@                   @",
    "@ score:            @",
    "@                   @",
    "@@@@@@@@@@@@@@@@@@@@@"
};
int print_start();
char * itoc_2048(int data);
int print_num();
int  mov_left();
int swap_if0l();
int swap();
int put_to(int line, int row);
#endif

2048.c

复制代码 代码如下:
#include"2048.h"
int start_game()
{
    system("clear");
    printf("\33[");
    print_start();
    ran_appear();
    print_num();
    print_score();
    print_getchar();
    printf("\33[");
}
int print_getchar()
{
    struct termios old,new;
    int ch;
    tcgetattr(0,&old);
    tcgetattr(0,&new);
    new.c_lflag = new.c_lflag &~(ICANON |ECHO);
    new.c_cc[VTIME]=0;
    new.c_cc[VMIN]=1;
    tcsetattr(0,TCSANOW,&new);
    while(1)
    {
        if(end_flag==1)
            break;
        ch=getchar();
        if(ch=='\33')
        {
            ch=getchar();
            if(ch=='[')
            {
                ch=getchar();
                switch(ch)
                {
                    case 'A':
                        mov_up();
                        is_full();
                        break;
                    case 'B':
                        mov_down();
                        is_full();
                        break;
                    case 'C':
                        mov_right();
                        is_full();
                        break;
                    case 'D':
                        mov_left();
                        is_full();
                        break;
                    default:
                        break;
                }
            }
        }
        if(ch=='q')
            break;
        fflush(NULL);
    }
tcsetattr(0,TCSANOW,&old);
}
int print_start()
{  
    int i,j;
    for(i=0;i<LINE;i++)
    {
        for(j=0;j<ROW;j++)
        {
            if(start_back0[i][j]=='@')
            {
                printf("\33[%dm",BACK);
                printf("%c",start_back0[i][j]);
                printf("\33[0m");
            }
            else
                if(start_back0[i][j]!=' ')
                {
                    printf("\33[%dm",BOLD);
                    printf("%c",start_back0[i][j]);
                    printf("\33[0m");
                }
                else
                {  
                    printf("%c",start_back0[i][j]);
                }
        }
    printf("\n");
    }
}
char *itoc_2048(int data)
{
    int x=0;
    int i=4;
    while(i--)
    {
        tmp[i]=data%10+'0';
        data=data/10;
    }
    return tmp;
}
int is_full()
{
    int i,j;
    int count=0;
    for(i=0;i<ARR_L;i++)
        for(j=0;j<ARR_R;j++)
        {
            if(arr[i][j]==0)
                count++;
        }
    if(count==0)
    {
        for(i=0;i<ARR_L;i++)
            for(j=0;j<ARR_R-1;j++)
            {
            if(arr[i][j]==arr[i][j+1])
                return 0;
            if(arr[j][i]==arr[j+1][i])
                return 0;
            }
        end_flag=1;
    }
    return 1;
}
int put_to(int line, int row)
{
    int x,y;
    int i=0;
    char *p=NULL;
    p=itoc_2048(arr[line][row]);
    printf("\33[%d;%dH",3+line*4,2+row*5);
    printf("    ");
    printf("\33[%d;%dH",3+line*4,2+row*5);
    if(arr[line][row]!=0)
        for(i=0;i<4;i++)
        {
            if(p[i]=='0'&&i<1)
                printf(" ");
            else
            if(p[i]!='0')
            {
                printf("\33[%dm",NUM_COLOR);
                printf("%c",p[i]);
                printf("\33[0m");
            }
        }
    else
        if(arr[line][row]==0)
        printf("    ");
}
int print_num()
{
    int i,j;
    for(i=0;i<4;i++)
        for(j=0;j<4;j++)
    put_to(i,j);
}
print_score()
{
    int x,y;
    printf("\33[19;9H");
    printf("%d",score);
}
int ran_appear()
{
    int line,row;
    int i=0;
    int j=0;
    int x,y;
    int arr1[16][2]={0};
    if(print_appear_flag==1)
        return 0;
    for(x=0;x<4;x++)
        for(y=0;y<4;y++)
        {
            if(arr[x][y]==0)
            {
                arr1[i][0]=x;
                arr1[i][1]=y;
                i++;
            }
        }
    srand(time(NULL));
    j=rand()%i;
    if(rand()%2==0)
      {
        arr[arr1[j][0]][arr1[j][1]]=4;
        //arr[arr1[j][0]][arr1[j][1]]=2;
      }
    else
        arr[arr1[j][0]][arr1[j][1]]=2;
}
int mov_left()
{
    int count=0;
    count=mov_l()+count;
    count=sum_2048_l()+count;
    if(count==-2)
        print_appear_flag=1;
    mov_l();
    ran_appear();
    print_num();
    return 0;
}
int mov_right()
{
    int count=0;
    count=mov_r()+count;
    count=sum_2048_r()+count;
    if(count==-2)
        print_appear_flag=1;
    mov_r();
    ran_appear();
    print_num();
    return 0;
}
int mov_up()
{
    int count=0;
    count=mov_u()+count;
    count=sum_2048_u()+count;
    if(count==-2)
        print_appear_flag=1;
    mov_u();
    ran_appear();
    print_num();
    return 0;
}
int mov_down()
{
    int count=0;
    count=mov_d()+count;
    count=sum_2048_d()+count;
    if(count==-2)
        print_appear_flag=1;
    mov_d();
    ran_appear();
    print_num();
    return 0;
}
int swap(int *a,int *b)
{
    int tmp;
    tmp=*a;
    *a=*b;
    *b=tmp;
}
int mov_l()
{
    int line,row;
    int i=3;
    int count=0;
    while(i--)
    {
        for(line=0;line<4;line++)
         for(row=0;row<3;row++)
        {
            if(arr[line][row]==0&&arr[line][row+1]!=0)
            {  
                swap(&arr[line][row],&arr[line][row+1]);
                count++;
                print_appear_flag=0;
            }
        }
    }
    if(count==0)
        return -1;
    return 0;
}
int sum_2048_l()
{
    int line,row;
    int count=0;
    for(row=1;row<4;row++)
        for(line=0;line<4;line++)
        {
            if(arr[line][row]!=0&&arr[line][row-1]==arr[line][row])
            {
                arr[line][row-1]=arr[line][row]+arr[line][row-1];
                arr[line][row]=0;
                score=score+arr[line][row-1];
                print_score();
                count++;
                print_appear_flag=0;
            }
        }
    if(count==0)
        return -1;
return 0;
}
int mov_r()
{
    int line,row;
    int i=3;
    int count=0;
    while(i--)
    {
        for(line=0;line<4;line++)
         for(row=0;row<3;row++)
        {
            if(arr[line][row]!=0&&arr[line][row+1]==0)
            {  
                swap(&arr[line][row],&arr[line][row+1]);
                count++;
                print_appear_flag=0;
            }
        }
    }
    if(count==0)
        return -1;
    return 0;
}
int sum_2048_r()
{
    int line,row;
    int count=0;
    for(row=2;row>=0;row--)
        for(line=0;line<4;line++)
        {
            if(arr[line][row]!=0&&arr[line][row+1]==arr[line][row])
            {
                arr[line][row+1]=arr[line][row]+arr[line][row+1];
                arr[line][row]=0;
                score=score+arr[line][row+1];
                print_score();
                count++;
                print_appear_flag=0;
            }
        }
    if(count==0)
        return -1;
    return 0;
}
int mov_u()
{  
    int line,row;
    int i=3;
    int count=0;
    while(i--)
    {
        for(line=0;line<3;line++)
         for(row=0;row<4;row++)
        {
            if(arr[line][row]==0&&arr[line+1][row]!=0)
            {  
                swap(&arr[line][row],&arr[line+1][row]);
                count++;
                print_appear_flag=0;
            }
        }
    }
    if(count==0)
        return -1;
    return 0;
}
int sum_2048_u()
{
    int line,row;
    int count=0;
        for(line=1;line<4;line++)
        for(row=0;row<4;row++)
        {
            if(arr[line][row]!=0&&arr[line-1][row]==arr[line][row])
            {
                arr[line-1][row]=arr[line][row]+arr[line-1][row];
                arr[line][row]=0;
                score=score+arr[line-1][row];
                print_score();
                count++;
                print_appear_flag=0;
            }
        }
        if(count==0)
            return -1;
        return 0;
}
int mov_d()
{
    int line,row;
    int i=3;
    int count=0;
    while(i--)
    {
        for(line=0;line<3;line++)
         for(row=0;row<4;row++)
        {
            if(arr[line][row]!=0&&arr[line+1][row]==0)
            {  
                swap(&arr[line][row],&arr[line+1][row]);
                count++;
                print_appear_flag=0;
            }
        }
    }
    if(count==0)
        return -1;
    return 0;
}
int sum_2048_d()
{
    int line,row;
    int count=0;
        for(line=2;line>=0;line--)
        for(row=0;row<4;row++)
        {
            if(arr[line][row]!=0&&arr[line+1][row]==arr[line][row])
            {
                arr[line+1][row]=arr[line][row]+arr[line+1][row];
                arr[line][row]=0;
                score=score+arr[line+1][row];
                print_score();
                count++;
                print_appear_flag=0;
            }
        }
        if(count==0)
            return -1;
        return 0;
}

以上就是本文分享的全部代码了,希望对大家学习Linux控制台能够有所帮助。

广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!

RTX 5090要首发 性能要翻倍!三星展示GDDR7显存

三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。

首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。

据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。