


复制代码 代码如下:
#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!

The core components of Linux include kernel, file system, shell, user and kernel space, device drivers, and performance optimization and best practices. 1) The kernel is the core of the system, managing hardware, memory and processes. 2) The file system organizes data and supports multiple types such as ext4, Btrfs and XFS. 3) Shell is the command center for users to interact with the system and supports scripting. 4) Separate user space from kernel space to ensure system stability. 5) The device driver connects the hardware to the operating system. 6) Performance optimization includes tuning system configuration and following best practices.

The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

Linux maintenance mode can be entered through the GRUB menu. The specific steps are: 1) Select the kernel in the GRUB menu and press 'e' to edit, 2) Add 'single' or '1' at the end of the 'linux' line, 3) Press Ctrl X to start. Maintenance mode provides a secure environment for tasks such as system repair, password reset and system upgrade.

The steps to enter Linux recovery mode are: 1. Restart the system and press the specific key to enter the GRUB menu; 2. Select the option with (recoverymode); 3. Select the operation in the recovery mode menu, such as fsck or root. Recovery mode allows you to start the system in single-user mode, perform file system checks and repairs, edit configuration files, and other operations to help solve system problems.

The core components of Linux include the kernel, file system, shell and common tools. 1. The kernel manages hardware resources and provides basic services. 2. The file system organizes and stores data. 3. Shell is the interface for users to interact with the system. 4. Common tools help complete daily tasks.

The basic structure of Linux includes the kernel, file system, and shell. 1) Kernel management hardware resources and use uname-r to view the version. 2) The EXT4 file system supports large files and logs and is created using mkfs.ext4. 3) Shell provides command line interaction such as Bash, and lists files using ls-l.

The key steps in Linux system management and maintenance include: 1) Master the basic knowledge, such as file system structure and user management; 2) Carry out system monitoring and resource management, use top, htop and other tools; 3) Use system logs to troubleshoot, use journalctl and other tools; 4) Write automated scripts and task scheduling, use cron tools; 5) implement security management and protection, configure firewalls through iptables; 6) Carry out performance optimization and best practices, adjust kernel parameters and develop good habits.

Linux maintenance mode is entered by adding init=/bin/bash or single parameters at startup. 1. Enter maintenance mode: Edit the GRUB menu and add startup parameters. 2. Remount the file system to read and write mode: mount-oremount,rw/. 3. Repair the file system: Use the fsck command, such as fsck/dev/sda1. 4. Back up the data and operate with caution to avoid data loss.


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 English version
Recommended: Win version, supports code prompts!

SublimeText3 Chinese version
Chinese version, very easy to use

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software