粤嵌实训笔记二

2023-05-16

目录

    • 20230227-20230303(第二周)
      • main.c
      • lcd.c
      • lcd.h
      • bmp.c
      • bmp.h
      • game.c
      • game.h

20230227-20230303(第二周)

1、在Linux下,使用 gcc 来编译
gcc xxx.c --> 默认生成的可执行文件 名为 a.out
gcc xxx.c -o xx --> 生成指定名字的可执行文件 xx

2、交叉编译 :在一个环境下编译生成 适用于 另一个环境下的可执行文件
3、为交叉编译工具创建软连接
sudo ln -s /usr/lib/x86_64-linux-gnu/libmpfr.so.6 /usr/lib/x86_64-linux-gnu/libmpfr.so.4
4、使用交叉编译工具链 arm-linux-gcc main.c -o k
5、Linux的常用的命令
cd、 ls、pwd、touch、rm、 Ctrl l 、鼠标按下滚轮 等
6、下载程序 -串口终端软件 SecureCRT
7、LCD显示屏
开发板的参数:
芯片:三星S5P6818
处理器:ARM Cortex A53 64bits
OS:Linux
LCD屏幕:800*480
8、RGBA
每种颜色分量占1个字节 8bits 0~255 0x00~0xFF
红色 Red Green Blue
0xFF 0x00 0x00 ----> 0xFF0000
但是 LCD屏幕中的每一个像素点是占4个字节 --》 A R G B
A:透明度
9、操作文件
(0)打开int open(const char *pathname, int flags);
(1)写入 ssize_t write(int fd, const void *buf, size_t count);

		#include <sys/types.h>
		#include <sys/stat.h>
		#include <fcntl.h>

(2)读 read() ssize_t read(int fd, void *buf, size_t count);

(3)重定位光标 off_t lseek(int fd, off_t offset, int whence);
10、

				(0,0)---------------------------------------------------------------------->x
				|
				|
				|			(x,y)
				|
				|
				y

11、 内存映射
#include <sys/mman.h>
void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset);

任务: 
			在开发板上显示一张纯色的图片,并检查有坏的像素点 
			
				int show_a_pure_color( int c )
				{
					//1.打开屏幕 /dev/fb0 
					int fd = open( "/dev/fb0", O_RDWR );
					if( fd == -1 )
					{
						perror("open lcd error :");	//打印出错信息
					}
					
					//准备颜色值数据
					int color[800*480];
					int i;
					for( i=0; i<800*480; i++ )
					{
						color[i] = c;
					}
					
					//2.写入
					int re = write( fd, color, 800*480*4 );
					if( re < 0 )
					{
						perror("write error :");
					}
					
					//3.关闭文件
					close( fd );
				}
				
				int main()
				{
					show_a_pure_color( 0xFF0000 );
					
				}
	练习:
		1)画一个圆、矩形 
		
			//圆
			void display_Circle(int x0, int y0, int r, int color )
			{
				int x, y;
				for( x=0; x<800; x++ )
				{
					for( y=0; y<480; y++ )
					{
						//满足圆方程 
						if( (x-x0)*(x-x0) + (y-y0)*(y-y0) <= r*r )
						{
							display_point( x, y, color );
						}
					}
				}
			}

			//矩形
			void display_Rectangle( int x0, int y0, int w, int h, int color )
			{
				int x, y;
				for( x=0; x<800; x++ )
				{
					for( y=0; y<480; y++ )
					{
						//矩形 --》 四条直线方程 
						if( x>=x0 && x<=x0+w && y>=y0 && y<=y0+h  )
						{
							display_point( x, y, color );
						}
					}
				}
			}


		作业: 
			画一个爱心、四叶草、风车... 任意一个图像  (画五角星 是加分项)

12、BMP图片

任务:
		在开发板上显示一张bmp图片  
		
			bmp.c   / bmp.h    
			
				int show_bmp(int x0, int y0, char *pathname)
				{
					//1.打开图片文件
					int fd = open( pathname, O_RDWR );
					if( fd == -1)
					{
						perror("open bmp error :");
						return -1;
					}
					
					//2.解析图片 
						取像素点的数据 
						
					//3.显示 
						//获取 宽、高、色深 
						//获取bmp图片的像素点的数据
						
					printf("w = %d, h = %d, d = %d, laizi = %d\n", width, height, depth, laizi );
						
					//4.关闭文件 
				}


				//调用
				show_bmp( 0, 0, "01.bmp" );

13、触摸屏
“输入事件”:Linux下 触摸事件、按键事件、鼠标事件 …
这一类“输入设备”在Linux下对应的文件名为:
/dev/input/eventx(x=0,1,2,3…)
在开发板上 触摸屏对应的设备文件名为
/dev/input/event0
Linux用了标准的事件结构体来描述一个标准的事件
/usr/include/linux/input.h

struct input_event 
					{
						struct timeval time;	//事件发生的时间 
						
						__u16 type;		//事件的类型 
								#define EV_KEY  0x01	按键事件或键盘事件
								#define EV_REL  0x02	相对事件或鼠标事件
								#define EV_ABS  0x03	绝对事件或触摸事件
						
						__u16 code;		//事件的编码,根据type的不同 而有不同的含义 
								当 type == EV_ABS, code表示坐标轴 
											--> code == ABS_X  //x轴 	#define ABS_X  0x00
												code == ABS_Y  //y轴	#define ABS_Y  0x01
												code == ABS_PRESSURE  //触摸屏压力事件
													#define ABS_PRESSURE  0x18type == EV_KEY, code表示键值 
													KEY_A 
													KEY_B 
													...
												code == BTN_TOUCH   //把整个屏幕当作成一个按键来使用
													#define BTN_TOUCH  0x14a
						
						__s32 value;	//事件的值,根据type的不同 而有不同的含义
								当 type == EV_ABS, value表示坐标值 
											code == ABS_X , value表示x轴坐标值
											code == ABS_Y , value表示y轴坐标值
											code == ABS_PRESSURE , value表示压力值 
													>0  表示触摸屏按下
													==0 表示触摸屏弹起 
													
								当 type == EV_KEY, value表示按键的状态 
												1	表示按键按下
												0	表示按键松开
						
					};
任务: 
			1)获取手指触摸屏幕的坐标,并测出触摸屏的坐标范围 

				touch.c   / touch.h   
				
					#include <linux/input.h>
				
					int get_touch()
					{
						
					}


			2)判定手指滑动的方向, 并通过返回值返回  
			
					#define UP 1
					#define DOWN 2
					#define LEFT 3
					#define RIGHT 4

					
					int get_direction()
					{
						
					}

main.c

#include "lcd.h"
#include "stdio.h"
#include "math.h"
#include "bmp.h"
#include "touch.h"
#include "game.h"
#include <unistd.h>
int main()
{
	//1.打开屏幕 
	lcd_init();

	//2.操作 
	show_a_pure_color();//满屏显示纯色

	//display_Circle(600,300,100);//画圆
	//display_Rectangle( 100,200,200,100 );//画矩形

	five_Pointed(100,80,50,0xffffff,30);//打印五角星
	five_Pointed(200,80,30,0xffffff,45);
	five_Pointed(300,80,40,0xffffff,60);
	five_Pointed(400,80,60,0xffffff,90);
	//national_flag();//打印红旗

    //show_bmp(200, 100, "2331.bmp");//显示一张图片

	//refresh();//刷新

	//position();/判断方向
    
	/*while(1)
	{
		//get_touch();//获取坐标
		position();
		get_direction();//判断滑动方向
	}*/

	my_2048_game();
	
	//3.关闭屏幕
	lcd_close();
}



/*int dr=get_direction();
if(dr==1)
{
	printf("up\n");
}
else if(dr==2)
{
	printf("down\n");
}
else if(dr==3)
{
	printf("left\n");
}
else if(dr==4)
{
	printf("right\n");
}*/


lcd.c

#include "lcd.h"
#include "math.h"

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>


int fd ;	//屏幕文件的文件描述符
int *plcd = NULL ;	//帧缓冲的首地址 

//lcd屏幕的初始化
void lcd_init()
{
	//1.打开屏幕 /dev/fb0 
	fd = open( "/dev/fb0", O_RDWR );
	if( fd == -1 )
	{
		perror("open lcd error :");	//打印出错信息
	}
	//内存映射
	plcd = mmap(NULL,800*480*4,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0 );
	if(MAP_FAILED == plcd)
	{
		perror("mmap error :");	//打印出错信息
	}
	
}

//关闭屏幕 
void lcd_close()
{
	//解除映射 
	munmap(plcd,800*480*4);
	//关闭屏幕 
	//3.关闭文件
	close( fd );
}


//画点 
void display_point( int color, int y, int x )
{
	if( x>=0 && x<800 && y>=0 && y<480 )
	{
		*(plcd + 800*y + x) = color;
	}
}



//显示一张纯色的图片
void show_a_pure_color(  )
{
	//准备颜色值数据
	int i,j;
	for( i=0; i<800; i++ )
	{
		//color[i] = c;
		for(j=0;j<480;j++)
		{
			display_point(0x1E90FF,j,i);
		}	
	}

	//2.写入
	/*int re = write( fd, color, 800*480*4 );
	if( re < 0 )
	{
		perror("write error :");
	}*/

	
}



//1)画一个圆、矩形 

//圆
void display_Circle(int i0, int j0, int r)
{
	int i,j;
	for( i=0; i<800; i++ )
	{
		//color[i] = c;
		for(j=0;j<480;j++)
		{
			if((i-i0)*(i-i0)+(j-j0)*(j-j0)<=r*r)
			{
				//display_point(i,j,0xCDCD00);
				display_point(0xCDCD00,j,i);
			}
			else
			{
				//display_point(i,j,0xff0000);
				display_point(0xff0000,j,i);
			}
		}	
	}

}

//矩形
void display_Rectangle( int x0, int y0, int w, int h )
{
	int i,j;
	for( i=0; i<800; i++ )
	{
		//color[i] = c;
		for(j=0;j<480;j++)
		{
			if((i>x0)&&(i<x0+w)&&(j>y0)&&(j<y0+h))
			{
				//display_point(i,j,0x0000ff);
				display_point(0xff0000,j,i);
			}
		}	
	}
}

/* 
triangle :画三角形
参数:
    x1 :三角形第一个顶点的X坐标
    y1 :三角形第一个顶点的y坐标
    x2 :三角形第二个顶点的X坐标
    y2 :三角形第二个顶点的y坐标
    x3 :三角形第三个顶点的X坐标
    y3 :三角形第三个顶点的y坐标
    col:三角形的颜色
 */
void triangle(int x1,int y1,int x2,int y2,int x3,int y3,int col)
{
    //flag:代表本三角形在本直线的左边(0)还是右边(1)(左边右边是抽象概念)
    int i,j,flag1=0,flag2=0,flag3=0;
    float A1,B1,C1,A2,B2,C2,A3,B3,C3;
    //1号点与2号点的直线方程的A,B,C
    A1 = y2 - y1;
    B1 = x1 - x2;
    C1 = x2*y1 - x1*y2;
    //2号点与3号点的直线方程的A,B,C
    A2 = y2 - y3;
    B2 = x3 - x2;
    C2 = x2*y3 - x3*y2;
    //1号点与3号点的直线方程的A,B,C
    A3 = y3 - y1;
    B3 = x1 - x3;
    C3 = x3*y1 - x1*y3;
 
    //判断第三个点与直线的相对位置
    if(x3*A1+y3*B1+C1 > 0)  flag1=1;
    if(x1*A2+y1*B2+C2 > 0)  flag2=1;
    if(x2*A3+y2*B3+C3 > 0)  flag3=1;
 
    for(i=0;i<480;i++){
        for(j=0;j<800;j++){
            if(flag1 == 1){
                if(flag2 == 1){
                    if(j*A1+i*B1+C1 > 0 && j*A2+i*B2+C2 > 0 && j*A3+i*B3+C3 < 0)
                    {
                        display_point(col,j,i);
                    }
                }
                else{
                    if(flag3 == 1){
                        if(j*A1+i*B1+C1 > 0 && j*A2+i*B2+C2 < 0 && j*A3+i*B3+C3 > 0)
                        {
                            display_point(col,j,i);
                        }
                    }
                    else{
                        if(j*A1+i*B1+C1 > 0 && j*A2+i*B2+C2 < 0 && j*A3+i*B3+C3 < 0)
                        {
                            display_point(col,j,i);
                        }
                    }
                }
            }
            else{
                if(flag2 == 0){
                    if(j*A1+i*B1+C1 < 0 && j*A2+i*B2+C2 < 0 && j*A3+i*B3+C3 > 0)
                    {
                        display_point(col,j,i);
                    }
                }
                else{
                    if(flag3 == 1){
                        if(j*A1+i*B1+C1 < 0 && j*A2+i*B2+C2 > 0 && j*A3+i*B3+C3 > 0)
                        {
                            display_point(col,j,i);
                        }
                    }
                    else{
                        if(j*A1+i*B1+C1 < 0 && j*A2+i*B2+C2 > 0 && j*A3+i*B3+C3 < 0)
                        {
                            display_point(col,j,i);
                        }
                    }
                }
            }
        }
    }
}



/* 
five_Pointed :画五角星
参数:
    x :五角星的在正中心点的X坐标
    y :五角星的在正中心点的Y坐标
    R :中心点到外顶点的长度
    col:五角星的颜色
    yDegree :五角星的倾斜程度
 */
void five_Pointed(int x,int y,int R,unsigned int col,int yDegree)
{
    struct Vertex
    {
        int x;
        int y;
    };
    struct Vertex RVertex[5], rVertex[5];              //外围5个顶点的坐标与内部五个顶点的坐标
    
    double rad = 3.1415926 / 180;                    //每度的弧度值
    double r = R * sin(18 * rad) / cos(36 * rad);    //五角星短轴的长度
    for (int k = 0; k < 5; k++)                      //求取坐标
    {
       RVertex[k].x = (int)(x - (R * cos((90 + k * 72 + yDegree) *rad)));
       RVertex[k].y = (int)(y - (R * sin((90 + k * 72 + yDegree) * rad)));
       rVertex[k].x = (int)(x - (r * cos((90 + 36 + k * 72 + yDegree) *rad)));
       rVertex[k].y = (int)(y - (r * sin((90 + 36 + k * 72 + yDegree) * rad)));
    }
    triangle(RVertex[1].x,RVertex[1].y,RVertex[3].x,RVertex[3].y,rVertex[4].x,rVertex[4].y,col);
    triangle(RVertex[2].x,RVertex[2].y,RVertex[4].x,RVertex[4].y,rVertex[0].x,rVertex[0].y,col);
    triangle(RVertex[3].x,RVertex[3].y,RVertex[0].x,RVertex[0].y,rVertex[1].x,rVertex[1].y,col);
}

/* 
national_flag :画国旗
 */
void national_flag()
{
    int i=0,j=0;
    for(i = 0;i < 480;i++)
    {
        for(j = 0;j < 800; j++)
        {
           display_point(0xff0000,i,j);
        }
    }
    five_Pointed(110,150,70,0xffff00,0);
    five_Pointed(200,60,40,0xffff00,20);
    five_Pointed(270,130,40,0xffff00,40);
    five_Pointed(270,230,40,0xffff00,0);
    five_Pointed(200,290,40,0xffff00,40);
}


lcd.h


#ifndef __LCD_H_
#define __LCD_H_


void lcd_init(void);

void lcd_close();

void display_point( int x, int y, int color );

void show_a_pure_color();

void display_Circle(int i0, int j0, int r);

void display_Rectangle( int i, int j, int w, int h );

void triangle(int x1,int y1,int x2,int y2,int x3,int y3,int col);

void five_Pointed(int x,int y,int R,unsigned int col,int yDegree);

void national_flag();


#endif



bmp.c

#include "bmp.h"
#include "lcd.h"
#include "stdlib.h"

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>


int show_bmp(int x0, int y0, char *pathname)
{
	//1.打开图片文件 
    int fd = open(pathname, O_RDWR);
	if (-1 == fd)
	{
		perror("open bmp error");
	}
	//2.解析图片 
		//获取 宽、高、色深 
		//获取像素点的数据 
	
	int width = 0;
	lseek( fd, 0x12, SEEK_SET );
	read( fd, &width, 4 );
	int height = 0;
	lseek(  fd, 0x16, SEEK_SET);
	read(fd, &height, 4 );
	short depth = 0;
	lseek(  fd, 0x1C, SEEK_SET);
	read(fd, &depth, 2 );
	int line_size = 0;	//保存一行实际的字节数 
	int laizi = 0;		//保存填充的字节数 
	laizi = 4 - (abs(width)*(depth/8))%4 ; 
	if( laizi == 4 )
	{
		laizi = 0;
	}
	line_size = abs(width)*(depth/8) + laizi; 
	unsigned char buf[ abs(height) * line_size ];
	lseek( fd, 0x36, SEEK_SET );
	read( fd, buf, abs(height) * line_size );
	//color = (a<<24) | (r<<16) | (g<<8) | b;
	//3.显示 
	unsigned char b,g,r,a=0;
	int color;	
	int num = 0;	//下标
	int x,y;
	for(y=0; y<abs(height); y++ )
	{
		for( x=0; x<abs(width); x++ )
		{
			b = buf[num++];
			g = buf[num++];
			r = buf[num++];
			if( depth == 32 )
			{
				a = buf[num++];
			}
			color = (a<<24) | (r<<16) | (g<<8) | b;
			display_point( color, height>0 ? abs(height)-1-y+y0 : y+y0, width>0 ? x+x0 : abs(width)-1-x+x0);

		}
	num = num + laizi;	//跳过无效数据 
	}
	printf(" w = %d , h = %d , d = %d , laizi = %d\n ",width,height,depth,laizi);
	//4.关闭文件
	close( fd );
}

bmp.h


#ifndef __BMP_H_
#define __BMP_H_


int show_bmp(int x0, int y0, char *pathname);




#endif

game.c

#include "linux/input.h"
#include "touch.h"
#include "bmp.h"
#include "stdlib.h"
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
#include <time.h>
#include "game.h"


//(1)显示游戏界面 

//unsigned int game[4][4] = {0,2,4,8,16,32,64,128,256,512,1024,2048};
unsigned int game[4][4] = {0};

//刷新游戏界面
void refresh()
{
	int i,j;
	for( i=0; i<4; i++ )
	{
		for( j=0; j<4; j++ )
		{
			// 0 --> 0.bmp 
			printf("%d/t",game[i][j]);
			char buf[64] = {0};
			snprintf(buf , 64 , "%d%s",game[i][j],".bmp");
			show_bmp(160+120*j, 120*i , buf);
			
		}
		
	}
	putchar('\n');
}


/*
 

int x= time(0);//设置时间种子
srand(x);

生成随机的2或4
	rand()%2   -->余数只能为0或2 
	rand()%2+1  --》1或2
	(rand()%2+1)*2  --》2或4

	int rand_value=(rand()%2+1)*2;

在随机的位置上
	int i=rand()%4;//余数的范围0~3
	int j=rand()%4;

	
*/

//在随机的位置上生成2或4
void position()
{
	//1.判断还有没有空白位置,让我们去生成随机数 --> 遍历数组
	int flag = 0;
	int i,j;
	for( i=0; i<4; i++ )
	{
		for( j=0; j<4; j++ )
		{
			if( game[i][j] == 0 )
			{
				flag = 1;
				break;
			}
		}
		if( flag == 1 )
		{
			break;
		}
	}

	//2.设置时间种子
	int x = time(0);
	srand(x);

	//3.生成随机的2或4
	int rand_value = ( rand()%2 + 1 ) * 2;

	//4.生成随机的位置 
	while( flag )
	{
		int i = rand() % 4;	//余数的范围是 0~3 
		int j = rand() % 4;
		
		if( game[i][j] == 0 )
		{
			game[i][j] = rand_value;
			break;
		}
	}

	//5.刷新游戏界面
	refresh();

}


int my_2048_game()
{
	//初始化游戏界面 
	//背景 
	//刷新游戏界面
	refresh();
		
	//在随机的位置上生成2或4
	position();
	position();


	//开始游戏
	while( 1 )
	{
		//根据用户的滑动方向,进行游戏数据的合并和移动
		int fx = get_direction();
		
		//判断所有的方向是否能够移动 
		int flag = judge_move( fx );
		
		if( flag == 1 )		//游戏继续
		{
			//进行数据的合并和移动
			move(fx);
			
			//判断游戏是否成功 --> 遍历数组,是否出现2048
			int re = is_win();
			if( re == 2 )
			{
				show_bmp(0, 0, "v.bmp");//游戏成功 
				break;
			}
			else
			{
				//游戏继续,再次生成随机的2或4
				position();
			}
			

		}
		else if( flag == 3 )	//失败  
		{
			show_bmp(0, 0, "l.bmp");//显示一张图片//游戏失败   (失败界面、选择结束或者重玩 ... )
		}
		else if( flag == 4 )	//无效滑动,单个方向不能移动,重新滑一次
		{
			continue;
		}
		
	}
		
}



int judge_move( int fx )
{
	//1.判断所有方向
	int count = 0;
	
	count += judge_up();
	count += judge_down();
	count += judge_left();
	count += judge_right();
	
	if( count == 0 )
	{
		//4个方向都不能移动
		return 3;	//游戏失败
	}
	
	//2.判断单个方向fx 
	count = 0;
	switch( fx )
	{
		case 1: count = judge_up();break;	//上 
		case 2: count = judge_down();break;
		case 3: count = judge_left();break;
		case 4: count = judge_right();break;
		default: break;
	}
	
	if( count == 0 )
	{
		return 4;	//单个方向是不能移动的,无效滑动
	}
	else
	{
		return 1;	//游戏继续,该方向是可以移动的
	}
}


	//判断是否左移,能够移动返回1,不能移动返回0
int judge_left()
{
	int i,j;
	for( i=0; i<4; i++ )
	{
		for( j=0; j<3; j++ )
		{
			if( game[i][j]==game[i][j+1] && game[i][j]!=0 )
			{
				return 1;	//能够移动
			}
			if( game[i][j]!=game[i][j+1] && game[i][j]==0 )
			{
				return 1;	//能够移动
			}
		}
	}
	return 0;	//不能移动
}

//判断是否右移,能够移动返回1,不能移动返回0
int judge_right()
{
	int i,j;
	for( i=0; i<4; i++ )
	{
		for( j=3; j>0; j-- )
		{
			if( game[i][j]==game[i][j-1] && game[i][j]!=0 )
			{
				return 1;	//能够移动
			}
			if( game[i][j]!=game[i][j-1] && game[i][j]==0 )
			{
				return 1;	//能够移动
			}
		}
	}
	return 0;	//不能移动
}

//判断是否上移,能够移动返回1,不能移动返回0
int judge_up()
{
	int i,j;
	for( j=0; j<4; j++ )
	{
		for( i=0; i<3; i++ )
		{
			if( game[i][j]==game[i+1][j] && game[i][j]!=0 )
			{
				return 1;	//能够移动
			}
			if( game[i][j]!=game[i+1][j] && game[i][j]==0 )
			{
				return 1;	//能够移动
			}
		}
	}
	return 0;	//不能移动
}

//判断是否下移,能够移动返回1,不能移动返回0
int judge_down()
{
	int i,j;
	for( j=0; j<4; j++ )
	{
		for( i=3; i>0; i-- )
		{
			if( game[i][j]==game[i-1][j] && game[i][j]!=0 )
			{
				return 1;	//能够移动
			}
			if( game[i][j]!=game[i-1][j] && game[i][j]==0 )
			{
				return 1;	//能够移动
			}
		}
	}
	return 0;	//不能移动
}

	
//(4)根据用户的滑动方向,进行数据的合并和移动

void move( int fx )
{
	switch( fx )
	{
		case 1: move_up(); break;
		case 2: move_down(); break;
		case 3: move_left(); break;
		case 4: move_right(); break;
		default: break;
	}
}

//左移 ,先合并 后移动 
void move_left()
{
	//1.先合并
	int i,j,k;
	for( i=0; i<4; i++ )
	{
		for( j=0; j<3; j++ )
		{
			if( game[i][j] != 0 )
			{
				for( k=j+1; k<4; k++ )	//k表示它的下一个
				{
					if( game[i][j] != game[i][k] && game[i][k]!=0 )
					{
						break;
					}
					//else if( game[i][j] != game[i][k] && game[i][k]==0 )
					//{
					//	continue;	//中间隔0的情况,继续比较后面的数 
					//}
					else if( game[i][j] == game[i][k] ) 
					{
						game[i][j] = game[i][j]*2;
						game[i][k] = 0;
						break;
					}
				}
			}
		}
	}

	//2.后移动,前一个为0,后一个不为0即可移动
	for( i=0; i<4; i++ )
	{
		for( j=0; j<3; j++ )
		{
			if( game[i][j] == 0 )
			{
				for( k=j+1; k<4; k++ )
				{
					if( game[i][k] != 0 )
					{
						game[i][j] = game[i][k];
						game[i][k] = 0;
						break;
					}
				}
			}
		}
	}
	
	//3.刷新游戏界面 
	refresh();
	
}

//右移 ,先合并 后移动 
void move_right()
{
	//1.先合并
	int i,j,k;
	for( i=0; i<4; i++ )
	{
		for( j=3; j>0; j-- )
		{
			if( game[i][j] != 0 )
			{
				for( k=j-1; k>=0; k--)	//k表示它的下一个
				{
					if( game[i][j] != game[i][k] && game[i][k]!=0 )
					{
						break;
					}
					else if( game[i][j] != game[i][k] && game[i][k]==0 )
					{
						continue;	//中间隔0的情况,继续比较后面的数 
					}
					else if( game[i][j] == game[i][k] ) 
					{
						
						game[i][j] = game[i][j]*2;
						game[i][k] = 0;
						break;
					}
				}
			}
		}
	}

	//2.后移动,前一个为0,后一个不为0即可移动
	for( i=0; i<4; i++ )
	{
		for( j=3; j>0; j-- )
		{
			if( game[i][j] == 0 )
			{
				for( k=j-1; k>=0; k-- )
				{
					if( game[i][k] != 0 )
					{
						game[i][j] = game[i][k];
						game[i][k] = 0;
						break;
					}
				}
			}
		}
	}
	
	//3.刷新游戏界面 
	refresh();
}

//上移 ,先合并 后移动 
void move_up()
{
	//1.先合并
	int i,j,k;
	for( j=0; j<4; j++ )
	{
		for( i=0; i<3; i++ )
		{
			if( game[i][j] != 0 )
			{
				for( k=i+1; k<4; k++ )	//k表示它的下一个
				{
					if( game[i][j] != game[k][j] && game[k][j]!=0 )
					{
						break;
					}
					//else if( game[i][j] != game[i][k] && game[i][k]==0 )
					//{
					//	continue;	//中间隔0的情况,继续比较后面的数 
					//}
					else if( game[i][j] == game[k][j] ) 
					{
						game[i][j] = game[i][j]*2;
						game[k][j] = 0;
						break;
					}
				}
			}
		}
	}

	//2.后移动,前一个为0,后一个不为0即可移动
	for( j=0; j<4; j++ )
	{
		for( i=0; i<3; i++ )
		{
			if( game[i][j] == 0 )
			{
				for( k=i+1; k<4; k++ )
				{
					if( game[k][j] != 0 )
					{
						game[i][j] = game[k][j];
						game[k][j] = 0;
						break;
					}
				}
			}
		}
	}
	
	//3.刷新游戏界面 
	refresh();
}

//下移 ,先合并 后移动 
void move_down()
{
	//1.先合并
	int i,j,k;
	for( j=0; j<4; j++ )
	{
		for( i=3; i>0; i-- )
		{
			if( game[i][j] != 0 )
			{
				for( k=i-1; k>=0; k-- )	//k表示它的下一个
				{
					if( game[i][j] != game[k][j] && game[k][j]!=0 )
					{
						break;
					}
					else if( game[i][j] != game[k][j] && game[k][j]==0 )
					{
						continue;	//中间隔0的情况,继续比较后面的数 
					}
					else if( game[i][j] == game[k][j] ) 
					{
						game[i][j] = game[i][j]*2;
						game[k][j] = 0;
						break;
					}
				}
			}
		}
	}

	//2.后移动,前一个为0,后一个不为0即可移动
	for( j=0; j<4; j++ )
	{
		for( i=3; i>0; i-- )
		{
			if( game[i][j] == 0 )
			{
				for( k=i-1; k>=0; k-- )
				{
					if( game[k][j] != 0 )
					{
						game[i][j] = game[k][j];
						game[k][j] = 0;
						break;
					}
				}
			}
		}
	}
	
	//3.刷新游戏界面 
	refresh();

}


//(5)判断游戏是否成功 ,成功返回2,失败返回0

int is_win()
{
	int i,j;
	//遍历数组,是否出现2048
	for( i=0; i<4; i++ )
	{
		for( j=0; j<4; j++ )
		{
			if( game[i][j] == 2048 )
			{
				return 2;
			}
		}		
	}
	
}


game.h


#ifndef __GAME_H_
#define __GAME_H_

void refresh();
void position();
int my_2048_game();
int judge_move( int fx );
int judge_left();
int judge_right();
int judge_up();
int judge_down();
void move( int fx );
void move_left();
void move_right();
void move_up();
void move_down();
int is_win();

#endif
              

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

粤嵌实训笔记二 的相关文章

  • MATLAB 利用YALMIP+Gurobi 求解线性规划 -多无人机扫描覆盖

    使用要点 创建决策变量设置目标函数添加约束条件参数配置求解问题 问题描述 假设M个无人机的任务是尽快覆盖一组由 P 顶点表示的多边形凸区域 xff0c 假设每架无人机的最大飞行时间是有限的 xff0c 并且是预先知道的 每架无人机的都配备了
  • 毕业论文格式系列1 Word 图片交叉引用其题注

    图表论文自动编号 自动编号可以通过 Word 的 题注 功能实现 按论文格式要求 xff0c 第一章的图编号格式为 图1 X xff0c 具体做法如下 xff1a 将图插入文档中后 xff0c 选中新插入的图 xff0c 在 引用 菜单选
  • Visual Studio 2022 编译新版 Mission Planner 地面站

    下载安装VS 2022 安装时 xff0c 注意勾选 安装成功后 xff0c 从Visual Studio官方SDKs下载net461开发包 xff0c 网址 xff1a https dotnet microsoft com en us d
  • GNU Radio中的流标签(Stream Tags)

    目录 0 GR 中常用术语的官方解释 1 定义概述 2 在数据流中添加标签 3 添加标签的demo举例 4 从数据流中的获取标签 5 提取标签的demo举例 0 GR 中常用术语的官方解释 直接吧官方的解释抄过来 xff0c 直接看英文更容
  • 飞控学习随记

    常见指令 编译Arduplane程序 span class token builtin class name cd span ardupilot waf plane 进入 Tools autotest 文件夹中 xff0c 启动3D fli
  • 【无标题】

    apm飞控飞行模式详解 1 稳定模式Stabilize 稳定模式是使用得最多的飞行模式 xff0c 也是最基本的飞行模式 xff0c 起飞和降落都应该使用此模式 此模式下 xff0c 飞控会让飞行器保持稳定 xff0c 是初学者进行一般飞行
  • C# CustomMessageBox.Show() 输出多个变量调试

    Mission Planner 地面站调试中会遇到输出多个变量问题 xff0c 这里采用CustomMessageBox Show来输出调试多个变量 xff0c 用到string Format方法 span class token clas
  • MapReduce实验——学生总成绩报表,学生平均成绩

    学生总成绩报表 Map类 span class token keyword package span span class token class name StudentScore 06 span span class token pun
  • 【Docker操作必看,原来这才是正确打开Docker的新方式】

    前言 一 Docker操作镜像 首先镜像名称一般分为两个部分 xff1a repository tag xff0c 前者是镜像名 xff0c 后者是版本号 在没有指定tag的情况下 xff0c 默认是latest 代表的是最新版本 1 拉取
  • 第五章 FreeRTOS 任务基础知识

    5 1 什么是多任务系统 在使用 51 AVR STM32 单片机裸机 未使用系统 的时候一般都是在main 函数里面用 while 1 做一个大循环来完成所有的处理 xff0c 即应用程序是一个无限的循环 xff0c 循环中调用相应的函数
  • C语言for循环详解

    for 循环的使用更加灵活 xff0c 在日常的程序开发过程中我们会使用的更多一些 使用 while 循环来计算1加到100的值 xff0c 代码如下 xff1a include span class token generics func
  • Python批量下载sci-hub文献

    coding utf 8 import requests from bs4 import BeautifulSoup import os re path 61 34 Downloaded 34 if os path exists path
  • Ubuntu16.04 安装NS3.36.1及可视化模块

    如果不是必要 xff0c 尽量不要在Ubuntu 16 04上装3 36 1这个版本 xff0c 因为比较麻烦 NS3 36 1的新特性 安装依赖 一条一条执行 xff01 xff01 xff01 ns3 36需要用的python3 xff
  • ES6模块化及ES7新增特新性

    一 babel ES6代码转换为ES5的代码 1 初始化项目 npm init npm init y 不需要配置 xff0c 直接跳过 2 安装转码工具 cnpm install g babel cli cnpm install save
  • GNU Radio中的消息传递机制(Message Passing)

    目录 0 首先看下 GR 中一些常用术语的官方解释 1 定义理解 2 消息传递端口API 3 消息处理函数 4 通过流程图连接消息 5 从外部源发布数据 6 使用消息传送命令 7 一个消息传输的例子 0 首先看下 GR 中一些常用术语的官方
  • 单片机零基础完整攻略-1

    序 xff1a 学习原因 在网上看到各路大神用一个小小的板子就能玩起来一些很有趣的小项目 xff0c 觉得非常之神奇 为什么一个小小的板子就能做到物联网 xff0c 机器人那么多花里胡哨的功能 xff1f 正好赶上学校开设了这门课 xff0
  • HDF5 header version与HDF5 library不匹配问题的解决

    如图 xff1a 试着安装这个 conda install c conda forge hdf5 61 1 10 5 conda不行用pip 还不行就去这个网站下载 xff0c 上面搜索框直接搜hdf5 xff0c 然后找1 10 5版本的
  • Vscode C++的基础配置文件以及无法产生可编译文件exe的处理方法(undefined reference)

    采用排除法 xff1a 1 是否将工作文件夹添加工作区 xff1f 打开vscode 文件 打开文件夹 文件 将文件夹添加工作区 xff08 或者另存为一个 xff09 xff0c 把工作区文件放到工作文件夹里 如下 xff1a Manag
  • Arduino零基础实践2——串口数据发送

    具体的原理在微机开发中详细介绍了 xff0c 下面直接使用arduino进行数据收发 13条消息 单片机攻略4 中断和串口 r135792uuuu的博客 CSDN博客 void setup Serial begin 9600 void lo
  • px4开发bug记录

    一 仿真问题 1 roslaunch无法启动px4 gazebo的无人机仿真 xff0c 但是make px4 sitl gazbeo可以正常启动 2 make px4 sitl gazbeo启动到一半无法启动 xff0c 显示无法连接ga

随机推荐

  • linux无损扩容

    linux笔记本上空间不够用了 xff0c 重新从windows里划分30个g出来给linux xff0c 记录一下 1 准备u盘 xff0c u盘里面全部清空 xff0c 不能有任何东西 下载一个Ubuntu的桌面文件 xff0c 大概有
  • Linux更新源 source.list 自定义第三方源

    1 官方默认源 打开软件和更新 xff0c 直接图形化操作 但是只能设置一个源 xff0c 对于下载多种多样的包可能不够 xff0c 所以需要自定义多个不同源 2 自定义添加源 源的文件 sources list在 etc apt sour
  • 现在没有可用的软件包 *** ,但是它被其它的软件包引用了 和 E: 无法定位软件包 ***问题解决

    root 64 zhouls virtual machine snort src apt get install bison flex 正在读取软件包列表 完成 正在分析软件包的依赖关系树 正在读取状态信息 完成 现在没有可用的软件包 fl
  • sdf文件轨范

    sdf文件规范 xff1a a href https www zhihu com org aigraphx posts page 61 3 title 深圳季连AIGRAPHX 知乎 深圳季连AIGRAPHX 知乎 a span style
  • HBase基本操作

    HBase Java API 操作 Tips xff1a 其实每一个操作都可以简化为 xff1a 1 配置并连接数据库 2 编写 Java API 的 HBase 的操作 3 使用权限 执行操作 要对一个Hbase数据库进行操作的话 xff
  • GNU Radio3.8:编辑yaml文件的方法

    GNU Radio 学习使用 OOT 系列教程 xff1a GNU Radio3 8创建OOT的详细过程 基础 C 43 43 GNU Radio3 8创建OOT的详细过程 进阶 C 43 43 GNU Radio3 8创建OOT的详细过程
  • input上传图片并且实现预览

    文章目录 前言一 确定思路二 书写代码1 HTML部分2 CSS部分3 JS部分 xff08 重点 xff09 3 1 点击选择图片按钮 xff0c 调用input文件框事件的的代码3 2 转换格式3 3 发送图片给后端 三 编写优化1 i
  • hashcode()和equals()方法详解

    hashcode 和equals 方法详解 1 何为hashcode hash是一个函数 xff0c 就是通过一种算法来得到一个hash值 通过hash算法得到的hash值就存放在这张hash表中 xff0c 也就是说hash表示所有的ha
  • ubuntu安装kvm

    kvm是linux下的虚拟机 文章目录 一 电脑硬件支持二 安装ubuntu三 安装kvm 一 电脑硬件支持 首先不用多说啦 xff0c 既然是虚拟机 xff0c 当然要自己的机器支持虚拟技术 xff0c 重启机器进入biso xff0c
  • 类的静态(static)成员

    有时候类需要它的一些成员与类本身直接相关 xff0c 而不是与类的各个对象保持关联 xff08 这意味着无论创建多少个类的对象 xff0c 静态成员都只有一个副本 xff09 我们通过在成员的声明前加上关键字static使得其与类关联在一起
  • Keil uvision5 介绍

    keil 5 Keil uvision5 安装过程Keil uvision5安装包1 Keil uvision5 介绍2 Keil uVision5 特点3 Keil uVision5 功能4 Keil uVision5 快捷键 Keil
  • px4仿真时,/mavros/state现实连接不上

    仿真时 xff0c 使用px4 xff0c 启动 PX4 Firmware launch文件中的launch文件 进入gazebo世界中 xff0c 通过 xff1a rostopic list 查看发布的话题 xff0c 并且打印 mav
  • 插值方法(一维插值、三次样条插值、二维插值的matlab自带函数,python实现/作图)

    数模比赛中 xff0c 常常需要根据已知的函数点进行数据 模型的处理和分析 xff0c 而有时候现有的数据是极少的 xff0c 不足以支撑分析的进行 xff0c 这时就需要使用一些数学的方法 xff0c 模拟产生 一些新的单又比较靠谱的值来
  • ROS中常用命令

    1 xff09 工作空间初始化 xff1a catkin init workspace 2 xff09 创建功能包 xff1a catkin create pkg pkg name reply 3 xff09 编译工作空间中的功能包 xff
  • 【DL】CNN的前向传播和反向传播(python手动实现)

    卷积层的前向传播和反向传播 说明 本文中 xff0c 只实现一层卷积层的正反向传播 xff08 带激活函数Relu xff09 xff0c 实现的是多通道输入 xff0c 多通道输出的 之前对深度学习的理解大多止于pytorch里面现成的A
  • Postman使用教程详解

    目录 1 Postman安装与接口请求基本操作1 1Postman安装1 2发起一个接口请求的小测试 2 接口测试实战2 1百度IP查询接口从抓包到测试实战2 2需要设置头域的请求实战2 3文件上传与json请求实战 3 Newman命令行
  • GNU Radio3.8创建OOT的详细过程(基础/C++)

    GNU Radio 学习使用 OOT 系列教程 xff1a GNU Radio3 8创建OOT的详细过程 基础 C 43 43 GNU Radio3 8创建OOT的详细过程 进阶 C 43 43 GNU Radio3 8创建OOT的详细过程
  • Johnson-Trotter(JT)算法生成排列

    对于生成 xff5b 1 xff0c xff0c n xff5d 的所有n xff01 个排列的问题 xff0c 我们可以利用减治法 xff0c 该问题的规模减一就是要生成所有 xff08 n 1 xff09 xff01 个排列 假设这个小
  • OSMWebWizard无法使用(Address family not supported by protocol)

    根据报错信息依次打开osmWebWizard py SimpleWebSocketServer py 查看对应行号的内容 xff0c 发现Simple py中有一个socket net6 可能是网络协议的问题 xff0c 查了一下 xff0
  • 粤嵌实训笔记二

    目录 20230227 20230303 xff08 第二周 xff09 main clcd clcd hbmp cbmp hgame cgame h 20230227 20230303 xff08 第二周 xff09 1 在Linux下