


How to implement 2048 mini game in Linux
复制代码 代码如下:
#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 31static 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[?25l"); print_start(); ran_appear(); print_num(); print_score(); print_getchar(); printf("\33[?25h");}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有哪些版本
Linux的版本有:Deepin、UbuntuKylin、Manjaro、LinuxMint、Ubuntu等版本。其中Deepin是国内发展最好的Linux发行版之一;UbuntuKylin是基于Ubuntu的衍生发行版;Manjaro是基于Arch的Linux发行版;LinuxMint默认的Cinnamon桌面类似Windows XP简单易用;Ubuntu则是以桌面应用为主的Linux操作系统。
The above is the detailed content of How to implement 2048 mini game in Linux. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The key differences between CentOS and Ubuntu are: origin (CentOS originates from Red Hat, for enterprises; Ubuntu originates from Debian, for individuals), package management (CentOS uses yum, focusing on stability; Ubuntu uses apt, for high update frequency), support cycle (CentOS provides 10 years of support, Ubuntu provides 5 years of LTS support), community support (CentOS focuses on stability, Ubuntu provides a wide range of tutorials and documents), uses (CentOS is biased towards servers, Ubuntu is suitable for servers and desktops), other differences include installation simplicity (CentOS is thin)

CentOS installation steps: Download the ISO image and burn bootable media; boot and select the installation source; select the language and keyboard layout; configure the network; partition the hard disk; set the system clock; create the root user; select the software package; start the installation; restart and boot from the hard disk after the installation is completed.

CentOS will be shut down in 2024 because its upstream distribution, RHEL 8, has been shut down. This shutdown will affect the CentOS 8 system, preventing it from continuing to receive updates. Users should plan for migration, and recommended options include CentOS Stream, AlmaLinux, and Rocky Linux to keep the system safe and stable.

Docker uses Linux kernel features to provide an efficient and isolated application running environment. Its working principle is as follows: 1. The mirror is used as a read-only template, which contains everything you need to run the application; 2. The Union File System (UnionFS) stacks multiple file systems, only storing the differences, saving space and speeding up; 3. The daemon manages the mirrors and containers, and the client uses them for interaction; 4. Namespaces and cgroups implement container isolation and resource limitations; 5. Multiple network modes support container interconnection. Only by understanding these core concepts can you better utilize Docker.

Backup and Recovery Policy of GitLab under CentOS System In order to ensure data security and recoverability, GitLab on CentOS provides a variety of backup methods. This article will introduce several common backup methods, configuration parameters and recovery processes in detail to help you establish a complete GitLab backup and recovery strategy. 1. Manual backup Use the gitlab-rakegitlab:backup:create command to execute manual backup. This command backs up key information such as GitLab repository, database, users, user groups, keys, and permissions. The default backup file is stored in the /var/opt/gitlab/backups directory. You can modify /etc/gitlab

How to use Docker Desktop? Docker Desktop is a tool for running Docker containers on local machines. The steps to use include: 1. Install Docker Desktop; 2. Start Docker Desktop; 3. Create Docker image (using Dockerfile); 4. Build Docker image (using docker build); 5. Run Docker container (using docker run).

After CentOS is stopped, users can take the following measures to deal with it: Select a compatible distribution: such as AlmaLinux, Rocky Linux, and CentOS Stream. Migrate to commercial distributions: such as Red Hat Enterprise Linux, Oracle Linux. Upgrade to CentOS 9 Stream: Rolling distribution, providing the latest technology. Select other Linux distributions: such as Ubuntu, Debian. Evaluate other options such as containers, virtual machines, or cloud platforms.

CentOS hard disk mount is divided into the following steps: determine the hard disk device name (/dev/sdX); create a mount point (it is recommended to use /mnt/newdisk); execute the mount command (mount /dev/sdX1 /mnt/newdisk); edit the /etc/fstab file to add a permanent mount configuration; use the umount command to uninstall the device to ensure that no process uses the device.
