先描述一下:
加载游戏的后先读取历史最高分并显示出来,4*4宫格随机生成两个坐标,设置为2,有个reset键可以重置游戏,相当于加载游戏。
左边的总分为此局游戏获取的分数,当合并两个相同的数字后,分数增加合并后的数字(合并的数字的两倍)。
界面没怎么做 (大佬勿喷 嘻嘻):
MainActivity.java
package com.example.mp;
import androidx.appcompat.app.AppCompatActivity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import org.w3c.dom.Text;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import static java.lang.Integer.max;
import static java.lang.Integer.parseInt;
public class MainActivity extends AppCompatActivity {
Button font;
//初始化数据库
private GameScore gDB;
private SQLiteDatabase mDB;
String s;
public void init(){
Cursor cursor = mDB.rawQuery("select * from Score order by cast(sco as '9999') desc", null);
while (cursor.moveToNext()){
s=cursor.getString(cursor.getColumnIndex("sco"));
return ;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//打开数据库
gDB = new GameScore(this,"Score.db",null,1);
mDB = gDB.getReadableDatabase();
init();
TextView history=findViewById(R.id.history);
history.setText(s);
// font=findViewById(R.id.welcome);
// font.setTypeface(Typeface.createFromAsset(getAssets(),"1.ttf"));
// font.setEnabled(false);
TextView score=findViewById(R.id.score);
TextView name=findViewById(R.id.name);
TextView ok=findViewById(R.id.ok);
TextView mm=findViewById(R.id.mm);
TextView hy=findViewById(R.id.hy);
TextView num0=findViewById(R.id.num0);
TextView num2=findViewById(R.id.num2);
TextView num4=findViewById(R.id.num4);
TextView num8=findViewById(R.id.num8);
TextView up=findViewById(R.id.up);
TextView down=findViewById(R.id.down);
TextView left=findViewById(R.id.left);
TextView right=findViewById(R.id.right);
TextView t11=findViewById(R.id.text11);
TextView t12=findViewById(R.id.text12);
TextView t13=findViewById(R.id.text13);
TextView t14=findViewById(R.id.text14);
//row2
TextView t21=findViewById(R.id.text21);
TextView t22=findViewById(R.id.text22);
TextView t23=findViewById(R.id.text23);
TextView t24=findViewById(R.id.text24);
//row3
TextView t31=findViewById(R.id.text31);
TextView t32=findViewById(R.id.text32);
TextView t33=findViewById(R.id.text33);
TextView t34=findViewById(R.id.text34);
//row4
TextView t41=findViewById(R.id.text41);
TextView t42=findViewById(R.id.text42);
TextView t43=findViewById(R.id.text43);
TextView t44=findViewById(R.id.text44);
t11.setTypeface(Typeface.createFromAsset(getAssets(),"1.ttf"));
t11.setEnabled(false);
t12.setTypeface(Typeface.createFromAsset(getAssets(),"1.ttf"));
t12.setEnabled(false);
t13.setTypeface(Typeface.createFromAsset(getAssets(),"1.ttf"));
t13.setEnabled(false);
t14.setTypeface(Typeface.createFromAsset(getAssets(),"1.ttf"));
t14.setEnabled(false);
t21.setTypeface(Typeface.createFromAsset(getAssets(),"1.ttf"));
t21.setEnabled(false);
t22.setTypeface(Typeface.createFromAsset(getAssets(),"1.ttf"));
t22.setEnabled(false);
t23.setTypeface(Typeface.createFromAsset(getAssets(),"1.ttf"));
t23.setEnabled(false);
t24.setTypeface(Typeface.createFromAsset(getAssets(),"1.ttf"));
t24.setEnabled(false);
t31.setTypeface(Typeface.createFromAsset(getAssets(),"1.ttf"));
t31.setEnabled(false);
t32.setTypeface(Typeface.createFromAsset(getAssets(),"1.ttf"));
t32.setEnabled(false);
t33.setTypeface(Typeface.createFromAsset(getAssets(),"1.ttf"));
t33.setEnabled(false);
t34.setTypeface(Typeface.createFromAsset(getAssets(),"1.ttf"));
t34.setEnabled(false);
t41.setTypeface(Typeface.createFromAsset(getAssets(),"1.ttf"));
t41.setEnabled(false);
t42.setTypeface(Typeface.createFromAsset(getAssets(),"1.ttf"));
t42.setEnabled(false);
t43.setTypeface(Typeface.createFromAsset(getAssets(),"1.ttf"));
t43.setEnabled(false);
t44.setTypeface(Typeface.createFromAsset(getAssets(),"1.ttf"));
t44.setEnabled(false);
name.setTypeface(Typeface.createFromAsset(getAssets(),"1.ttf"));
name.setEnabled(false);
ok.setTypeface(Typeface.createFromAsset(getAssets(),"1.ttf"));
ok.setEnabled(false);
score.setTypeface(Typeface.createFromAsset(getAssets(),"1.ttf"));
score.setEnabled(false);
mm.setTypeface(Typeface.createFromAsset(getAssets(),"1.ttf"));
mm.setEnabled(false);
num0.setTypeface(Typeface.createFromAsset(getAssets(),"1.ttf"));
num0.setEnabled(false);
num2.setTypeface(Typeface.createFromAsset(getAssets(),"1.ttf"));
num2.setEnabled(false);
num4.setTypeface(Typeface.createFromAsset(getAssets(),"1.ttf"));
num4.setEnabled(false);
num8.setTypeface(Typeface.createFromAsset(getAssets(),"1.ttf"));
num8.setEnabled(false);
hy.setTypeface(Typeface.createFromAsset(getAssets(),"1.ttf"));
hy.setEnabled(false);
// up.setTypeface(Typeface.createFromAsset(getAssets(),"1.ttf"));
// up.setEnabled(false);
// down.setTypeface(Typeface.createFromAsset(getAssets(),"1.ttf"));
// down.setEnabled(false);
// left.setTypeface(Typeface.createFromAsset(getAssets(),"1.ttf"));
// left.setEnabled(false);
// right.setTypeface(Typeface.createFromAsset(getAssets(),"1.ttf"));
// right.setEnabled(false);
//
// t11.setTypeface(Typeface.createFromAsset(getAssets(),"1.ttf"));
// t11.setEnabled(false);
// t11.setTypeface(Typeface.createFromAsset(getAssets(),"1.ttf"));
// t11.setEnabled(false);
}
//重置按钮 随机两个不同的坐标 设置为2,其余的为0
public void reset(View v){
// TextView ok=findViewById(R.id.ok);
// TextView score=findViewById(R.id.score);
TextView score=findViewById(R.id.score);
TextView mm=findViewById(R.id.mm);
TextView finish=findViewById(R.id.finish);
// finish
score.setText("0");
mm.setText("0");
TextView t11=findViewById(R.id.text11);
TextView t12=findViewById(R.id.text12);
TextView t13=findViewById(R.id.text13);
TextView t14=findViewById(R.id.text14);
//row2
TextView t21=findViewById(R.id.text21);
TextView t22=findViewById(R.id.text22);
TextView t23=findViewById(R.id.text23);
TextView t24=findViewById(R.id.text24);
//row3
TextView t31=findViewById(R.id.text31);
TextView t32=findViewById(R.id.text32);
TextView t33=findViewById(R.id.text33);
TextView t34=findViewById(R.id.text34);
//row4
TextView t41=findViewById(R.id.text41);
TextView t42=findViewById(R.id.text42);
TextView t43=findViewById(R.id.text43);
TextView t44=findViewById(R.id.text44);
finish.setText("");
t11.setText("0");
t12.setText("0");
t13.setText("0");
t14.setText(String.valueOf("0"));
t21.setText(String.valueOf("0"));
t22.setText("0");
t23.setText(String.valueOf("0"));
t24.setText(String.valueOf("0"));
t31.setText(String.valueOf("0"));
t32.setText(String.valueOf("0"));
t33.setText(String.valueOf("0"));
t34.setText(String.valueOf("0"));
t41.setText(String.valueOf("0"));
t42.setText(String.valueOf("0"));
t43.setText(String.valueOf("0"));
t44.setText(String.valueOf("0"));
//1.创建集合容器对象
List listx = new ArrayList();
List listy = new ArrayList();
//2.创建Random对象
Random r = new Random();
Random r1 = new Random();
//循环将得到的随机数进行判断,如果随机数不存在于集合中,则将随机数放入集合中,如果存在,则将随机数丢弃不做操作,进行下一次循环,直到集合长度等于nums
while(listx.size() != 2){
int num = r.nextInt(4)+1;
if(!listx.contains(num)){
listx.add(num);
}
}
while(listy.size() != 2){
int num = r.nextInt(4)+1;
if(!listy.contains(num)){
listy.add(num);
}
}
if((int)listx.get(0)==1 && (int)listy.get(0)==1){
t11.setText("2");
}else if((int)listx.get(0)==1 && (int)listy.get(0)==2){
t12.setText("2");
}else if((int)listx.get(0)==1 && (int)listy.get(0)==3){
t13.setText("2");
}else if((int)listx.get(0)==1 && (int)listy.get(0)==4){
t14.setText("2");
}else if((int)listx.get(0)==2 && (int)listy.get(0)==1){
t21.setText("2");
}else if((int)listx.get(0)==2 && (int)listy.get(0)==2){
t22.setText("2");
}else if((int)listx.get(0)==2 && (int)listy.get(0)==3){
t23.setText("2");
}else if((int)listx.get(0)==2 && (int)listy.get(0)==4){
t24.setText("2");
}else if((int)listx.get(0)==3 && (int)listy.get(0)==1){
t31.setText("2");
}else if((int)listx.get(0)==3 && (int)listy.get(0)==2){
t32.setText("2");
}else if((int)listx.get(0)==3 && (int)listy.get(0)==3){
t33.setText("2");
}else if((int)listx.get(0)==3 && (int)listy.get(0)==4){
t34.setText("2");
}else if((int)listx.get(0)==4 && (int)listy.get(0)==1){
t41.setText("2");
}else if((int)listx.get(0)==4 && (int)listy.get(0)==2){
t42.setText("2");
}else if((int)listx.get(0)==4 && (int)listy.get(0)==3){
t43.setText("2");
}else if((int)listx.get(0)==4 && (int)listy.get(0)==4) {
t44.setText("2");
}
if((int)listx.get(1)==1 && (int)listy.get(1)==1){
t11.setText("2");
}else if((int)listx.get(1)==1 && (int)listy.get(1)==2){
t12.setText("2");
}else if((int)listx.get(1)==1 && (int)listy.get(1)==3){
t13.setText("2");
}else if((int)listx.get(1)==1 && (int)listy.get(1)==4){
t14.setText("2");
}else if((int)listx.get(1)==2 && (int)listy.get(1)==1){
t21.setText("2");
}else if((int)listx.get(1)==2 && (int)listy.get(1)==2){
t22.setText("2");
}else if((int)listx.get(1)==2 && (int)listy.get(1)==3){
t23.setText("2");
}else if((int)listx.get(1)==2 && (int)listy.get(1)==4){
t24.setText("2");
}else if((int)listx.get(1)==3 && (int)listy.get(1)==1){
t31.setText("2");
}else if((int)listx.get(1)==3 && (int)listy.get(1)==2){
t32.setText("2");
}else if((int)listx.get(1)==3 && (int)listy.get(1)==3){
t33.setText("2");
}else if((int)listx.get(1)==3 && (int)listy.get(1)==4){
t34.setText("2");
}else if((int)listx.get(1)==4 && (int)listy.get(1)==1){
t41.setText("2");
}else if((int)listx.get(1)==4 && (int)listy.get(1)==2){
t42.setText("2");
}else if((int)listx.get(1)==4 && (int)listy.get(1)==3){
t43.setText("2");
}else if((int)listx.get(1)==4 && (int)listy.get(1)==4) {
t44.setText("2");
}
}
//四个方向 调用op方法
public void up(View v){
op("up");
}
public void down(View v){
op("down");
}
public void left(View v){
op("left");
}
public void right(View v){
op("right");
}
//找到第一个不是0的位置
public int findfirst(int pre, int[] arr){
while(pre<=3&&arr[pre]==0) pre++;
return pre;
}
//判断每一行每一列相邻元素已经没有相同
public boolean cirss(int pre, int next, int len, int[] arr){
if(next<=3){
if(arr[pre]==0||arr[next]==0)
return false;
}
//超过4 终止游戏
if(next>=len){
return true;
}
//还存在连续相同的数字 游戏继续
if(arr[pre]!=0 && arr[pre]==arr[next] && arr[pre]!=0 ){
return false;
}
pre++;
next++;
return cirss(pre,next,len,arr);
}
//合并的关键代码
//step1
public int[] cir(int pre, int next, int len, int[] arr){
// 终止条件
if(next==4){
return arr;
}
pre=findfirst(pre,arr);
//剩下的集合中找不到非零了
if(pre>=3){
return arr;
}
next=findfirst(pre+1,arr);
//***********************这里边界问题*****************
if(next==3&&arr[next]==0){
return arr;
}else if(next>3){
return arr;
}
if(arr[pre]==arr[next]){
//设置分数++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
TextView s=findViewById(R.id.score);
System.out.println("分数:"+s.getText());
int sum=-1;
if(s.getText().toString()!="")
sum=Integer.parseInt(s.getText().toString());
else{
sum=0;
}
arr[pre]*=2;
s.setText(String.valueOf(arr[pre]+sum));
arr[next]=0;
}
pre=next;
next+=1;
arr=cir(pre,next,len,arr);
return arr;
}
//step2 中间相邻的数字下标(中间有连续0也视为相邻)
public int[] calculate(int[] arr){
int pre=0,next=1,len=arr.length;
return cir(pre,next,len,arr);
}
//step3 把计算好的数字移动到某一边
public int[] getArr(int[] arr){
// int[] tmp ;
int[] tmp=new int[4];
int index=0;
for (int key:arr
) {
if(key!=0){
tmp[index++]=key;
}
}
for (int i=0;i<tmp.length;i++){
arr[i]=tmp[i];
}
for(int j=tmp.length;j<arr.length;j++){
arr[j]=0;
}
return arr;
}
//换了一种思路 就不需要这个方法了
public int[] erase0(int[] arr){
int flag=0;
for(int i=0;i<3;i++){
if(arr[i]==0){
flag=1;
arr[i]=arr[i+1];
arr[i+1]=0;
}
}
//合并
if(flag==1){
//暂且先添0 未设置随机位置生成数字
arr[3]=0;
}
return arr;
}
public static int getMax(int [] arr){
int max =arr[0];
for(int x =1;x<arr.length;x++){//注意这里不要越界
if(arr[x]>max){
max =arr[x];
}
}
return max;
}
public void setleft(){
TextView finish=findViewById(R.id.finish);
TextView ok=findViewById(R.id.ok);
TextView mm=findViewById(R.id.mm);
TextView score=findViewById(R.id.score);
TextView t11=findViewById(R.id.text11);
TextView t12=findViewById(R.id.text12);
TextView t13=findViewById(R.id.text13);
TextView t14=findViewById(R.id.text14);
//row2
TextView t21=findViewById(R.id.text21);
TextView t22=findViewById(R.id.text22);
TextView t23=findViewById(R.id.text23);
TextView t24=findViewById(R.id.text24);
//row3
TextView t31=findViewById(R.id.text31);
TextView t32=findViewById(R.id.text32);
TextView t33=findViewById(R.id.text33);
TextView t34=findViewById(R.id.text34);
//row4
TextView t41=findViewById(R.id.text41);
TextView t42=findViewById(R.id.text42);
TextView t43=findViewById(R.id.text43);
TextView t44=findViewById(R.id.text44);
//获取控件数字
int[] a={Integer.parseInt(t11.getText().toString()),Integer.parseInt(t12.getText().toString()),Integer.parseInt(t13.getText().toString()),Integer.parseInt(t14.getText().toString())};
//计算值 不是最终结果
a=calculate(a);
int[] a2={Integer.parseInt(t21.getText().toString()),Integer.parseInt(t22.getText().toString()),Integer.parseInt(t23.getText().toString()),Integer.parseInt(t24.getText().toString())};
a2=calculate(a2);
int[] a3={Integer.parseInt(t31.getText().toString()),Integer.parseInt(t32.getText().toString()),Integer.parseInt(t33.getText().toString()),Integer.parseInt(t34.getText().toString())};
a3=calculate(a3);
int[] a4={Integer.parseInt(t41.getText().toString()),Integer.parseInt(t42.getText().toString()),Integer.parseInt(t43.getText().toString()),Integer.parseInt(t44.getText().toString())};
a4=calculate(a4);
//用来判断改过么有,先存下来 千万别用 int[] aa=a; 这是引用,a改了 aa也改,判断不了
int[] aa=new int[4];
int[] aa2=new int[4];
int[] aa3=new int[4];
int[] aa4=new int[4];
for(int i=0;i<=3;i++){
aa[i]=a[i];
aa2[i]=a2[i];
aa3[i]=a3[i];
aa4[i]=a4[i];
}
// for (int key:a
// ) {
// System.out.println(key);
// }
int max1=getMax(a);
int maxv=max1;
int max2=getMax(a2);
int max3=getMax(a3);
int max4=getMax(a4);
if(maxv<max1){
maxv=max1;
}
if(maxv<max2){
maxv=max2;
}
if(maxv<max3){
maxv=max3;
}
if(maxv<max4){
maxv=max4;
}
mm.setText(String.valueOf(maxv));
// if(maxv==64){
// ok.setText("达成使命,任务完成!");
// }else{
// ok.setText("");
// }
if(maxv==2048){
ok.setText("达成使命,任务完成!");
}
//判断有没有改动数组 若没有 不生成随机数-游戏特性
boolean flagSame=false;
//第一行都要
int[] ta=getArr(a);
int[] ta2=getArr(a2);
int[] ta3=getArr(a3);
int[] ta4=getArr(a4);
for(int i=0;i<=3;i++){
//只要有一个不等 就改了则可以
if(ta[i]!=aa[i] ){
flagSame=true;
break;
}else if(ta2[i]!=aa2[i]){
flagSame=true;
break;
} else if (ta3[i]!=aa3[i]) {
flagSame=true;
break;
}else if(ta4[i]!=aa4[i]){
flagSame=true;
break;
}
}
// ************************************************************
a=getArr(a);
a2=getArr(a2);
a3=getArr(a3);
a4=getArr(a4);
int[] ya={a[0],a2[0],a3[0],a4[0]};
int[] ya2={a[1],a2[1],a3[1],a4[1]};
int[] ya3={a[2],a2[2],a3[2],a4[2]};
int[] ya4={a[3],a2[3],a3[3],a4[3]};
boolean fffffff=false;
System.out.println("bool x1:"+cirss(0,1,4,a));
System.out.println("bool x2:"+cirss(0,1,4,a2));
System.out.println("bool x3:"+cirss(0,1,4,a3));
System.out.println("bool x4:"+cirss(0,1,4,a4));
System.out.println("bool y1:"+cirss(0,1,4,ya));
System.out.println("bool y2:"+cirss(0,1,4,ya2));
System.out.println("bool y3:"+cirss(0,1,4,ya3));
System.out.println("bool y4:"+cirss(0,1,4,ya4));
if(cirss(0,1,4,a)&&cirss(0,1,4,a2)&&cirss(0,1,4,a3)&&cirss(0,1,4,a4)
&&cirss(0,1,4,ya)&&cirss(0,1,4,ya2)&&cirss(0,1,4,ya3)&&cirss(0,1,4,ya4)
){
fffffff=true;
}
if (fffffff){
finish.setText("失败");
//将分数写入数据库
mDB.execSQL("insert into Score values(null,?)",new String[]{
score.getText().toString()
});
}
// boolean kk=false;
// for(int i=0;i<=3;i++){
// if(a[i]==0 || a2[i]==0 ||a3[i]==0 || a4[i]==0){
// kk=true;
// break;
// }
// }
// if (!kk){
// ok.setText("任务失败,再接再厉");
// }
// boolean[] tag0=new boolean[4];
// boolean[] tag1=new boolean[4];
// boolean[] tag4=new boolean[4];
// for(int i=0;i<=3;i++){
// if(r0[i]==0){
// tag0[i]=true;
// }
// if(r1[i]==0){
// tag1[i]=true;
// }
// if(r4[i]==0){
// tag4[i]=true;
// }
// }
t11.setText(String.valueOf(a[0]));
t12.setText(String.valueOf(a[1]));
t13.setText(String.valueOf(a[2]));
t14.setText(String.valueOf(a[3]));
t21.setText(String.valueOf(a2[0]));
t22.setText(String.valueOf(a2[1]));
t23.setText(String.valueOf(a2[2]));
t24.setText(String.valueOf(a2[3]));
t31.setText(String.valueOf(a3[0]));
t32.setText(String.valueOf(a3[1]));
t33.setText(String.valueOf(a3[2]));
t34.setText(String.valueOf(a3[3]));
t41.setText(String.valueOf(a4[0]));
t42.setText(String.valueOf(a4[1]));
t43.setText(String.valueOf(a4[2]));
t44.setText(String.valueOf(a4[3]));
int[] r0=new int[4];
r0[0]=a2[0];
r0[1]=a2[3];
r0[2]=a3[0];
r0[3]=a3[3];
int[] r1=a;
int[] r4=a4;
//可知道边缘0的所有位置
//①直接随机块,②然后随机下标
//若下标元素不为0,则继续随机
//用途:找到一个空位置为止
boolean f=true;
System.out.println("ffffffffffffff+flagSame:"+flagSame);
if(flagSame){
while(f){
Random r = new Random();
// r.nextInt(); //返回一个随机整数
int random1=(int) r.nextInt(3);//返回一个10以内的随机整数,不包括10
// r.nextInt(); //返回一个随机整数
int random2=(int) r.nextInt(4);//返回一个10以内的随机整数,不包括10
System.out.println("r1:"+random1);
System.out.println("r2:"+random2);
if(random1==0){
if(random2==0){
if(r0[0]==0){
t21.setText(String.valueOf(2));
f=false;
}
//1 0
}else if(random2==1){
if(r0[1]==0){
t24.setText(String.valueOf(2));
f=false;
}
//1 3
}else if(random2==2){
if(r0[2]==0){
t31.setText(String.valueOf(2));
f=false;
}
//2 0
}else if(random2==3){
if(r0[3]==0){
t34.setText(String.valueOf(2));
f=false;
}
//2 3
}
}else if(random1==1){
if(random2==0){
//0 0
if(a[0]==0){
t11.setText(String.valueOf(2));
f=false;
}
}else if(random2==1){
if(a[1]==0){
t12.setText(String.valueOf(2));
f=false;
}
//0 1
}else if(random2==2){
if(a[2]==0){
t13.setText(String.valueOf(2));
f=false;
}
//0 2
}else if(random2==3){
if(a[3]==0){
t14.setText(String.valueOf(2));
f=false;
}
//0 3
}
}else if(random1==2){
if(random2==0){
if(a4[0]==0){
t41.setText(String.valueOf(2));
f=false;
}
//3 0
}else if(random2==1){
if(a4[1]==0){
t42.setText(String.valueOf(2));
f=false;
}
//3 1
}else if(random2==2){
if(a4[2]==0){
t43.setText(String.valueOf(2));
f=false;
}
//3 2
}else if(random2==3){
if(a4[3]==0){
t44.setText(String.valueOf(2));
f=false;
}
//3 3
}
}
}
}
// t11.setText("古德up");
}
public void setdown(){
TextView finish=findViewById(R.id.finish);
TextView mm=findViewById(R.id.mm);
TextView ok=findViewById(R.id.ok);
TextView t11=findViewById(R.id.text11);
TextView t12=findViewById(R.id.text12);
TextView t13=findViewById(R.id.text13);
TextView t14=findViewById(R.id.text14);
//row2
TextView t21=findViewById(R.id.text21);
TextView t22=findViewById(R.id.text22);
TextView t23=findViewById(R.id.text23);
TextView t24=findViewById(R.id.text24);
//row3
TextView t31=findViewById(R.id.text31);
TextView t32=findViewById(R.id.text32);
TextView t33=findViewById(R.id.text33);
TextView t34=findViewById(R.id.text34);
//row4
TextView t41=findViewById(R.id.text41);
TextView t42=findViewById(R.id.text42);
TextView t43=findViewById(R.id.text43);
TextView t44=findViewById(R.id.text44);
int[] a={Integer.parseInt(t41.getText().toString()),Integer.parseInt(t31.getText().toString()),Integer.parseInt(t21.getText().toString()),Integer.parseInt(t11.getText().toString())};
a=calculate(a);
int[] a2={Integer.parseInt(t42.getText().toString()),Integer.parseInt(t32.getText().toString()),Integer.parseInt(t22.getText().toString()),Integer.parseInt(t12.getText().toString())};
a2=calculate(a2);
int[] a3={Integer.parseInt(t43.getText().toString()),Integer.parseInt(t33.getText().toString()),Integer.parseInt(t23.getText().toString()),Integer.parseInt(t13.getText().toString())};
a3=calculate(a3);
int[] a4={Integer.parseInt(t44.getText().toString()),Integer.parseInt(t34.getText().toString()),Integer.parseInt(t24.getText().toString()),Integer.parseInt(t14.getText().toString())};
a4=calculate(a4);
//用来判断改过么有,先存下来 千万别用 int[] aa=a; 这是引用,a改了 aa也改,判断不了
int[] aa=new int[4];
int[] aa2=new int[4];
int[] aa3=new int[4];
int[] aa4=new int[4];
for(int i=0;i<=3;i++){
aa[i]=a[i];
aa2[i]=a2[i];
aa3[i]=a3[i];
aa4[i]=a4[i];
}
// for (int key:a
// ) {
// System.out.println(key);
// }
int max1=getMax(a);
int maxv=max1;
int max2=getMax(a2);
int max3=getMax(a3);
int max4=getMax(a4);
if(maxv<max1){
maxv=max1;
}
if(maxv<max2){
maxv=max2;
}
if(maxv<max3){
maxv=max3;
}
if(maxv<max4){
maxv=max4;
}
mm.setText(String.valueOf(maxv));
if(maxv==2048){
ok.setText("达成使命,任务完成!");
}
//判断有没有改动数组 若没有 不生成随机数-游戏特性
boolean flagSame=false;
//第一行都要
int[] ta=getArr(a);
int[] ta2=getArr(a2);
int[] ta3=getArr(a3);
int[] ta4=getArr(a4);
for(int i=0;i<=3;i++){
//只要有一个不等 就改了则可以
if(ta[i]!=aa[i] ){
flagSame=true;
break;
}else if(ta2[i]!=aa2[i]){
flagSame=true;
break;
} else if (ta3[i]!=aa3[i]) {
flagSame=true;
break;
}else if(ta4[i]!=aa4[i]){
flagSame=true;
break;
}
}
TextView score=findViewById(R.id.score);
// score.setText(String.valueOf(maxv));
a=getArr(a);
a2=getArr(a2);
a3=getArr(a3);
a4=getArr(a4);
int[] ya={a[0],a2[0],a3[0],a4[0]};
int[] ya2={a[1],a2[1],a3[1],a4[1]};
int[] ya3={a[2],a2[2],a3[2],a4[2]};
int[] ya4={a[3],a2[3],a3[3],a4[3]};
boolean fffffff=false;
if(cirss(0,1,4,a)&&cirss(0,1,4,a2)&&cirss(0,1,4,a3)&&cirss(0,1,4,a4)
&&cirss(0,1,4,ya)&&cirss(0,1,4,ya2)&&cirss(0,1,4,ya3)&&cirss(0,1,4,ya4)
){
fffffff=true;
}
if (fffffff){
finish.setText("失败");
}
// boolean kk=false;
// for(int i=0;i<=3;i++){
// if(a[i]==0 || a2[i]==0 ||a3[i]==0 || a4[i]==0){
// kk=true;
// break;
// }
// }
// if (!kk){
// ok.setText("任务失败,再接再厉");
// }
t11.setText(String.valueOf(a[3]));
t21.setText(String.valueOf(a[2]));
t31.setText(String.valueOf(a[1]));
t41.setText(String.valueOf(a[0]));
t12.setText(String.valueOf(a2[3]));
t22.setText(String.valueOf(a2[2]));
t32.setText(String.valueOf(a2[1]));
t42.setText(String.valueOf(a2[0]));
t13.setText(String.valueOf(a3[3]));
t23.setText(String.valueOf(a3[2]));
t33.setText(String.valueOf(a3[1]));
t43.setText(String.valueOf(a3[0]));
t14.setText(String.valueOf(a4[3]));
t24.setText(String.valueOf(a4[2]));
t34.setText(String.valueOf(a4[1]));
t44.setText(String.valueOf(a4[0]));
int[] r0=new int[4];
r0[0]=a2[0];
r0[1]=a2[3];
r0[2]=a3[0];
r0[3]=a3[3];
int[] r1=a;
int[] r4=a4;
//可知道边缘0的所有位置
//①直接随机块,②然后随机下标
//若下标元素不为0,则继续随机
boolean f=true;
if(flagSame){
while(f){
Random r = new Random();
// r.nextInt(); //返回一个随机整数
int random1=(int) r.nextInt(3);//返回一个10以内的随机整数,不包括10
// r.nextInt(); //返回一个随机整数
int random2=(int) r.nextInt(4);//返回一个10以内的随机整数,不包括10
System.out.println("r1:"+random1);
System.out.println("r2:"+random2);
if(random1==0){
if(random2==0){
if(r0[0]==0){
t42.setText(String.valueOf(2));
f=false;
}
//1 0
}else if(random2==1){
if(r0[1]==0){
t12.setText(String.valueOf(2));
f=false;
}
//1 3
}else if(random2==2){
if(r0[2]==0){
t43.setText(String.valueOf(2));
f=false;
}
//2 0
}else if(random2==3){
if(r0[3]==0){
t13.setText(String.valueOf(2));
f=false;
}
//2 3
}
}else if(random1==1){
if(random2==0){
//0 0
if(a[0]==0){
t41.setText(String.valueOf(2));
f=false;
}
}else if(random2==1){
if(a[1]==0){
t31.setText(String.valueOf(2));
f=false;
}
//0 1
}else if(random2==2){
if(a[2]==0){
t21.setText(String.valueOf(2));
f=false;
}
//0 2
}else if(random2==3){
if(a[3]==0){
t11.setText(String.valueOf(2));
f=false;
}
//0 3
}
}else if(random1==2){
if(random2==0){
if(a4[0]==0){
t44.setText(String.valueOf(2));
f=false;
}
//3 0
}else if(random2==1){
if(a4[1]==0){
t34.setText(String.valueOf(2));
f=false;
}
//3 1
}else if(random2==2){
if(a4[2]==0){
t24.setText(String.valueOf(2));
f=false;
}
//3 2
}else if(random2==3){
if(a4[3]==0){
t14.setText(String.valueOf(2));
f=false;
}
//3 3
}
}
}
}
// t11.setText("古德down");
}
public void setup(){
TextView finish=findViewById(R.id.finish);
TextView mm=findViewById(R.id.mm);
TextView ok=findViewById(R.id.ok);
TextView t11=findViewById(R.id.text11);
TextView t12=findViewById(R.id.text12);
TextView t13=findViewById(R.id.text13);
TextView t14=findViewById(R.id.text14);
//row2
TextView t21=findViewById(R.id.text21);
TextView t22=findViewById(R.id.text22);
TextView t23=findViewById(R.id.text23);
TextView t24=findViewById(R.id.text24);
//row3
TextView t31=findViewById(R.id.text31);
TextView t32=findViewById(R.id.text32);
TextView t33=findViewById(R.id.text33);
TextView t34=findViewById(R.id.text34);
//row4
TextView t41=findViewById(R.id.text41);
TextView t42=findViewById(R.id.text42);
TextView t43=findViewById(R.id.text43);
TextView t44=findViewById(R.id.text44);
// t11.setText("古德left");
int[] a={Integer.parseInt(t11.getText().toString()),Integer.parseInt(t21.getText().toString()),Integer.parseInt(t31.getText().toString()),Integer.parseInt(t41.getText().toString())};
a=calculate(a);
int[] a2={Integer.parseInt(t12.getText().toString()),Integer.parseInt(t22.getText().toString()),Integer.parseInt(t32.getText().toString()),Integer.parseInt(t42.getText().toString())};
a2=calculate(a2);
int[] a3={Integer.parseInt(t13.getText().toString()),Integer.parseInt(t23.getText().toString()),Integer.parseInt(t33.getText().toString()),Integer.parseInt(t43.getText().toString())};
a3=calculate(a3);
int[] a4={Integer.parseInt(t14.getText().toString()),Integer.parseInt(t24.getText().toString()),Integer.parseInt(t34.getText().toString()),Integer.parseInt(t44.getText().toString())};
a4=calculate(a4);
//用来判断改过么有,先存下来 千万别用 int[] aa=a; 这是引用,a改了 aa也改,判断不了
int[] aa=new int[4];
int[] aa2=new int[4];
int[] aa3=new int[4];
int[] aa4=new int[4];
for(int i=0;i<=3;i++){
aa[i]=a[i];
aa2[i]=a2[i];
aa3[i]=a3[i];
aa4[i]=a4[i];
}
// for (int key:a
// ) {
// System.out.println(key);
// }
int max1=getMax(a);
int maxv=max1;
int max2=getMax(a2);
int max3=getMax(a3);
int max4=getMax(a4);
if(maxv<max1){
maxv=max1;
}
if(maxv<max2){
maxv=max2;
}
if(maxv<max3){
maxv=max3;
}
if(maxv<max4){
maxv=max4;
}
mm.setText(String.valueOf(maxv));
if(maxv==2048){
ok.setText("达成使命,任务完成!");
}
//判断有没有改动数组 若没有 不生成随机数-游戏特性
boolean flagSame=false;
//第一行都要
int[] ta=getArr(a);
int[] ta2=getArr(a2);
int[] ta3=getArr(a3);
int[] ta4=getArr(a4);
for(int i=0;i<=3;i++){
//只要有一个不等 就改了则可以
if(ta[i]!=aa[i] ){
flagSame=true;
break;
}else if(ta2[i]!=aa2[i]){
flagSame=true;
break;
} else if (ta3[i]!=aa3[i]) {
flagSame=true;
break;
}else if(ta4[i]!=aa4[i]){
flagSame=true;
break;
}
}
// ************************************************************
TextView score=findViewById(R.id.score);
// score.setText(String.valueOf(maxv));
a=getArr(a);
a2=getArr(a2);
a3=getArr(a3);
a4=getArr(a4);
int[] ya={a[0],a2[0],a3[0],a4[0]};
int[] ya2={a[1],a2[1],a3[1],a4[1]};
int[] ya3={a[2],a2[2],a3[2],a4[2]};
int[] ya4={a[3],a2[3],a3[3],a4[3]};
boolean fffffff=false;
if(cirss(0,1,4,a)&&cirss(0,1,4,a2)&&cirss(0,1,4,a3)&&cirss(0,1,4,a4)
&&cirss(0,1,4,ya)&&cirss(0,1,4,ya2)&&cirss(0,1,4,ya3)&&cirss(0,1,4,ya4)
){
fffffff=true;
}
if (fffffff){
finish.setText("失败");
}
// boolean kk=false;
// for(int i=0;i<=3;i++){
// if(a[i]==0 || a2[i]==0 ||a3[i]==0 || a4[i]==0){
// kk=true;
// break;
// }
// }
// if (!kk){
// ok.setText("任务失败,再接再厉");
// }
t11.setText(String.valueOf(a[0]));
t21.setText(String.valueOf(a[1]));
t31.setText(String.valueOf(a[2]));
t41.setText(String.valueOf(a[3]));
t12.setText(String.valueOf(a2[0]));
t22.setText(String.valueOf(a2[1]));
t32.setText(String.valueOf(a2[2]));
t42.setText(String.valueOf(a2[3]));
t13.setText(String.valueOf(a3[0]));
t23.setText(String.valueOf(a3[1]));
t33.setText(String.valueOf(a3[2]));
t43.setText(String.valueOf(a3[3]));
t14.setText(String.valueOf(a4[0]));
t24.setText(String.valueOf(a4[1]));
t34.setText(String.valueOf(a4[2]));
t44.setText(String.valueOf(a4[3]));
int[] r0=new int[4];
r0[0]=a2[0];
r0[1]=a2[3];
r0[2]=a3[0];
r0[3]=a3[3];
int[] r1=a;
int[] r4=a4;
//可知道边缘0的所有位置
//①直接随机块,②然后随机下标
//若下标元素不为0,则继续随机
boolean f=true;
if(flagSame){
while(f){
Random r = new Random();
// r.nextInt(); //返回一个随机整数
int random1=(int) r.nextInt(3);//返回一个10以内的随机整数,不包括10
// r.nextInt(); //返回一个随机整数
int random2=(int) r.nextInt(4);//返回一个10以内的随机整数,不包括10
System.out.println("r1:"+random1);
System.out.println("r2:"+random2);
if(random1==0){
if(random2==0){
if(r0[0]==0){
t12.setText(String.valueOf(2));
f=false;
}
//1 0
}else if(random2==1){
if(r0[1]==0){
t42.setText(String.valueOf(2));
f=false;
}
//1 3
}else if(random2==2){
if(r0[2]==0){
t13.setText(String.valueOf(2));
f=false;
}
//2 0
}else if(random2==3){
if(r0[3]==0){
t43.setText(String.valueOf(2));
f=false;
}
//2 3
}
}else if(random1==1){
if(random2==0){
//0 0
if(a[0]==0){
t11.setText(String.valueOf(2));
f=false;
}
}else if(random2==1){
if(a[1]==0){
t21.setText(String.valueOf(2));
f=false;
}
//0 1
}else if(random2==2){
if(a[2]==0){
t31.setText(String.valueOf(2));
f=false;
}
//0 2
}else if(random2==3){
if(a[3]==0){
t41.setText(String.valueOf(2));
f=false;
}
//0 3
}
}else if(random1==2){
if(random2==0){
if(a4[0]==0){
t14.setText(String.valueOf(2));
f=false;
}
//3 0
}else if(random2==1){
if(a4[1]==0){
t24.setText(String.valueOf(2));
f=false;
}
//3 1
}else if(random2==2){
if(a4[2]==0){
t34.setText(String.valueOf(2));
f=false;
}
//3 2
}else if(random2==3){
if(a4[3]==0){
t44.setText(String.valueOf(2));
f=false;
}
//3 3
}
}
}
}
// t11.setText("古德down");
}
public void setright(){
TextView mm=findViewById(R.id.mm);
TextView finish=findViewById(R.id.finish);
TextView ok=findViewById(R.id.ok);
TextView t11=findViewById(R.id.text11);
TextView t12=findViewById(R.id.text12);
TextView t13=findViewById(R.id.text13);
TextView t14=findViewById(R.id.text14);
//row2
TextView t21=findViewById(R.id.text21);
TextView t22=findViewById(R.id.text22);
TextView t23=findViewById(R.id.text23);
TextView t24=findViewById(R.id.text24);
//row3
TextView t31=findViewById(R.id.text31);
TextView t32=findViewById(R.id.text32);
TextView t33=findViewById(R.id.text33);
TextView t34=findViewById(R.id.text34);
//row4
TextView t41=findViewById(R.id.text41);
TextView t42=findViewById(R.id.text42);
TextView t43=findViewById(R.id.text43);
TextView t44=findViewById(R.id.text44);
// t11.setText("古德right");
int[] a={Integer.parseInt(t14.getText().toString()),Integer.parseInt(t13.getText().toString()),Integer.parseInt(t12.getText().toString()),Integer.parseInt(t11.getText().toString())};
a=calculate(a);
int[] a2={Integer.parseInt(t24.getText().toString()),Integer.parseInt(t23.getText().toString()),Integer.parseInt(t22.getText().toString()),Integer.parseInt(t21.getText().toString())};
a2=calculate(a2);
int[] a3={Integer.parseInt(t34.getText().toString()),Integer.parseInt(t33.getText().toString()),Integer.parseInt(t32.getText().toString()),Integer.parseInt(t31.getText().toString())};
a3=calculate(a3);
int[] a4={Integer.parseInt(t44.getText().toString()),Integer.parseInt(t43.getText().toString()),Integer.parseInt(t42.getText().toString()),Integer.parseInt(t41.getText().toString())};
a4=calculate(a4);
//用来判断改过么有,先存下来 千万别用 int[] aa=a; 这是引用,a改了 aa也改,判断不了
int[] aa=new int[4];
int[] aa2=new int[4];
int[] aa3=new int[4];
int[] aa4=new int[4];
for(int i=0;i<=3;i++){
aa[i]=a[i];
aa2[i]=a2[i];
aa3[i]=a3[i];
aa4[i]=a4[i];
}
// for (int key:a
// ) {
// System.out.println(key);
// }
int max1=getMax(a);
int maxv=max1;
int max2=getMax(a2);
int max3=getMax(a3);
int max4=getMax(a4);
if(maxv<max1){
maxv=max1;
}
if(maxv<max2){
maxv=max2;
}
if(maxv<max3){
maxv=max3;
}
if(maxv<max4){
maxv=max4;
}
mm.setText(String.valueOf(maxv));
if(maxv==2048){
ok.setText("达成使命,任务完成!");
}
//判断有没有改动数组 若没有 不生成随机数-游戏特性
boolean flagSame=false;
//第一行都要
int[] ta=getArr(a);
int[] ta2=getArr(a2);
int[] ta3=getArr(a3);
int[] ta4=getArr(a4);
for(int i=0;i<=3;i++){
//只要有一个不等 就改了则可以
if(ta[i]!=aa[i] ){
flagSame=true;
break;
}else if(ta2[i]!=aa2[i]){
flagSame=true;
break;
} else if (ta3[i]!=aa3[i]) {
flagSame=true;
break;
}else if(ta4[i]!=aa4[i]){
flagSame=true;
break;
}
}
// ************************************************************
TextView score=findViewById(R.id.score);
// score.setText(String.valueOf(maxv));
a=getArr(a);
a2=getArr(a2);
a3=getArr(a3);
a4=getArr(a4);
int[] ya={a[0],a2[0],a3[0],a4[0]};
int[] ya2={a[1],a2[1],a3[1],a4[1]};
int[] ya3={a[2],a2[2],a3[2],a4[2]};
int[] ya4={a[3],a2[3],a3[3],a4[3]};
boolean fffffff=false;
if(cirss(0,1,4,a)&&cirss(0,1,4,a2)&&cirss(0,1,4,a3)&&cirss(0,1,4,a4)
&&cirss(0,1,4,ya)&&cirss(0,1,4,ya2)&&cirss(0,1,4,ya3)&&cirss(0,1,4,ya4)
){
fffffff=true;
}
if (fffffff){
finish.setText("失败");
}
// boolean kk=false;
// for(int i=0;i<=3;i++){
// if(a[i]==0 || a2[i]==0 ||a3[i]==0 || a4[i]==0){
// kk=true;
// break;
// }
// }
// if (!kk){
// ok.setText("任务失败,再接再厉");
// }
t11.setText(String.valueOf(a[3]));
t12.setText(String.valueOf(a[2]));
t13.setText(String.valueOf(a[1]));
t14.setText(String.valueOf(a[0]));
t21.setText(String.valueOf(a2[3]));
t22.setText(String.valueOf(a2[2]));
t23.setText(String.valueOf(a2[1]));
t24.setText(String.valueOf(a2[0]));
t31.setText(String.valueOf(a3[3]));
t32.setText(String.valueOf(a3[2]));
t33.setText(String.valueOf(a3[1]));
t34.setText(String.valueOf(a3[0]));
t41.setText(String.valueOf(a4[3]));
t42.setText(String.valueOf(a4[2]));
t43.setText(String.valueOf(a4[1]));
t44.setText(String.valueOf(a4[0]));
int[] r0=new int[4];
r0[0]=a2[0];
r0[1]=a2[3];
r0[2]=a3[0];
r0[3]=a3[3];
int[] r1=a;
int[] r4=a4;
//可知道边缘0的所有位置
//①直接随机块,②然后随机下标
//若下标元素不为0,则继续随机
boolean f=true;
if (flagSame){
while(f){
Random r = new Random();
// r.nextInt(); //返回一个随机整数
int random1=(int) r.nextInt(3);//返回一个10以内的随机整数,不包括10
// r.nextInt(); //返回一个随机整数
int random2=(int) r.nextInt(4);//返回一个10以内的随机整数,不包括10
System.out.println("r1:"+random1);
System.out.println("r2:"+random2);
if(random1==0){
if(random2==0){
if(r0[0]==0){
t24.setText(String.valueOf(2));
f=false;
}
//1 0
}else if(random2==1){
if(r0[1]==0){
t21.setText(String.valueOf(2));
f=false;
}
//1 3
}else if(random2==2){
if(r0[2]==0){
t34.setText(String.valueOf(2));
f=false;
}
//2 0
}else if(random2==3){
if(r0[3]==0){
t31.setText(String.valueOf(2));
f=false;
}
//2 3
}
}else if(random1==1){
if(random2==0){
//0 0
if(a[0]==0){
t14.setText(String.valueOf(2));
f=false;
}
}else if(random2==1){
if(a[1]==0){
t13.setText(String.valueOf(2));
f=false;
}
//0 1
}else if(random2==2){
if(a[2]==0){
t12.setText(String.valueOf(2));
f=false;
}
//0 2
}else if(random2==3){
if(a[3]==0){
t11.setText(String.valueOf(2));
f=false;
}
//0 3
}
}else if(random1==2){
if(random2==0){
if(a4[0]==0){
t44.setText(String.valueOf(2));
f=false;
}
//3 0
}else if(random2==1){
if(a4[1]==0){
t43.setText(String.valueOf(2));
f=false;
}
//3 1
}else if(random2==2){
if(a4[2]==0){
t42.setText(String.valueOf(2));
f=false;
}
//3 2
}else if(random2==3){
if(a4[3]==0){
t41.setText(String.valueOf(2));
f=false;
}
//3 3
}
}
}
}
// t11.setText("古德down");
}
public void op(String flag){
switch (flag){
case "up":
setup();
break;
case "down":
setdown();
break;
case "left":
setleft();
break;
case "right":
setright();
break;
}
}
}
数据库DB类 记录最高分
package com.example.mp;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import androidx.annotation.Nullable;
public class GameScore extends SQLiteOpenHelper {
//创建一张存储分数的数据库
private String CREATE_TABLE_SCORE = "create table score(_id integer primary key autoincrement,sco)";
public GameScore(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL(CREATE_TABLE_SCORE);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
}
xml就不摆出来了,另外增加了一个字体
效果图: