import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Random;
class Test
{
public static int num[] = new int[42];
public static int numl = 16;
public static int chosenum[] = new int[2];
public static int chosenumno = 0;
public static int chosebtID = 0;
public static int chose = 0;
public static Boolean first = false;
public static void main(String[] args)
{
JFrame jtfMainFrame = new JFrame("Which Button Demo");
jtfMainFrame.setSize(450, 150);
JPanel jplPanel = new JPanel();
int i;
int num1 , num2;
//if(first == false){
for( i = 0 ;i<16;i++){
num[i] = i+1;
}
//first = true;
//}
JButton jbnButton[] = new JButton[16];
int buttonNum[] = new int[16];
jplPanel.setLayout (new GridLayout(4, 4));
for(i = 0;i<=15;i++){
Random ran = new Random();
chose = ran.nextInt(numl);
if(num[chose] == 16)
jbnButton[i]=new JButton("");
else
jbnButton[i]=new JButton(""+num[chose]);
buttonNum[i] = num[chose];
jplPanel.add(jbnButton[i]);
for(int j = chose ;j<numl;j++){
num[j] = num[j+1];
}
numl--;
System.out.println(numl);
jbnButton[i].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int i;
Boolean canmove = false;
Boolean GameOver = false;
int canmoveID = 0;
for( i=0; i<jbnButton.length; i++){
if(e.getSource() == jbnButton[i]){
/*if(chosenumno == 0){
chosenum[chosenumno] = buttonNum[i];
chosebtID = i;
chosenumno++;
}
else{
chosenum[chosenumno] = buttonNum[i];
buttonNum[chosebtID] = chosenum[chosenumno];
buttonNum[i] = chosenum[chosenumno-1];
if(buttonNum[i] == 16)
jbnButton[i].setText("");
else
jbnButton[i].setText(""+buttonNum[i]);
if(buttonNum[chosebtID] == 16)
jbnButton[chosebtID].setText("");
else
jbnButton[chosebtID].setText(""+buttonNum[chosebtID]);
chosenumno = 0;
}*/
if(i-1>=0 && canmove == false){
if(buttonNum[i-1] == 16){
buttonNum[i-1] = buttonNum[i];
buttonNum[i] = 16;
jbnButton[i-1].setText(""+buttonNum[i-1]);
jbnButton[i].setText("");
canmove = true;
}
}
if(i-4>=0 && canmove == false){
if(buttonNum[i-4] == 16){
buttonNum[i-4] = buttonNum[i];
buttonNum[i] = 16;
jbnButton[i-4].setText(""+buttonNum[i-4]);
jbnButton[i].setText("");
canmove = true;
}
}
if(i+1<=15 && canmove == false){
if(buttonNum[i+1] == 16){
buttonNum[i+1] = buttonNum[i];
buttonNum[i] = 16;
jbnButton[i+1].setText(""+buttonNum[i+1]);
jbnButton[i].setText("");
canmove = true;
}
}
if(i+4>=0 && canmove == false){
if(buttonNum[i+4] == 16){
buttonNum[i+4] = buttonNum[i];
buttonNum[i] = 16;
jbnButton[i+4].setText(""+buttonNum[i+4]);
jbnButton[i].setText("");
canmove = true;
}
}
for(int j = 0 ; j<16; j++){
if(buttonNum[j] != j+1)
break;
else if(j == 15 && buttonNum[j] == 16){
JFrame jtfMainFrame1 = new JFrame("WIN");
jtfMainFrame1.setSize(450, 150);
JPanel jplPanel1 = new JPanel();
jtfMainFrame1.getContentPane().add(jplPanel1, BorderLayout.CENTER);
jtfMainFrame1.setVisible(true);
}
}
canmove = false;
}
}
}
});
}
jtfMainFrame.getContentPane().add(jplPanel, BorderLayout.CENTER);
jtfMainFrame.setVisible(true);
//System.out.print("Yang");
}
}
沒有留言:
張貼留言