/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package system.test1;
/**
*
* @author 张修
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class author implements ActionListener
{
JFrame j=new JFrame("关于游戏");
JLabel j1=new JLabel("author:zx");
JLabel j2=new JLabel("email:1234");
public void init()
{
j.setBounds(320, 100, 250, 225);
j.setLayout(null);
j.add(j1);
j1.setBounds(40,20,200,100);
j2.setBounds(40,60,200,100);
j.add(j2);
j.getContentPane().setBackground(Color.pink);
j.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
new author().init();
}
}
class degree extends JFrame implements ActionListener{
JFrame jf=new JFrame("模式");
JButton b1=new JButton("简单");
JButton b2=new JButton("困难");
JButton b3=new JButton("关于游戏");
public void init(){
jf.setLayout(null);
b1.setBounds(150,30,200,100);
b1.setBackground(Color.green);
b2.setBounds(150,150,200,100);
b2.setBackground(Color.red);
b3.setBounds(150,270,200,100);
b3.setBackground(Color.pink);
jf.add(b1);
jf.add(b2);
jf.add(b3);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(new author());
jf.setBounds(280, 100, 500, 450);
//jf.setBackground(Color.red);
jf.getContentPane().setBackground(Color.black);
jf.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == b1)
{
jf.setVisible(false);
lianliankan l=new lianliankan();
l.randomBuild();
l.init();
}
if(e.getSource()==b2)
{
jf.setVisible(false);
lianliankan2 l1=new lianliankan2();
l1.randomBuild();
l1.init();
}
}
}
class lianliankan extends degree implements ActionListener {
static int a=8;
public lianliankan()
{
}
public lianliankan(int a)
{
this.a=a;
}
JFrame mainFrame;
Container thisContainer;
JPanel centerPanel, southPanel, northPanel;
JButton diamondsButton[][] = new JButton[a][a];
JButton exitButton, resetButton, newlyButton;
JLabel fractionLable = new JLabel("0");
JButton firstButton, secondButton;
int grid[][] = new int[a+2][a+2];
static boolean pressInformation = false;
int x0 = 0, y0 = 0, x = 0, y = 0, fristMsg = 0, secondMsg = 0;
int i, j, k, n;
public void init() {
mainFrame = new JFrame("连连看");
thisContainer = mainFrame.getContentPane();
thisContainer.setLayout(new BorderLayout());
centerPanel = new JPanel();
southPanel = new JPanel();
northPanel = new JPanel();
thisContainer.add(centerPanel, "Center");
thisContainer.add(southPanel, "South");
thisContainer.add(northPanel, "North");
centerPanel.setLayout(new GridLayout(a, a));
for (int cols = 0; cols < a; cols++) {
for (int rows = 0; rows < a; rows++) {
diamondsButton[cols][rows] = new JButton(
String.valueOf(grid[cols + 1][rows + 1]));
diamondsButton[cols][rows].addActionListener(this);
diamondsButton[cols][rows].setBackground(Color.YELLOW);
centerPanel.add(diamondsButton[cols][rows]);
}
}
exitButton = new JButton("退出");
exitButton.addActionListener(this);
resetButton = new JButton("重列");
resetButton.addActionListener(this);
southPanel.add(exitButton);
southPanel.add(resetButton);
fractionLable.setText(String.valueOf(Integer.parseInt(fractionLable
.getText())));//记录分数
northPanel.add(fractionLable);
mainFrame.setBounds(525, 100, 750, 750);
//mainFrame.getContentPane().setBackground(Color.green);
mainFrame.setVisible(true);
}
public void randomBuild() {
int randoms, cols, rows;
for (int twins = 1; twins <= a*a/2; twins++) {
randoms = (int) (Math.random() * 9 + 1);
for (int alike = 1; alike <= 2; alike++) {
cols = (int) (Math.random() * a +1);//避免重复两次循环
rows = (int) (Math.random() * a +1);
while (grid[cols][rows] != 0) {//再次赋值
cols = (int) (Math.random() * a+1 );
rows = (int) (Math.random() * a +1);
}
this.grid[cols][rows] = randoms;
}
}
}
public void fraction() {//计分方法
fractionLable.setText(String.valueOf(Integer.parseInt(fractionLable
.getText()) + 100));
}
public void reload() {//重排到每个二维数组中
int save[] = new int[a*a];
int n = 0, cols, rows;
int grid[][] = new int[a+2][a+2];
for (int i = 0; i <= a; i++) {
for (int j = 0; j <= a; j++) {
if (this.grid[i][j] != 0) {//循环二维数组如果成立赋值
save[n] = this.grid[i][j];
n++;
}
}
}
n = n - 1;
this.grid = grid;
while (n >= 0) {
cols = (int) (Math.random() * a + 1);
rows = (int) (Math.random() * a + 1);
while (grid[cols][rows] != 0) {
cols = (int) (Math.random() * a + 1);
rows = (int) (Math.random() * a + 1);
}
this.grid[cols][rows] = save[n];
n--;
}
mainFrame.setVisible(false);
pressInformation = false; // 要将按钮点击信息归为初始
init();
for (int i = 0; i < a; i++) {
for (int j = 0; j < a; j++) {
if (grid[i + 1][j + 1] == 0) {
diamondsButton[i][j].setVisible(false);//边界未赋值按钮设为不可见
}
}
}
}
public void estimateEven(int placeX, int placeY, JButton bz) {//检测第一个按钮按了吗 估计数estimateEven(cols + 1, rows + 1, diamondsButton[cols][rows]);
if (pressInformation == false) {
x = placeX;
y = placeY;
secondMsg = grid[x][y];
secondButton = bz;
pressInformation = true;
} else {
x0 = x;
y0 = y;
fristMsg = secondMsg;
firstButton = secondButton;
x = placeX;
y = placeY;
secondMsg = grid[x][y];
secondButton = bz;
if (fristMsg == secondMsg && secondButton != firstButton) {//信息相等按钮不同消去
xiao();
}
}
}
public void xiao() { // 相同的情况下能不能消去。仔细分析,不一条条注释
if ((x0 == x && (y0 == y + 1 || y0 == y - 1))
|| ((x0 == x + 1 || x0 == x - 1) && (y0 == y))) { // 判断是否相邻
remove();
} else {
for (j = 0; j < a+2; j++) {
if (grid[x0][j] == 0) { // 判断第一个按钮同行哪个按钮为空
if (y > j) { // 如果第二个按钮的Y坐标大于空按钮的Y坐标说明第一按钮在第二按钮左边
for (i = y - 1; i >= j; i--) { // 判断第二按钮左侧直到第一按钮中间有没有按钮
if (grid[x][i] != 0) {
k = 0;
break;
} else {
k = 1;
} // K=1说明通过了第一次验证
}
if (k == 1) {
linePassOne();
}
}
if (y < j) { // 如果第二个按钮的Y坐标小于空按钮的Y坐标说明第一按钮在第二按钮右边
for (i = y + 1; i <= j; i++) { // 判断第二按钮左侧直到第一按钮中间有没有按钮
if (grid[x][i] != 0) {
k = 0;
break;
} else {
k = 1;
}
}
if (k == 1) {
linePassOne();
}
}
if (y == j) {
linePassOne();
}
}
if (k == 2) {
if (x0 == x) {
remove();
}
if (x0 < x) {
for (n = x0; n <= x - 1; n++) {
if (grid[n][j] != 0) {
k = 0;
break;
}
if (grid[n][j] == 0 && n == x - 1) {
remove();
}
}
}
if (x0 > x) {
for (n = x0; n >= x + 1; n--) {
if (grid[n][j] != 0) {
k = 0;
break;
}
if (grid[n][j] == 0 && n == x + 1) {
remove();
}
}
}
}
}
for (i = 0; i < a+2; i++) { // 列
if (grid[i][y0] == 0) {
if (x > i) {
for (j = x - 1; j >= i; j--) {
if (grid[j][y] != 0) {
k = 0;
break;
} else {
k = 1;
}
}
if (k == 1) {
rowPassOne();
}
}
if (x < i) {
for (j = x + 1; j <= i; j++) {
if (grid[j][y] != 0) {
k = 0;
break;
} else {
k = 1;
}
}
if (k == 1) {
rowPassOne();
}
}
if (x == i) {
rowPassOne();
}
}
if (k == 2) {
if (y0 == y) {
remove();
}
if (y0 < y) {
for (n = y0; n <= y - 1; n++) {
if (grid[i][n] != 0) {
k = 0;
break;
}
if (grid[i][n] == 0 && n == y - 1) {
remove();
}
}
}
if (y0 > y) {
for (n = y0; n >= y + 1; n--) {
if (grid[i][n] != 0) {
k = 0;
break;
}
if (grid[i][n] == 0 && n == y + 1) {
remove();
}
}
}
}
}
}
}
public void linePassOne() {
if (y0 > j) { // 第一按钮同行空按钮在左边
for (i = y0 - 1; i >= j; i--) { // 判断第一按钮同左侧空按钮之间有没按钮
if (grid[x0][i] != 0) {
k = 0;
break;
} else {
k = 2;
} // K=2说明通过了第二次验证
}
}
if (y0 < j) { // 第一按钮同行空按钮在与第二按钮之间
for (i = y0 + 1; i <= j; i++) {
if (grid[x0][i] != 0) {
k = 0;
break;
} else {
k = 2;
}
}
}
}
public void rowPassOne() {
if (x0 > i) {
for (j = x0 - 1; j >= i; j--) {
if (grid[j][y0] != 0) {
k = 0;
break;
} else {
k = 2;
}
}
}
if (x0 < i) {
for (j = x0 + 1; j <= i; j++) {
if (grid[j][y0] != 0) {
k = 0;
break;
} else {
k = 2;
}
}
}
}
public void remove() {//设置两个按钮不可见 加分
firstButton.setVisible(false);
secondButton.setVisible(false);
fraction();
pressInformation = false;
k = 0;
grid[x0][y0] = 0;
grid[x][y] = 0;
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == exitButton) {
System.exit(0);
}
if (e.getSource() == resetButton) {
reload();
}
for (int cols = 0; cols < a; cols++) {
for (int rows = 0; rows < a; rows++) {
if (e.getSource() == diamondsButton[cols][rows]) {
estimateEven(cols + 1, rows + 1, diamondsButton[cols][rows]);
}
}
}
}
}
class lianliankan2 extends lianliankan
{
public void randomBuild() {
int randoms, cols, rows;
for (int twins = 1; twins <= a*a/2; twins++) {
randoms = (int) (Math.random() * 25 + 1);
for (int alike = 1; alike <= 2; alike++) {
cols = (int) (Math.random() * a +1);
rows = (int) (Math.random() * a +1);
while (grid[cols][rows] != 0) {
cols = (int) (Math.random() * a+1 );
rows = (int) (Math.random() * a +1);
}
this.grid[cols][rows] = randoms;
}
}
}
}
public class lianliankantest{
public static void main(String[] args) {
// TODO code application logic here
new degree().init();
//lianliankan l=new lianliankan();
//l.randomBuild();
//l.init();
}
}
java连连看(GUI)
最新推荐文章于 2024-04-25 11:43:21 发布