#include <stdint.h>
#include <TFTv2.h>
#include <SPI.h>
#include<math.h>
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int a[4][4]={}; //装本次(n+1)的数组的数字
int b[4][4]={}; //装上次(n)的数组的数字
int count;
int getmax();
int up1=0,up2=0,up3=0,up4=0;
void printa() //一号函数 ---打印数字--打印积分
{
int max;
char str[5],score[5];
if(count>0)
itoa(count-1, score, 10);//转化函数将int类型转换成字符串类型(整形,字符串,进制)
Tft.drawString(score,100,20,3,BLACK); //在(显示字符串内容,在x,y上,字体大小,字体颜色)
for(int i =0; i <240;i+=60)
{
for(int j =0 ; j<240 ;j+=60)
{
itoa(b[i/60][j/60], str, 10);
Tft.drawString(str,i+5,j+90,3,BLACK); //将上一步的显示字符串变为黑色(清除上一步骤)
itoa(a[i/60][j/60], str, 10);
Tft.drawString(str,i+5,j+90,3,CYAN) ; //将这一步的显示字符串
}
itoa(count, score, 10);
Tft.drawString(score,100,30,3,WHITE); //显示移动次数
}
max=getmax();//最大值函数寻找数组中的最大值
if(max>=1024)//五号函数--找到数组中最大的数,用于判断是否取得胜利
{
Tft.drawString("WIN",100,80,5,WHITE);
while(1);
}
}
void start() //二号函数
{
int x, y;
x = random(3) ;//从0-3中随机生成一个数字
y = random(3);
a[x][y] = 2;
x = random(3);
y =random(3) ;
while (a[x][y] == 2)
{
x = random(3) ;
y = random(3) ;
}
a[x][y] = 2;
for(int i =0; i <4;i++)
for(int j =0 ; j<4 ;j++)
b[i][j]=a[i][j];
}
void fresh() //三号函数--
{
srand((unsigned)time(NULL));//随机生成一个数字
int x, y;
x = rand() % 4;//让数字在0-3之间
y = rand() % 4;//让数字在0-3之间
while (a[x][y] != 0)
{
x = rand() % 4;
y = rand() % 4;
}
a[x][y] = 2;
}
int is_die()//判断游戏中是否存在相邻相等即可以合并的数存在返回1不存在返回0
{ //四号函数--判断整个四宫格满了,游戏失败
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 3; j++)
{
if (a[i][j] == a[i][j + 1])
{
return 1;
}
if (a[j][i] == a[j + 1][i])
{
return 1;
}
}
}
return 0;
}
int getmax() //五号函数--找到数组中最大的数,用于判断是否取得胜利
{
int max=0;
for(int i=0;i<=3;i++)
for(int j=0;j<=3;j++)
if(a[i][j]>=max)
max=a[i][j];
return max;
}
void run() //六号函数
{
char c;
c=panduan(); //七号函数--对于按键上下左右的感知
switch (c)
{
case 'A': //左
for (int j = 0; j < 4; ++j)
{
for (int i = 0; i < 4; ++i)
{
int k;
for (k = i - 1; k >= 0; --k)
{
if (a[k][j] != 0) //寻找该列中第一个不为0的位置
break;
}
a[k + 1][j] = a[i][j];
if ((k + 1) != i)
{
a[i][j] = 0;
}
}
}
for (int j = 0; j < 4; ++j)
{
for (int i = 0; i < 3; ++i)
{
if (a[i][j] == 0)
continue;
if (a[i][j] == a[i + 1][j])
{
a[i][j] *= 2;
a[i + 1][j] = 0;
++i;
}
}
}
break;
case 'D': //右
for (int j = 0; j < 4; ++j)
{
for (int i = 3; i >= 0; --i)
{
int k;
for (k = i + 1; k < 4; ++k)
{
if (a[k][j] != 0)
break;
}
a[k - 1][j] = a[i][j];
if ((k - 1) != i)
{
a[i][j] = 0;
}
}
}
case 'W': //上
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
int k;
for (k = j - 1; k >= 0; --k)
{
if (a[i][k] != 0)
{
break;
}
}
a[i][k + 1] = a[i][j];
if ((k + 1) != j){
a[i][j] = 0;
}
}
}
for (int j = 0; j < 4; ++j)
{
for (int i = 3; i > 0; --i)
{
if (a[i][j] == 0)
continue;
if (a[i][j] == a[i - 1][j])
{
a[i][j] *= 2;
a[i - 1][j] = 0;
--i;
}
}
}
break;
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 3; ++j)
{
if (a[i][j] == 0)
continue;
if (a[i][j] == a[i][j + 1])
{
a[i][j] *= 2;
a[i][j + 1] = 0;
++j;
}
}
}
break;
case 'S': //下
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 3; j++)
{
if (a[i][j] != 0)
{
for (int k = j + 1; k < 4; k++)
{
if (a[i][k] != 0)
{
if (a[i][k] == a[i][j])
{
a[i][k] *= 2;
a[i][j] = 0;
j = k;
break;
}
else
{
break;
}
}
}
}
}
}
for (int i = 0; i < 4; i++) {
for (int j = 3; j > 0; j--) {
if (a[i][j] == 0) {
for (int k = j - 1; k >= 0; k--) {
if (a[i][k] != 0) {
a[i][j] = a[i][k];
a[i][k] = 0;
break;
}
}
}
}
}
default:
break;
}
}
char panduan() //上下左右判断函数
{ //七号函数--对于按键上下左右的感知
while(1)
{
if(digitalRead(5)==HIGH)
{
up1=1;
}
else if(digitalRead(2)==HIGH)
{
up2=1;
}
else if(digitalRead(3)==HIGH)
{
up3=1;
}
else if(digitalRead(4)==HIGH)
{
up4=1;
}
if(up1 &&(digitalRead(5)==LOW))
{
up1=0;
return 'A';
}
else if(up2&&digitalRead(2)==LOW)
{
up2=0;
return 'W';
}
else if(up3&&digitalRead(3)==LOW)
{
up3=0;
return 'D';
}
else if(up4&&digitalRead(4)==LOW)
{
up4=0;
return 'S';
}
}
}
void setup()
{
Serial.begin(9600); //串口初始化为9600
SPSR |= (1 << SPI2X); // 2x SPI speed 串行外设接口
TFT_BL_ON; // turn on the background light
Tft.TFTinit( ); //ili9341屏幕初始化函数
for(int i = 0 ;i <= 240; i+=60 )
{
Tft.drawVerticalLine(i,70,260,GREEN); //从x=i开始画一条从y=0到y=260的一条竖线
Tft.drawHorizontalLine(0,i+70,280,GREEN); //从y=i开始画一条从x=0到x=280的一条竖线
}
Tft.drawString("Fer = " ,0,30,2,CYAN);
for(int i=2;i<=5;i++)
pinMode(i,INPUT);
count=0;
start();
printa();
run();
count++;
fresh();
printa();
delay(500);
while (1) {
if (is_die() == 1)
{
for(int i =0; i <4;i++)
for(int j =0 ; j<4 ;j++)
b[i][j]=a[i][j];
run();
fresh();
count++;
printa();
delay(500);
}
else
{
Tft.drawString("GG", 0,100,6,WHITE);
while(1);
}
}
}
void loop()
{
}
有一部分错误,不知道怎么改