#2
湖中花2019-12-17 10:46
|
package com.example.administrator.myapplication;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.GridLayout;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
private GridLayout GL;
static int compare = 0, compare2;
static int change = 0;
static String txtall="",txtall2="";
static int img[][] = new int[6][10];
static int column[][] = new int[6][10];
static int row[][] = new int[6][10];
String img2[] = {"timg_01_03", "timg_01_04", "timg_01_05", "timg_01_33", "timg_01_44", "timg_01_46", "timg_01_50"};
// Handler handler=new Handler();
int strId[] = {R.drawable.timg_01_04, R.drawable.timg_01_03, R.drawable.timg_01_05, R.drawable.timg_01_33, R.drawable.timg_01_44, R.drawable.timg_01_46, R.drawable.timg_01_50};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GL = findViewById(R.id.gridLayout);
for (int i = 0; i <= 59; i++) {
ImageButton btn = new ImageButton(this);
btn.setId(i);
//btn.setText("a"+i);
// btn.getBackground(drawable/timg_01_03")
Random random = new Random(System.currentTimeMillis());
int i2 = i / 6;
int j = i % 6;
column[j][i2] = 0;
row[j][i2] = 0;
img[j][i2] = random.nextInt(7);
btn.setImageResource(strId[img[j][i2]]);
//btn.setImageResource(0);
btn.setPadding(0, 0, 0, 0);
btn.setOnClickListener(new btnListener(btn));
GL.addView(btn);
// handler.postDelayed(runnable,1000);
}
}
public class POINT {
public int x;
public int y;
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
public int getY() {
return y;
}
}
Runnable runnable = new Runnable() {
@Override
public void run() {
init();
}
};
private void init() {
int k = 0;
for (int j = 0; j <= 9; j++) {
for (int i = 0; i <= 5; i++) {
ImageButton btnChecked = findViewById(k);
if(img[i][j]==0){
btnChecked.setImageResource(0);
}else {
btnChecked.setImageResource(strId[img[i][j]]);}
k++;
}
}
}
private class btnListener implements View.OnClickListener {
//int strId = getResources().getIdentifier("R.drawable.timg_01_04", "string", getPackageName());
// int strId =R.drawable.timg_01_04;//21310997746,21310997747
private ImageButton btn2;
public btnListener(ImageButton btn) {
this.btn2 = btn;
}
@Override
public void onClick(View v) {
// btn2.setText("tt");
//Toast.makeText(MainActivity.this, v.getId() + "", Toast.LENGTH_LONG).show();
pushBotton(v.getId());
}
void pushBotton(int n) {
//aa();
// MessageBox(NULL, L"a", NULL, NULL);
int c;
int j = n % 6;
int i = n / 6;
//Toast.makeText(MainActivity.this, compare+"compare", Toast.LENGTH_LONG).show();
if (compare == 0) {
compare = n;
} else {
// aa();
compare2 = n;
if ((j + 1 + i * 6 == compare) && (j + 1 <= 5)) {
c = img[j + 1][i];
img[j + 1][i] = img[j][i];
img[j][i] = c;
compare = 0;
compare2 = 0;
Toast.makeText(MainActivity.this, "aa", Toast.LENGTH_LONG).show();
init();
change = 0;
eliminate();
// cellDown(hwnd);
if (change == 0) {
c = img[j + 1][i];
img[j + 1][i] = img[j][i];
img[j][i] = c;
init();
} else {
change = 0;
}
//MessageBox(hwnd, NULL, L"ll", NULL);
}
if ((j - 1 + i * 6 == compare) && (j - 1 >= 0)) {
c = img[j - 1][i];
img[j - 1][i] = img[j][i];
img[j][i] = c;
compare = 0;
compare2 = 0;
//Toast.makeText(MainActivity.this, "aa", Toast.LENGTH_LONG).show();
//InvalidateRect(hwnd, &rect, TRUE);
init();
change = 0;
eliminate();
//cellDown(hwnd);
if (change == 0) {
c = img[j - 1][i];
img[j - 1][i] = img[j][i];
img[j][i] = c;
init();
} else {
change = 0;
}
}
if (((j + i * 6 - 6) == compare) && (i - 1 >= 0)) {
c = img[j][i - 1];
img[j][i - 1] = img[j][i];
img[j][i] = c;
compare = 0;
compare2 = 0;
init();
change = 0;
eliminate();
// cellDown(hwnd);
if (change == 0) {
c = img[j][i - 1];
img[j][i - 1] = img[j][i];
img[j][i] = c;
init();
} else {
change = 0;
}
}
if (((j + i * 6 + 6) == compare) && (i + 1 <= 5)) {
c = img[j][i + 1];
img[j][i + 1] = img[j][i];
img[j][i] = c;
compare = 0;
compare2 = 0;
init();
change = 0;
eliminate();
//cellDown(hwnd);
if (change == 0) {
c = img[j][i + 1];
img[j][i + 1] = img[j][i];
img[j][i] = c;
init();
} else {
change = 0;
}
}
compare = compare2;
}
}
private void aa() {
Toast.makeText(getApplicationContext(),"aa()",Toast.LENGTH_LONG).show();
}
public void eliminate() {
//Toast.makeText(getApplicationContext(), "bbaa", Toast.LENGTH_LONG).show();
for (int i = 0; i <= 5; i++)
{
for (int j = 0; j <= 9; j++) {
if (column[i][j] == 0 && row[i][j] ==0)
{ eliminateCell(i, j);}
}
}
// Sleep(100);
cleanCell();
init();
}
void eliminateCell(int i, int j) {
POINT p[] = new POINT[10];
POINT q[] = new POINT[10];
for (int i2 = 0; i2 < 10; i2++) {
p[i2] = new POINT();
q[i2] = new POINT();
p[i2].x=0;
p[i2].y=0;
q[i2].x=0;
q[i2].y=0;
} //POINT p[10],q[10];
int main = img[i][j];
int k = 0, k2 = 0;
// p[1].setX(1);
//Toast.makeText(getApplicationContext(), k + "2k", Toast.LENGTH_LONG).show();
if (column[i][j] == 0) {
for (int up = 1; j - up >= 0; up++) {
// Toast.makeText(getApplicationContext(), k + "2k", Toast.LENGTH_LONG).show();
if (main == img[i][j - up]) {
column[i][j - up] = 1;
p[k].x=i;
p[k].setY(j - up);
//
k++;
} else {
break;
}
}
for (int down = 1; j + down < 10; down++) {
if (main == img[i][j + down]) {
column[i][j + down] = 1;
p[k].setX(i);
p[k].setY(j + down);
k++;
} else {
break;
}
}
}
/*
if (row[i][j] == 0) {
//Toast.makeText(getApplicationContext(), row[i][j]+ "row[i][j]", Toast.LENGTH_LONG).show();
for (int left = 1; i - left >=0; left++) {
if (main == img[i - left][j]) {
row[i - left][j] = 1;
q[k2].x = i - left;
q[k2].y = j;
//Toast.makeText(getApplicationContext(), i - left+ "row[i - left][j]"+j, Toast.LENGTH_LONG).show();
k2++;
} else {
break;
}
}
for (int right = 1; i + right < 6; right++) {
if (main == img[i + right][j]) {
row[i + right][j] = 1;
q[k2].x = i + right;
q[k2].y = j;
// Toast.makeText(getApplicationContext(), i + right+ "row[i + right][j]"+j, Toast.LENGTH_LONG).show();
k2++;
} else {
break;
}
}
}*/
TextView textView,textView1;
textView=findViewById(R.id.txt);
textView1=findViewById(R.id.txt);
//if (k》)
// Toast.makeText(getApplicationContext(), k + "kk" + k2, Toast.LENGTH_LONG).show();
String txtX="",txtY="";
if (k2 >= 2) {
row[i][j] = 1;
for (int l = 0; l < k2; l++) {
txtX+= q[l].x;
txtY+= q[l].y;
//row[i][j] = 0;
}
txtX+= i;
txtY+=j;
txtall+=txtX+">-"+txtY;
textView.setText(txtall);
Toast.makeText(MainActivity.this, txtX+">-"+txtY, Toast.LENGTH_LONG).show();
change = 1;
}
else if (k >= 2) {
String txtX2="",txtY2="";
for (int l = 0; l < k; l++) {
txtX2+= p[l].x;
txtY2+= p[l].y;
//row[i][j] = 0;
}
txtX2+= i;
txtY2+=j;
txtall2+=txtX2+">1"+txtY2;
textView1.setText(txtall2);
Toast.makeText(MainActivity.this, txtX2+">1"+txtY2, Toast.LENGTH_LONG).show();
column[i][j] = 1;
change = 1;
}
else
{
for (int l = 0; l < 10; l++) {
column[p[l].x][p[l].y] = 0;
column[i][j] = 0;
row[q[l].x][q[l].y] = 0;
row[i][j] = 0;
}
}
}
void cleanCell() {
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 6; j++) {
if (column[j][i] == 1 || row[j][i] == 1) {
img[j][i] = 0;
column[j][i] = 0;
row[j][i] = 0;
}
}
}
/*if (initCell==0)
{
//cellDown(hWnd);
}
initCell = 1;*/
}
}//xx
}
这对对碰哪里出问题了?