2014年8月12日 星期二

範例:九宮格遊戲

九宮格遊戲,在空格旁的格子點一下就可以把它移動到空格,直到從左上至右下分別1~9就算贏了。






撰寫的方式,首先寫一個產生版面的方法,這樣每次贏了要重啟版面的時候就很好用了,再來是產生格子,最後就是要怎麼玩的動作了!

import java.awt.*;
import java.applet.Applet;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.UIManager;
import javax.swing.SwingUtilities;
public class box extends JFrame implements ActionListener
{
        JButton[][] boxBu;
        int[] box=new int[9];
        public static void main(String[] arg)
        {
                new box();
        }
        public void tableSet()à遊戲版面產生
        {
                Font myFont=new Font("sansserif",Font.BOLD,48);
                int i=0;
                do
                {                     
                        boolean yn=false;
                        box[i]=(int)(Math.random()*9+1);
                       
                        for(int j=0;j<i;j++)
                        {
                                if(box[i]==box[j])
                                {
                                        yn=true;
                                        j=i;
                                }
                        }
                        if(yn==false)
                        {                                                                      
                                i=i+1;                              
                        }                      
                }while(i<=8);
                i=0;
                for(int j=0;j<=2;j++)
                {              
                        for(int k=0;k<=2;k++)
                        {      
                                add(boxBu[j][k]=new JButton(Integer.toString(box[i])));
                                boxBu[j][k].setFont(myFont);
                                boxBu[j][k].addActionListener(this);
                                i=i+1;
                                if(boxBu[j][k].getText().equals("9"))
                                {
                                        boxBu[j][k].setVisible(false);
                                       
                                }
                        }      
                }
        }
        public box()à格子產生
        {
                boxBu=new JButton[3][3];
                setTitle("BoxGame");
                setSize(900,900);
                setDefaultCloseOperation(DISPOSE_ON_CLOSE);

                setLayout(new GridLayout(3,3));                   
                tableSet();
                setVisible(true);
                                       
        }
        public void actionPerformed(ActionEvent e)à動作,這邊是用滑鼠的
        {
                int H=0;
                int W=0;
                int ynn=1;
                for(int j=0;j<=2;j++)
                {              
                        for(int k=0;k<=2;k++)
                        {      
                                for(int l=1;l<=9;l++)
                                {                              
                                        if(e.getActionCommand().equals(Integer.toString(l))&&boxBu[j]--[k].getText().equals(Integer.toString(l)))
                                        {
                                                H=j;
                                                W=k;
                                                break;
                                        }                                      
                                }
                        }      
                }
                String tmp;
                if(H-1>=0&&boxBu[H-1][W].getText().equals("9"))
                {                      
                                tmp=boxBu[H-1][W].getText();
                                boxBu[H-1][W].setText(boxBu[H][W].getText());
                                boxBu[H][W].setText(tmp);
                                boxBu[H][W].setVisible(false);
                                boxBu[H-1][W].setVisible(true);
                }
                else if(H+1<=2&&boxBu[H+1][W].getText().equals("9"))
                {
                                tmp=boxBu[H+1][W].getText();
                                boxBu[H+1][W].setText(boxBu[H][W].getText());
                                boxBu[H][W].setText(tmp);
                                boxBu[H][W].setVisible(false);
                                boxBu[H+1][W].setVisible(true);
                }
                else if(W-1>=0&&boxBu[H][W-1].getText().equals("9"))
                {
                                tmp=boxBu[H][W-1].getText();
                                boxBu[H][W-1].setText(boxBu[H][W].getText());
                                boxBu[H][W].setText(tmp);
                                boxBu[H][W].setVisible(false);
                                boxBu[H][W-1].setVisible(true);
                }
                else if(W+1<=2&&boxBu[H][W+1].getText().equals("9"))
                {
                                tmp=boxBu[H][W+1].getText();
                                boxBu[H][W+1].setText(boxBu[H][W].getText());
                                boxBu[H][W].setText(tmp);
                                boxBu[H][W].setVisible(false);
                                boxBu[H][W+1].setVisible(true);
                }
                boolean yn=true;
                for(int j=0;j<=2;j++)
                {              
                        for(int k=0;k<=2;k++)
                        {      
                                if(boxBu[j][k].getText().equals(Integer.toString(ynn)))
                                {
                                        ynn=ynn+1;
                                }                              
                        }      
                }
                               
                if(ynn==10)
                {
                        int com=JOptionPane.showConfirmDialog(null,"You Win!! Play --again?","Yo!!",JOptionPane.YES_NO_OPTION);       
                        if(com==JOptionPane.NO_OPTION)
                        {
                                System.exit(0);
                        }              
                }                                                                              
        }
}





如果您喜歡我的文章,請在文章最末按5下Like!
我將得到LikeCoin的回饋:)

回饋由LikeCoin基金會出資,您只要註冊/登入帳號(FB、Google帳號都可以註冊,流程超快),按L五次左鍵,可以贊助我的文章且完全不會花到錢!
支持創作,正向交流:)

沒有留言:

張貼留言