Java拼图游戏算法上的问题

kolnick 2011-10-13 09:52:27
package PinTu;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;

public class MyPintu implements ActionListener
{

private JButton b00, b01, b02, b10, b11, b12, b20, b21, b22;

private JFrame frame;

private ImageIcon[][] icon = new ImageIcon[3][3];
private ImageIcon Icon = new ImageIcon("");
private int[][] picture = new int[3][3];
private int[] array;

private Random random;

private boolean bol;

public void play() //生成0-8范围不同的随机数保存到数组当中
{
List<Integer> list = new ArrayList<Integer>();

while (true)
{
int num = (int) (Math.random() * 9);
boolean isHave = false;
for (int j = 0; j < list.size(); j++)
{
if (num == list.get(j))
{
isHave = true;
break;
}
}
if (!isHave)
{
list.add(num);
}

if (list.size() == 9)
{
break;
}
}
int[] arr = new int[9];
int t = 0;
while (t < 9)
{
for (Iterator iter = list.iterator(); iter.hasNext();)
{
String value = iter.next().toString();
int a = Integer.parseInt(value);
arr[t++] = a;
}
}
t = 0;
for (int i = 0; i < picture.length; i++)
{
for (int j = 0; j < picture[i].length; j++)
{
picture[i][j] = arr[t++];
}
}
for (int i = 0; i < picture.length; i++)
{
for (int j = 0; j < picture[i].length; j++)
{
icon[i][j] = new ImageIcon("src/picture/" + picture[i][j]
+ ".jpg");
}
}
b00.setIcon(icon[0][0]);
b01.setIcon(icon[0][1]);
b02.setIcon(icon[0][2]);
b10.setIcon(icon[1][0]);
b11.setIcon(icon[1][1]);
b12.setIcon(icon[1][2]);
b20.setIcon(icon[2][0]);
b21.setIcon(icon[2][1]);
b22.setIcon(icon[2][2]);
}

public MyPintu()
{
frame = new JFrame("拼图游戏");
b00 = new JButton();
b00.addActionListener(this);
b01 = new JButton();
b01.addActionListener(this);

b02 = new JButton();
b02.addActionListener(this);

b10 = new JButton();
b10.addActionListener(this);

b11 = new JButton();
b11.addActionListener(this);

b12 = new JButton();
b12.addActionListener(this);

b20 = new JButton();
b20.addActionListener(this);

b21 = new JButton();
b21.addActionListener(this);

b22 = new JButton();
b22.addActionListener(this);

this.init();
}

public void init()
{
frame.setLayout(new GridLayout(3, 3));
frame.add(b00);
frame.add(b01);
frame.add(b02);
frame.add(b10);
frame.add(b11);
frame.add(b12);
frame.add(b20);
frame.add(b21);
frame.add(b22);
frame.setSize(300, 300);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}

public static void main(String[] args)
{
MyPintu m = new MyPintu();
m.play();
}

public void actionPerformed(ActionEvent e)
{
if (e.getSource() == b00)
{
bol = false;
for (int i = 0; i < picture.length; i++)
{

for (int j = 0; j < picture[i].length; j++)
{
if (picture[i][j] == 0)
{
int n;
n = picture[i][j];
picture[i][j] = picture[0][0];
picture[0][0] = n;
ImageIcon icon1 = new ImageIcon("src/picture/"
+ picture[i][j] + ".jpg");
b00.setIcon(icon1);
ImageIcon icon2 = new ImageIcon("src/picture/"
+ picture[0][0] + ".jpg");
/*
* 一旦第i个==0 那么就进行替换按钮背景 在这里下面写不下去了 !!!!谁麻烦帮我修改下

* */
bol = true;
break;
}
}
if (bol == true)
{
break;
}
}
}
}
}
...全文
138 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
kolnick 2011-10-15
  • 打赏
  • 举报
回复
有谁能帮帮我吗
Robbie枫叶EX 2011-10-14
  • 打赏
  • 举报
回复
代码这么长,看着都晕了,占位学习中!
做一门精致,全面详细的 java数据结构与算法!!!让天下没有难学的数据结构,让天下没有难学的算法,不吹不黑,我们的讲师及其敬业,可以看到课程视频,课件,代码的录制撰写,都是在深夜,如此用心,其心可鉴,他不掉头发,谁掉头发???总之你知道的,不知道的,我们都讲,并且持续更新,走过路过,不要错过,不敢说是史上最全的课程,怕违反广告法,总而言之,言而总之,这门课你值得拥有,好吃不贵,对于你知识的渴求,我们管够管饱话不多说,牛不多吹,我们要讲的本门课程内容:稀疏数组、单向队列、环形队列、单向链表、双向链表、环形链表、约瑟夫问题、栈、前缀、中缀、后缀表达式、中缀表达式转换为后缀表达式、递归与回溯、迷宫问题、八皇后问题算法的时间复杂度、冒泡排序、选择排序、插入排序、快速排序、归并排序、希尔排序、基数排序(桶排序)、堆排序、排序速度分析、二分查找、插值查找、斐波那契查找、散列、哈希表、二叉树、二叉树与数组转换、二叉排序树(BST)、AVL树、线索二叉树、赫夫曼树、赫夫曼编码、多路查找树(B树B+树和B*树)、图、图的DFS算法和BFS、程序员常用10大算法、二分查找算法(非递归)、分治算法、动态规划算法、KMP算法、贪心算法、普里姆算法、克鲁斯卡尔算法、迪杰斯特拉算法、弗洛伊德算法马踏棋盘算法

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧