2015年5月9日 星期六

智慧推盤 完成品

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");
}
}

2015年5月8日 星期五

第七周 按鈕名稱亂數不重複

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 numno = 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 j=1;
int num1 , num2;
//if(first == false){
for( i = 0 ;i<16;i++){
   num[i] = i+1;

}
//first = true;
//}
JButton jbnButton[]  = new JButton[16];
JButton changeButton[] = new JButton[2];
jplPanel.setLayout (new GridLayout(4, 4));
for(i = 0;i<=15;i++){
 jbnButton[i]=new JButton("Button"+(i+1));
 jplPanel.add(jbnButton[i]);
 String s = Integer.toString(i+1);
 jbnButton[i].addActionListener(new ActionListener() {

   public void actionPerformed(ActionEvent e) {

Random ran = new Random();
chose = ran.nextInt(numl);
System.out.println(chose);

int i;
for( i=0; i<jbnButton.length; i++){
          if(e.getSource() == jbnButton[i]){
              jbnButton[i].setText(""+num[chose]);
          }
}
for(i = chose ;i<numl;i++){
num[i] = num[i+1];
//System.out.println(num[chose]);
  }
numl--;
System.out.println(numl);
for(i = 0 ;i<numl;i++){
//System.out.println(num[i]);
}

   }
  });
}

jtfMainFrame.getContentPane().add(jplPanel, BorderLayout.CENTER);
jtfMainFrame.setVisible(true);
System.out.print("Yang");
}
}