package com.yc.ui;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.wb.swt.SWTResourceManager;
import com.yc.dao.DBHelper;
import com.yc.utils.SWTUtil;
import com.yc.utils.SwtUiUtil;
import org.eclipse.swt.widgets.Button;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
public class Game extends Composite {
private int i;
private int j;
private int i3;
private int j3;
private GetXX get1 = new GetXX(null, 1, 1);
private GetXX get2 = new GetXX(null, 1, 1);
private Button[][] button = new Button[10][10];;
private double total;
private int z = 1;
private Text text;
private Text text_1;
private Text text_2;
private int i1;
private int j1;
private int i2;
private int j2;
private String[][] arr = new String[13][13];
private Composite composite;
/**
* Create the composite.
*
* @param parent
* @param style
*/
public Button[][] getbutton() {
return button;
}
public Game(Composite parent, int style) {
super(parent, style);
setLayout(new FillLayout(SWT.HORIZONTAL));
SashForm sashForm = new SashForm(this, SWT.NONE);
composite = new Composite(sashForm, SWT.NONE);
Composite composite_1 = new Composite(sashForm, SWT.NONE);
Label lblNewLabel = new Label(composite_1, SWT.NONE);
lblNewLabel.setBounds(76, 43, 76, 20);
lblNewLabel.setText("总分");
text = new Text(composite_1, SWT.BORDER);
text.setText("0");
text.setBounds(62, 77, 90, 26);
Label lblNewLabel_1 = new Label(composite_1, SWT.NONE);
lblNewLabel_1.setBounds(76, 151, 76, 20);
lblNewLabel_1.setText("完成度");
text_1 = new Text(composite_1, SWT.BORDER);
text_1.setText("0%");
text_1.setBounds(62, 177, 90, 26);
Label lblNewLabel_2 = new Label(composite_1, SWT.NONE);
lblNewLabel_2.setBounds(76, 240, 76, 20);
lblNewLabel_2.setText("步数");
text_2 = new Text(composite_1, SWT.BORDER);
text_2.setText("0");
text_2.setBounds(62, 276, 90, 26);
Button btnNewButton_1 = new Button(composite_1, SWT.NONE);
btnNewButton_1.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
if (help2()) {
button[i1][j1].setImage(
SWTResourceManager.getImage(Game.class, "/picA/0" + button[i1][j1].getData() + "s.png"));
button[i2][j2].setImage(
SWTResourceManager.getImage(Game.class, "/picA/0" + button[i2][j2].getData() + "s.png"));
System.out.println(i1 + "" + j1 + "" + i2 + "" + j2);
} else {
System.out.println("xxx");
}
}
});
btnNewButton_1.setBounds(62, 368, 98, 30);
btnNewButton_1.setText("提示");
Button btnNewButton_2 = new Button(composite_1, SWT.NONE);
btnNewButton_2.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
Display d = Display.getDefault();
RankList rl = new RankList(d);
Point centerPoint = SWTUtil.centerScreen(rl);
rl.setLocation(centerPoint);
rl.open();
}
});
btnNewButton_2.setBounds(62, 489, 98, 30);
btnNewButton_2.setText("查看榜单");
btnNewButton_2.setBounds(62, 489, 98, 30);
btnNewButton_2.setText("查看榜单");
Button btnNewButton_3 = new Button(composite_1, SWT.NONE);
btnNewButton_3.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
getArr();
setFile(arr);
String score = text.getText().trim().toString();
if (score == null || "".equals(score)) {
// SWTUtil.showMessageBox(getShell(),"出错了" ,"分数不能为空");
return;
}
String sql = "update llk_player set score=? where id=?";
// System.out.println(sql);
List<Object> params = new ArrayList<Object>();
params.add(score);
params.add(Login.MYID);
System.out.println(score + "" + Login.MYID);
DBHelper db = new DBHelper();
try {
int result = db.doUpdate(sql, params);
if (result > 0) {
System.out.println("积分存档完成");
}
} catch (SQLException e1) {
e1.printStackTrace();
SWTUtil.showMessageBox(getShell(), "出错了", e1.getMessage());
}
}
});
btnNewButton_3.setBounds(62, 611, 98, 30);
btnNewButton_3.setText("存档");
Button btnNewButton_4 = new Button(composite_1, SWT.NONE);
btnNewButton_4.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
getFile();
}
});
btnNewButton_4.setBounds(62, 708, 98, 30);
btnNewButton_4.setText("读档");
sashForm.setWeights(new int[] { 4, 1 });
for (i = 0; i < 10; i++) {
for (j = 0; j < 10; j++) {
Button btnNewButton = new Button(composite, SWT.NONE);
btnNewButton.setBounds(i * 80, j * 80, 80, 80);
button[i][j] = btnNewButton;
if (j <= 6) {
button[i][j].setImage(SWTResourceManager.getImage(Game.class, "/picA/0" + (j + 1) + "o.png"));
button[i][j].setData(j + 1);
}
if (j < 10 && j > 6) {
button[i][j].setImage(SWTResourceManager.getImage(Game.class, "/picA/0" + (10 - j) + "o.png"));
button[i][j].setData(10 - j);
}
}
}
Random rand = new Random();
int count = (9) * (9);
for (int i = 9; i >= 0; i--) {
for (int j = 9; j >= 0; j--) {
int n = rand.nextInt(count);
Button temp = button[i][j];
button[i][j] = button[n / (9) + 1][n % (9) + 1];
button[n / (9) + 1][n % (9) + 1] = temp;
button[i][j].setBounds(i * 80, j * 80, 80, 80);
button[n / (9) + 1][n % (9) + 1].setBounds((n / (9) + 1) * 80, (n % (9) + 1) * 80, 80, 80);
}
}
for (i = 0; i < 10; i++) {
for (j = 0; j < 10; j++) {
button[i][j].addSelectionListener(new SelectionAdapter() {
int tempi = i;
int tempj = j;
public void widgetSelected(SelectionEvent e) {
text_2.setText(z++ + "");
if (get1.getImage() == null) {
get1 = new GetXX(button[tempi][tempj].getImage(), tempi, tempj);
} else {
get2 = new GetXX(button[tempi][tempj].getImage(), tempi, tempj);
}
if (get1.getImage() == get2.getImage()) {
if (One(button, get1.getX(), get1.getY(), get2.getX(), get2.getY())
|| two(button, get1.getX(), get1.getY(), get2.getX(), get2.getY())
||three( get1.getX(), get1.getY(), get2.getX(), get2.getY())) {
yes(button[get2.getX()][get2.getY()], button[get1.getX()][get1.getY()]);
radio();
if(end()) {
SWTUtil.showMessageBox(getShell(), "游戏结束", "恭喜通关游戏");
}
} else {
no();
}
} else {
no();
}
}
});
}
}
}
public void yes(Button b1, Button b2) {
b1.dispose();
b2.dispose();
get2 = new GetXX(null, 1, 1);
get1 = new GetXX(null, 1, 1);
}
public void no() {
if (get2.getImage() != null) {
get1 = get2;
get2 = new GetXX(null, 1, 1);
}
}
// 直线
public boolean One(Button[][] button, int x1, int y1, int x2, int y2) {
if (x1 == x2) {
if (y1 < y2) {
for (int y = y1 + 1; y < y2; y++) {
if (button[x2][y].isDisposed() != true) {
return false;
}
}
return true;
} else if (y1 > y2) {
for (int y = y2 + 1; y < y1; y++) {
if (button[x2][y].isDisposed() != true) {
return false;
}
}
return true;
} else {
return false;
}
} else if (y1 == y2) {
if (x1 < x2) {
for (int y = x1 + 1; y < x2; y++) {
if (button[y][y2].isDisposed() != true) {
return false;
}
}
return true;
} else if (x1 > x2) {
for (int y = x2 + 1; y < x1; y++) {
if (button[y][y2].isDisposed() != true) {
return false;
}
}
return true;
} else {
return false;
}
}
return false;
}
// 一次转角
public boolean two(Button[][] button, int x1, int y1, int x2, int y2) {
int rightX = 0;
int rightY = 0;
int leftX = 0;
int leftY = 0;
if (x1 > x2 && y1 > y2) {
rightX = x2;
rightY = y1;
leftX = x1;
leftY = y2;
}
if (x1 < x2 && y1 < y2) {
rightX = x1;
rightY = y2;
leftX = x2;
leftY = y1;
}
if (button[rightX][rightY].isDisposed() == true && One(button, x1, y1, rightX, rightY)
&& One(button, rightX, rightY, x2, y2)) {
return true;
}
if (button[leftX][leftY].isDisposed() == true && One(button, x1, y1, leftX, leftY)
&& One(button, leftX, leftY, x2, y2)) {
return true;
}
if (x1 < x2 && y1 > y2) {
rightX = x2;
rightY = y1;
leftX = x1;
leftY = y2;
}
if (x1 > x2 && y1 < y2) {
rightX = x1;
rightY = y2;
leftX = x2;
leftY = y1;
}
if (button[rightX][rightY].isDisposed() == true && One(button, x1, y1, rightX, rightY)
&& One(button, rightX, rightY, x2, y2)) {
return true;
}
if (button[leftX][leftY].isDisposed() == true && One(button, x1, y1, leftX, leftY)
&& One(button, leftX, leftY, x2, y2)) {
return true;
}
return false;
}
public boolean three(int x1,int y1,int x2,int y2) {
if (x1 == x2 && y1 == y2)
{
return false;
}
for (int i = 0; i <10; i++)
{
for (int j = 0; j <10; j++)
{
if(button[i][j].isDisposed()==true) {
if(i==x1&&i!=x2 &&j!=y1&&j!=y2) {
if(One(button,x1,y1,i,j)&&two(button,x2,y2,i,j)) {
return true;
}
}
if(i!=x1&&i==x2 &&j!=y1&&j!=y2) {
if(One(button,x2,y2,i,j)&&two(button,x1,y1,i,j)) {
return true;
}
}
if(i!=x1&&i!=x2 &&j==y1&&j!=y2) {
if(One(button,x1,y1,i,j)&&two(button,x2,y2,i,j)) {
return true;
}
}
if(i!=x1&&i!=x2 &&j!=y1&&j==y2) {
if(One(button,x2,y2,i,j)&&two(button,x1,y1,i,j)) {
return true;
}
}
}
}
}
return false;
}
// 每点击一次 进行判断是否消除 并且改变text数值
public void radio() {
total = 0;
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if (button[i][j].isDisposed() == true) {
total = total + 1;
}
}
}
text.setText(total * 10 + "");
text_1.setText(total + "%");
}
// 帮助(循环中的循环)
public boolean help(Button bt, int a, int b) {
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if (button[i][j].isDisposed() != true) {
if (button[i][j].getImage() == bt.getImage()
&& (One(button, i, j, a, b) || two(button, i, j, a, b)||three(i,j,a,b))) {
if (i == a && j == b) {
return false;
} else {
i2 = i;
j2 = j;
return true;
}
}
}
}
}
return false;
}
// (循环)
public boolean help2() {
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if (button[i][j].isDisposed() != true) {
if (help(button[i][j], i, j)) {
i1 = i;
j1 = j;
return true;
}
}
}
}
return false;
}
public boolean end() {
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if (button[i][j].isDisposed() != true) {
return false;
}
}
}
return true;
}
public void getArr() {
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if (button[i][j].isDisposed() != true) {
arr[i][j] = button[i][j].getData().toString();
System.out.print(arr[i][j]);
} else {
arr[i][j] = "0";
}
}
System.out.println();
}
}
public void setFile(String[][] arr) {
FileDialog dialog = new FileDialog(getShell(), SWT.SAVE | SWT.SINGLE);
dialog.setFilterNames(new String[] { "存档(*.txt)", "所有文件" });
dialog.setFilterExtensions(new String[] { "*.txt", "*.*" });
String directory = dialog.open();
if (directory == null || "".equals(directory)) {
return;
}
File f = new File(directory);
if (f.isFile() && f.exists()) { // 原文件已经存在,不能保存.
SwtUiUtil.showMessage(getShell(), "出错了!", "文件已经存在");
return;
}
try {
FileWriter file = new FileWriter(f);
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
file.write(arr[i][j] + "\t");
}
file.write("\r\n");
}
file.write(text.getText() + "\n");
file.write(text_1.getText() + "\n");
file.write(text_2.getText() + "\n");
file.flush();
file.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
SwtUiUtil.showMessage(getShell(), "保存文件出错了", "流输出失败,请与开发工程师联系");
}
}
public void getFile() {
FileDialog dialog = new FileDialog(getShell(), SWT.OPEN | SWT.SINGLE);
dialog.setFilterNames(new String[] { "存档(*.txt)", "所有文件" });
dialog.setFilterExtensions(new String[] { "*.txt", "*.*" });
String directory = dialog.open();
if (directory == null || "".equals(directory)) {
return;
}
File f = new File(directory);
try {
BufferedReader in = new BufferedReader(new FileReader(f));
String line; // 一行数据
int row = 0;
// 逐行读取,并将每个数组放入到数组中
while ((line = in.readLine()) != null) {
String[] temp = line.split("\t");
System.out.print(temp.length);
for (int j = 0; j < temp.length; j++) {
arr[row][j] = temp[j];
}
row++;
}
text.setText(arr[10][0]);
text_1.setText(arr[11][0]);
text_2.setText(arr[12][0]);
try {
z = Integer.parseInt(arr[12][0]);
} catch (NumberFormatException e) {
e.printStackTrace();
}
in.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
SwtUiUtil.showMessage(getShell(), "读取文件出错了", "请与管理员联系");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
SwtUiUtil.showMessage(getShell(), "读取文件出错了", "请与管理员联系");
}
for ( i3 = 0; i3 < 10; i3++) {
for ( j3 = 0; j3 < 10; j3++) {
if (button[i3][j3].isDisposed() != true) {
if (arr[i3][j3].contains("0")) {
button[i3][j3].dispose();
} else {
button[i3][j3].setImage(SWTResourceManager.getImage(Game.class, "/picA/0" + arr[i3][j3] + "o.png"));
button[i3][j3].setData(arr[i3][j3]);
}
} else {
if (arr[i3][j3].contains("0")) {
button[i3][j3].dispose();
} else {
button[i3][j3] = new Button(composite, SWT.NONE);
button[i3][j3].addSelectionListener(new SelectionAdapter() {
int tempi = i3;
int tempj = j3;
public void widgetSelected(SelectionEvent e) {
text_2.setText(z++ + "");
if (get1.getImage() == null) {
get1 = new GetXX(button[tempi][tempj].getImage(), tempi, tempj);
} else {
get2 = new GetXX(button[tempi][tempj].getImage(), tempi, tempj);
}
if (get1.getImage() == get2.getImage()) {
if (One(button, get1.getX(), get1.getY(), get2.getX(), get2.getY())
|| two(button, get1.getX(), get1.getY(), get2.getX(), get2.getY())
||three( get1.getX(), get1.getY(), get2.getX(), get2.getY())) {
yes(button[get2.getX()][get2.getY()], button[get1.getX()][get1.getY()]);
radio();
// if(end()) {
// End end=new End(getShell(), SWT.NONE);
// }
} else {
no();
}
} else {
no();
}
}
});
button[i3][j3].setBounds(i3 * 80, j3 * 80, 80, 80);
button[i3][j3].setImage(SWTResourceManager.getImage(Game.class, "/picA/0" + arr[i3][j3] + "o.png"));
button[i3][j3].setData(arr[i3][j3]);
}
}
}
}
}
@Override
protected void checkSubclass() {
// Disable the check that prevents subclassing of SWT components
}
}
第一次连连看
最新推荐文章于 2024-10-09 11:13:12 发布