欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

基于Java實(shí)現(xiàn)經(jīng)典蜘蛛紙牌游戲

 更新時(shí)間:2022年05月12日 09:02:15   作者:錯(cuò)過(guò)了時(shí)間  
《蜘蛛紙牌》(Ancient?Spider)?是由Oberon?Games開(kāi)發(fā)的一款休閑益智類游戲。本文將利用Java語(yǔ)言實(shí)現(xiàn)這一經(jīng)典游戲,需要的可以參考一下

效果展示

前面的導(dǎo)入過(guò)程這里就不多說(shuō)了,不會(huì)的可以自己去問(wèn)度娘。導(dǎo)入后,選擇Spider.java類直接運(yùn)行就可以了,下面是游戲運(yùn)行的截圖:

游戲結(jié)構(gòu)

核心代碼

代碼顯示:代碼注釋很清楚 ,大家可以自行參考。

AboutDialog.java類

import javax.swing.*;
import java.awt.*;


/*
 **“關(guān)于”窗口
 */
public class AboutDialog extends JDialog
{
	JPanel jMainPane = new JPanel();

	JTabbedPane jTabbedPane = new JTabbedPane();
	private JPanel jPanel1 = new JPanel();
	private JPanel jPanel2 = new JPanel();

	private JTextArea jt1 = new JTextArea("將電腦多次分發(fā)給你的牌按照相同的花色由大至小排列起來(lái)。直到桌面上的牌全都消失。"); 
	private JTextArea jt2 = new JTextArea("該游戲中,紙牌的圖片來(lái)自于Windows XP的紙牌游戲,圖片權(quán)屬于原作者所有!"); 

	/*
	 **構(gòu)造函數(shù)
	 */
	public AboutDialog()
	{
		setTitle("蜘蛛牌");
		setSize(300,200);
		setResizable(false);
		setDefaultCloseOperation (WindowConstants.DISPOSE_ON_CLOSE); 
		
		Container c = this.getContentPane();
		
		jt1.setSize(260,200);
		jt2.setSize(260,200);
		
		jt1.setEditable(false);
		jt2.setEditable(false);
		
		jt1.setLineWrap(true); 
		jt2.setLineWrap(true); 

		jt1.setFont(new Font("楷體_GB2312", java.awt.Font.BOLD, 13));
		jt1.setForeground(Color.blue);

		jt2.setFont(new Font("楷體_GB2312", java.awt.Font.BOLD, 13));
		jt2.setForeground(Color.black);
		
		jPanel1.add(jt1);
		jPanel2.add(jt2);
		
		jTabbedPane.setSize(300,200);
		jTabbedPane.addTab("游戲規(guī)則", null, jPanel1, null);
		jTabbedPane.addTab("聲明", null, jPanel2, null);
		
		jMainPane.add(jTabbedPane);
		c.add(jMainPane);

		pack();
		this.setVisible(true);
	}
} 

PKCard.java類

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class PKCard extends JLabel implements MouseListener,
        MouseMotionListener{

    //紙牌的位置
	Point point = null;
    Point initPoint = null;
    
	int value = 0;
    int type = 0;
    
	String name = null;
    Container pane = null;
    
	Spider main = null;
    
	boolean canMove = false;
    boolean isFront = false;
    PKCard previousCard = null;

    public void mouseClicked(MouseEvent arg0){
	}
    
    public void flashCard(PKCard card){
		//啟動(dòng)Flash線程
		new Flash(card).start();
		//不停的獲得下一張牌,直到完成
		if(main.getNextCard(card) != null){
			card.flashCard(main.getNextCard(card));
		}
	}

    class Flash extends Thread{
		private PKCard card = null;
		
		public Flash(PKCard card){
			this.card = card;
		}
		
		/*
		 **線程的run()方法
		 **為紙牌的正面設(shè)置白色圖片
		 */
		public void run(){
			boolean is = false;
			ImageIcon icon = new ImageIcon("images/white.gif");
			for (int i = 0; i < 4; i++){
				try{
					Thread.sleep(200);
				}
				catch (InterruptedException e){
					e.printStackTrace();
				}
				
				if (is){
					this.card.turnFront();
					is = !is;
				}
				else{
					this.card.setIcon(icon);
					is = !is;
				}
				// 根據(jù)當(dāng)前外觀將card的UI屬性重置
				card.updateUI();
			}
		}
	}

    /**
	 **點(diǎn)擊鼠標(biāo)
	 */
	public void mousePressed(MouseEvent mp){
        point = mp.getPoint();
        main.setNA();
        this.previousCard = main.getPreviousCard(this);
    }

    /**
	 **釋放鼠標(biāo)
	 */
	public void mouseReleased(MouseEvent mr){
		Point point = ((JLabel) mr.getSource()).getLocation();
		//判斷可行列
		int n = this.whichColumnAvailable(point);
		if (n == -1 || n == this.whichColumnAvailable(this.initPoint)){
			this.setNextCardLocation(null);
			main.table.remove(this.getLocation());
			this.setLocation(this.initPoint);
			main.table.put(this.initPoint, this);
			return;
		}
		
		point = main.getLastCardLocation(n);
		boolean isEmpty = false;
		PKCard card = null;
		if (point == null){
			point = main.getGroundLabelLocation(n);
			isEmpty = true;
		}
		else{
			card = (PKCard) main.table.get(point);
		}
		
		if (isEmpty || (this.value + 1 == card.getCardValue())){
			point.y += 40;
			if (isEmpty) point.y -= 20;
			this.setNextCardLocation(point);
			main.table.remove(this.getLocation());
			point.y -= 20;
			this.setLocation(point);
			main.table.put(point, this);
			this.initPoint = point;
			if (this.previousCard != null){
				this.previousCard.turnFront();
				this.previousCard.setCanMove(true);
			}
			
			this.setCanMove(true);
		}
		else{
			this.setNextCardLocation(null);
			main.table.remove(this.getLocation());
			this.setLocation(this.initPoint);
			main.table.put(this.initPoint, this);
			return;
		}
        point = main.getLastCardLocation(n);
        card = (PKCard) main.table.get(point);
        if (card.getCardValue() == 1){
			point.y -= 240;
			card = (PKCard) main.table.get(point);
			if (card != null && card.isCardCanMove()){
				main.haveFinish(n);
			}
		}
	}
	
	/*
	 **方法:放置紙牌
	 */
	public void setNextCardLocation(Point point){
		PKCard card = main.getNextCard(this);
		if (card != null){
			if (point == null){
				card.setNextCardLocation(null);
				main.table.remove(card.getLocation());
				card.setLocation(card.initPoint);
				main.table.put(card.initPoint, card);
			}
			else{
				point = new Point(point);
				point.y += 20;
				card.setNextCardLocation(point);
				point.y -= 20;
				main.table.remove(card.getLocation());
				card.setLocation(point);
				main.table.put(card.getLocation(), card);
				card.initPoint = card.getLocation();
			}
		}
	}

    /**
	 **返回值:int
	 **方法:判斷可用列
	 */
	public int whichColumnAvailable(Point point){
		int x = point.x;
		int y = point.y;
		int a = (x - 20) / 101;
		int b = (x - 20) % 101;
		if (a != 9){
			if (b > 30 && b <= 71){
				a = -1;
			}
			else if (b > 71){
				a++;
			}
		}
		else if (b > 71){
			a = -1;
		}
		
		if (a != -1){
			Point p = main.getLastCardLocation(a);
			if (p == null) p = main.getGroundLabelLocation(a);
			b = y - p.y;
			if (b <= -96 || b >= 96){
				a = -1;
			}
		}
		return a;
	}

    public void mouseEntered(MouseEvent arg0){
    }

    public void mouseExited(MouseEvent arg0){
    }

    /**
	 **用鼠標(biāo)拖動(dòng)紙牌
	 */
	public void mouseDragged(MouseEvent arg0){
        if (canMove){
			int x = 0;
			int y = 0;
			Point p = arg0.getPoint();
			x = p.x - point.x;
			y = p.y - point.y;
			this.moving(x, y);
		}
	}

    /**
	 **返回值:void
	 **方法:移動(dòng)(x,y)個(gè)位置
	 */
	public void moving(int x, int y){
        PKCard card = main.getNextCard(this);
        Point p = this.getLocation();
        
		//將組件移動(dòng)到容器中指定的順序索引。 
		pane.setComponentZOrder(this, 1);
        
		//在Hashtable中保存新的節(jié)點(diǎn)信息
		main.table.remove(p);
        p.x += x;
        p.y += y;
        this.setLocation(p);
        main.table.put(p, this);
        if (card != null) card.moving(x, y);
    }

    public void mouseMoved(MouseEvent arg0){
    }

    /**
     **構(gòu)造函數(shù)
     */
    public PKCard(String name, Spider spider){
        super();
        this.type = new Integer(name.substring(0, 1)).intValue();
        this.value = new Integer(name.substring(2)).intValue();
        this.name = name;
        this.main = spider;
        this.pane = this.main.getContentPane();
        this.addMouseListener(this);
        this.addMouseMotionListener(this);
        this.setIcon(new ImageIcon("images/rear.gif"));
        this.setSize(71, 96);
        this.setVisible(true);
    }

    /**
	 **返回值:void
	 **方法:令紙牌顯示正面
	 */
	public void turnFront(){
        this.setIcon(new ImageIcon("images/" + name + ".gif"));
        this.isFront = true;
    }

    /**
	 **返回值:void
	 **方法:令紙牌顯示背面
	 */
	public void turnRear(){
        this.setIcon(new ImageIcon("images/rear.gif"));
        this.isFront = false;
        this.canMove = false;
    }

    /**
	 **返回值:void
	 **方法:將紙牌移動(dòng)到點(diǎn)point
	 */
	public void moveto(Point point){
        this.setLocation(point);
        this.initPoint = point;
    }

    /**
	 **返回值:void
	 **方法:判斷牌是否能移動(dòng)
	 */
	public void setCanMove(boolean can){
        this.canMove = can;
        PKCard card = main.getPreviousCard(this);
        if (card != null && card.isCardFront()){
            if (!can){
                if (!card.isCardCanMove()){
                    return;
				}
                else{
					card.setCanMove(can);
				}
			}
            else{
                if (this.value + 1 == card.getCardValue()
                        && this.type == card.getCardType()){
					card.setCanMove(can);
				}
				else{
					card.setCanMove(false);
				}
            }
        }
    }

    /**
	 **返回值:boolean
	 **方法:判斷card是否是正面
	 */
	public boolean isCardFront(){
        return this.isFront;
    }

    /*
	 **返回值:boolean
	 **方法:返回是否能夠移動(dòng)
	 */
	public boolean isCardCanMove(){
        return this.canMove;
    }

    /**
	 **返回值:int
	 **方法:獲得card的內(nèi)容值
	 */
	public int getCardValue(){
        return value;
    }

    /**
	 **返回值:int
	 **方法:獲得card的類型
	 */
	public int getCardType(){
        return type;
    }
}

SpiderMenuBar.java類

import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.ButtonGroup;

public class SpiderMenuBar extends JMenuBar{
    
	//生成spider框架對(duì)象
    Spider main = null;
   
    //生成菜單組
    JMenu jNewGame = new JMenu("游戲");
    JMenu jHelp = new JMenu("幫助");
    
    //生成菜單項(xiàng)
    JMenuItem jItemAbout = new JMenuItem("關(guān)于");
    JMenuItem jItemOpen = new JMenuItem("開(kāi)局");
    JMenuItem jItemPlayAgain = new JMenuItem("重新發(fā)牌");
   
    //生成單選框
    JRadioButtonMenuItem jRMItemEasy = new JRadioButtonMenuItem("簡(jiǎn)單:?jiǎn)我换ㄉ?);
    JRadioButtonMenuItem jRMItemNormal = new JRadioButtonMenuItem("中級(jí):雙花色");
    JRadioButtonMenuItem jRMItemHard = new JRadioButtonMenuItem("高級(jí):四花色");;
    
    JMenuItem jItemExit = new JMenuItem("退出");
    JMenuItem jItemValid = new JMenuItem("顯示可行操作");
    
    
    /**
     **構(gòu)造函數(shù),生成JMenuBar的圖形界面
     */
    public SpiderMenuBar(Spider spider){
        
        this.main = spider;
        
        /**
         **初始化“游戲”菜單欄
         */
        jNewGame.add(jItemOpen);
        jNewGame.add(jItemPlayAgain);
        jNewGame.add(jItemValid);
        
        jNewGame.addSeparator();
        jNewGame.add(jRMItemEasy);
        jNewGame.add(jRMItemNormal);
        jNewGame.add(jRMItemHard);
        
        jNewGame.addSeparator();
        
        jNewGame.add(jItemExit);
        
        ButtonGroup group = new ButtonGroup();
        group.add(jRMItemEasy);
        group.add(jRMItemNormal);
        group.add(jRMItemHard);

        jHelp.add(jItemAbout);

        this.add(jNewGame);
        this.add(jHelp);

		//為組件添加事件監(jiān)聽(tīng)并實(shí)現(xiàn)
        //“開(kāi)局”
		jItemOpen.addActionListener(new java.awt.event.ActionListener() { 
            public void actionPerformed(java.awt.event.ActionEvent e) {    
                main.newGame();
            }
        });

        //“重新發(fā)牌”
		jItemPlayAgain.addActionListener(new java.awt.event.ActionListener() { 
            public void actionPerformed(java.awt.event.ActionEvent e) {    
                if(main.getC() < 60){
                    main.deal();
                }
            }
        });

        //"顯示可行操作"
		jItemValid.addActionListener(new java.awt.event.ActionListener() { 
            public void actionPerformed(java.awt.event.ActionEvent e) {    
                new Show().start();
            }
        });
        
        //“退出”
		jItemExit.addActionListener(new java.awt.event.ActionListener() { 
            public void actionPerformed(java.awt.event.ActionEvent e) {    
                main.dispose();
                System.exit(0);
            }
        });
		
		//“簡(jiǎn)單級(jí)別”默認(rèn)已選
		jRMItemEasy.setSelected(true);
        
		//“簡(jiǎn)單級(jí)別”
		jRMItemEasy.addActionListener(new java.awt.event.ActionListener() { 
            public void actionPerformed(java.awt.event.ActionEvent e) {    
                main.setGrade(Spider.EASY);
                main.initCards();
                main.newGame();
            }
        });
        
        //“中級(jí)”
		jRMItemNormal.addActionListener(new java.awt.event.ActionListener() { 
            public void actionPerformed(java.awt.event.ActionEvent e) {    
                main.setGrade(Spider.NATURAL);
                main.initCards();
                main.newGame();
            }
        });

        //“高級(jí)”
		jRMItemHard.addActionListener(new java.awt.event.ActionListener() { 
            public void actionPerformed(java.awt.event.ActionEvent e) {    
                main.setGrade(Spider.HARD);
                main.initCards();
                main.newGame();
            }
        });

		jNewGame.addMenuListener(new javax.swing.event.MenuListener() { 
            public void menuSelected(javax.swing.event.MenuEvent e) {    
                if(main.getC() < 60){
                    jItemPlayAgain.setEnabled(true);
                }
                else{
                    jItemPlayAgain.setEnabled(false);
                }
            }
            public void menuDeselected(javax.swing.event.MenuEvent e) {} 
            public void menuCanceled(javax.swing.event.MenuEvent e) {} 
        });
        
        //“關(guān)于”
		jItemAbout.addActionListener(new java.awt.event.ActionListener() { 
            public void actionPerformed(java.awt.event.ActionEvent e) {    
                new AboutDialog();
            }
        });
    }

    /**
     **構(gòu)造線程:顯示可以執(zhí)行的操作
     */
    class Show extends Thread{
        public void run(){
            main.showEnableOperator();
        }
    }
 }

Spider.java 類

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

public class Spider extends JFrame{
	
	//整型變量,表示難度等級(jí)為:簡(jiǎn)單
	public static final int EASY = 1;
    //整型變量,表示難度等級(jí)為:普通
	public static final int NATURAL = 2;
    //整型變量,表示難度等級(jí)為:難
	public static final int HARD = 3;
    //設(shè)定初始難度等級(jí)為簡(jiǎn)單
	private int grade = Spider.EASY;
    private Container pane = null;
    //生成紙牌數(shù)組
	private PKCard cards[] = new PKCard[104];
    private JLabel clickLabel = null;
    private int c = 0;
    private int n = 0;
    private int a = 0;
    private int finish = 0;
    Hashtable table = null;
    private JLabel groundLabel[] = null;

    public static void main(String[] args){
        Spider spider = new Spider();
		spider.setVisible(true);
    }

    /**
     **構(gòu)造函數(shù)
     */
    public Spider(){
    	//改變系統(tǒng)默認(rèn)字體
		Font font = new Font("Dialog", Font.PLAIN, 12);
		java.util.Enumeration keys = UIManager.getDefaults().keys();
		while (keys.hasMoreElements()) {
			Object key = keys.nextElement();
			Object value = UIManager.get(key);
			if (value instanceof javax.swing.plaf.FontUIResource) {
				UIManager.put(key, font);
			}
		}
		setTitle("蜘蛛牌");
        setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
        //設(shè)置框架的大小
		setSize(1024, 742);
        
		//生成SpiderMenuBar對(duì)象,并放置在框架之上
		setJMenuBar(new SpiderMenuBar(this));
        pane = this.getContentPane();
        //設(shè)置背景顏色
		pane.setBackground(new Color(0, 112, 26));
        //將布局管理器設(shè)置成為null
		pane.setLayout(null);
        clickLabel = new JLabel();
        clickLabel.setBounds(883, 606, 121, 96);
        pane.add(clickLabel);
        
		clickLabel.addMouseListener(new MouseAdapter(){
            public void mouseReleased(MouseEvent me){
                if (c < 60){
					Spider.this.deal();
				}
            }
        });
        
		this.initCards();
        this.randomCards();
        this.setCardsLocation();
        groundLabel = new JLabel[10];
        
		int x = 20;
        for (int i = 0; i < 10; i++)
        {
            groundLabel[i] = new JLabel();
            groundLabel[i]
                    .setBorder(javax.swing.BorderFactory
                            .createEtchedBorder(javax.swing.border.EtchedBorder.RAISED));
            groundLabel[i].setBounds(x, 25, 71, 96);
            x += 101;
            this.pane.add(groundLabel[i]);
        }
        
		this.setVisible(true);
        this.deal();
        
		this.addKeyListener(new KeyAdapter(){
            class Show extends Thread{
                public void run(){
                    Spider.this.showEnableOperator();
                }
            }

            public void keyPressed(KeyEvent e){
                if (finish != 8) if (e.getKeyCode() == KeyEvent.VK_D && c < 60){
                    Spider.this.deal();
                }
                else if (e.getKeyCode() == KeyEvent.VK_M){
                    new Show().start();
                }
            }
        });
    }

    /**
	 **開(kāi)始新游戲
	 */
	public void newGame(){
        this.randomCards();
        this.setCardsLocation();
        this.setGroundLabelZOrder();
        this.deal();
    }

    /**
	 **返回值:int
	 **返回牌的數(shù)量
	  */
	public int getC(){
        return c;
    }

    /**
	 **設(shè)置等級(jí)
	 */
	public void setGrade(int grade){
        this.grade = grade;
    }

    /**
	 **紙牌初始化
	 */
	public void initCards(){
        //如果紙牌已被賦值,即將其從框架的面板中移去
		if (cards[0] != null){
            for (int i = 0; i < 104; i++){
                pane.remove(cards[i]);
            }
        }
        
		int n = 0;
        //通過(guò)難度等級(jí),為n賦值
		if (this.grade == Spider.EASY){
            n = 1;
        }
        else if (this.grade == Spider.NATURAL){
            n = 2;
        }
        else{
            n = 4;
        }
      //為card賦值  
		for (int i = 1; i <= 8; i++){
            for (int j = 1; j <= 13; j++){
                cards[(i - 1) * 13 + j - 1] = new PKCard((i % n + 1) + "-" + j,
                        this);
            }
        }
        
		//隨機(jī)紙牌初始化
		this.randomCards();
    }

    /**
	 **紙牌隨機(jī)分配
	 */
	public void randomCards(){
        PKCard temp = null;
        //隨機(jī)生成牌號(hào)
		for (int i = 0; i < 52; i++){
            int a = (int) (Math.random() * 104);
            int b = (int) (Math.random() * 104);
            temp = cards[a];
            cards[a] = cards[b];
            cards[b] = temp;
        }
    }

    /**
	 **設(shè)置還原
	 */
	public void setNA(){
        a = 0;
        n = 0;
    }

    /**
	 **設(shè)置紙牌的位置
	 */
	public void setCardsLocation(){
        table = new Hashtable();
        c = 0;
        finish = 0;
        n = 0;
        a = 0;
        int x = 883;
        int y = 580;
		//初始化待展開(kāi)的紙牌
        for (int i = 0; i < 6; i++){
            for (int j = 0; j < 10; j++){
                int n = i * 10 + j;
                pane.add(cards[n]);
                //將card轉(zhuǎn)向背面
				cards[n].turnRear();
                //將card放在固定的位置上
				cards[n].moveto(new Point(x, y));
                //將card的位置及相關(guān)信息存入
				table.put(new Point(x, y), cards[n]);
            }
            x += 10;
        }
		
		x = 20;
        y = 45;
        //初始化表面顯示的紙牌
		for (int i = 10; i > 5; i--){
            for (int j = 0; j < 10; j++){
                int n = i * 10 + j;
                if (n >= 104) continue;
                pane.add(cards[n]);
                cards[n].turnRear();
                cards[n].moveto(new Point(x, y));
                table.put(new Point(x, y), cards[n]);
                x += 101;
            }
            x = 20;
            y -= 5;
        }
    }

    /**
	 **返回值:void
	 **方法:顯示可移動(dòng)的操作
	 */
	public void showEnableOperator(){
        int x = 0;
        out: while (true){
            Point point = null;
            PKCard card = null;
            do{
                if (point != null){
					n++;
				}
                point = this.getLastCardLocation(n);
                while (point == null){
                    point = this.getLastCardLocation(++n);
                    if (n == 10) n = 0;
                    x++;
                    if (x == 10) break out;
                }
                card = (PKCard) this.table.get(point);
            }
            while (!card.isCardCanMove());
            while (this.getPreviousCard(card) != null
                    && this.getPreviousCard(card).isCardCanMove()){
                card = this.getPreviousCard(card);
            }
            if (a == 10){
				a = 0;
			}
            for (; a < 10; a++){
                if (a != n){
                    Point p = null;
                    PKCard c = null;
                    do{
                        if (p != null){
							a++;
						}
						p = this.getLastCardLocation(a);
                        int z = 0;
                        while (p == null){
                            p = this.getLastCardLocation(++a);
                            if (a == 10) a = 0;
                            if (a == n) a++;
                            z++;
                            if (z == 10) break out;
                        }
                        c = (PKCard) this.table.get(p);
                    }
                    while (!c.isCardCanMove());
                    if (c.getCardValue() == card.getCardValue() + 1){
                        card.flashCard(card);
                        try{
                            Thread.sleep(800);
                        }
                        catch (InterruptedException e){
                            e.printStackTrace();
                        }
                        c.flashCard(c);
                        a++;
                        if (a == 10){
							n++;
						}
                        break out;
                    }
                }
            }
            n++;
            if (n == 10){
				n = 0;
			}
            x++;
            if (x == 10){
				break out;
			}
        }
    }

    /*
	 **返回值:void
	 **方法:游戲運(yùn)行
	 */
	public void deal()
    {
        this.setNA();
        //判斷10列中是否空列
		for (int i = 0; i < 10; i++){
            if (this.getLastCardLocation(i) == null){
                JOptionPane.showMessageDialog(this, "有空位不能發(fā)牌!", "提示",
                        JOptionPane.WARNING_MESSAGE);
                return;
            }
        }
        int x = 20;
        
		for (int i = 0; i < 10; i++){
            Point lastPoint = this.getLastCardLocation(i);
            //這張牌應(yīng)“背面向上”
			if (c == 0){
                lastPoint.y += 5;
			}
            //這張牌應(yīng)“正面向上”
			else{
                lastPoint.y += 20;
			}
            
			table.remove(cards[c + i].getLocation());
            cards[c + i].moveto(lastPoint);
            table.put(new Point(lastPoint), cards[c + i]);
            cards[c + i].turnFront();
            cards[c + i].setCanMove(true);
            
			//將組件card移動(dòng)到容器中指定的順序索引。 
			this.pane.setComponentZOrder(cards[c + i], 1);
            
			Point point = new Point(lastPoint);
            if (cards[c + i].getCardValue() == 1){
                int n = cards[c + i].whichColumnAvailable(point);
                point.y -= 240;
                PKCard card = (PKCard) this.table.get(point);
                if (card != null && card.isCardCanMove()){
                    this.haveFinish(n);
                }
            }
            x += 101;
        }
        c += 10;
    }

	/*
	 **返回值:PKCard對(duì)象
	 **方法:獲得card上面的那張牌
	 */
	public PKCard getPreviousCard(PKCard card){
        Point point = new Point(card.getLocation());
        point.y -= 5;
        card = (PKCard) table.get(point);
        if (card != null){
			return card;
		}
        point.y -= 15;
        card = (PKCard) table.get(point);
        return card;
    }

    /**
	 **返回值:PKCard對(duì)象
	 **方法:取得card下面的一張牌
	 */
	public PKCard getNextCard(PKCard card){
        Point point = new Point(card.getLocation());
        point.y += 5;
        card = (PKCard) table.get(point);
        if (card != null)
			return card;
        point.y += 15;
        card = (PKCard) table.get(point);
        return card;
    }

    /**
	 **返回值:Point對(duì)象
	 **方法:取得第column列最后一張牌的位置
	 */
	public Point getLastCardLocation(int column){
        Point point = new Point(20 + column * 101, 25);
        PKCard card = (PKCard) this.table.get(point);
        if (card == null) return null;
        while (card != null){
            point = card.getLocation();
            card = this.getNextCard(card);
        }
        return point;
    }

    public Point getGroundLabelLocation(int column){
        return new Point(groundLabel[column].getLocation());
    }

    /*
	 **返回值:void
	 **方法:放置groundLable組件
	 */
	public void setGroundLabelZOrder(){
        for (int i = 0; i < 10; i++){
            //將組件groundLable移動(dòng)到容器中指定的順序索引。順序(105+i)確定了繪制組件的順序;具有最高順序的組件將第一個(gè)繪制,而具有最低順序的組件將最后一個(gè)繪制。在組件重疊的地方,具有較低順序的組件將覆蓋具有較高順序的組件。 
			pane.setComponentZOrder(groundLabel[i], 105 + i);
        }
    }

    /*
	 **返回值:void
	 **方法:判斷紙牌的擺放是否完成
	 */
	public void haveFinish(int column){
        Point point = this.getLastCardLocation(column);
        PKCard card = (PKCard) this.table.get(point);
        do{
            this.table.remove(point);
            card.moveto(new Point(20 + finish * 10, 580));
            //將組件移動(dòng)到容器中指定的順序索引。 
			pane.setComponentZOrder(card, 1);
            //將紙牌新的相關(guān)信息存入Hashtable
			this.table.put(card.getLocation(), card);
            card.setCanMove(false);
            point = this.getLastCardLocation(column);
            if (point == null)
                card = null;
            else
                card = (PKCard) this.table.get(point);
        }
        while (card != null && card.isCardCanMove());
        finish++;
        //如果8付牌全部組合成功,則顯示成功的對(duì)話框
		if (finish == 8){
            JOptionPane.showMessageDialog(this, "恭喜你,順利通過(guò)!", "成功",
                    JOptionPane.PLAIN_MESSAGE);
        }
        if (card != null){
            card.turnFront();
            card.setCanMove(true);
        }
    }
}

圖片資源這里就不方便貼出來(lái)了,想要源碼的直接點(diǎn)擊的鏈接地址

到此這篇關(guān)于基于Java實(shí)現(xiàn)經(jīng)典蜘蛛紙牌游戲的文章就介紹到這了,更多相關(guān)Java蜘蛛紙牌內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • java使用java.io.File類和java.nio.file.Path類對(duì)文件重命名

    java使用java.io.File類和java.nio.file.Path類對(duì)文件重命名

    這篇文章主要給大家介紹了關(guān)于java使用java.io.File類和java.nio.file.Path類對(duì)文件重命名的相關(guān)資料,本文僅為日常操作記錄,方便后期使用查找本地電腦文件太多了,又不想一個(gè)一個(gè)重命名,改名字什么的很麻煩,需要的朋友可以參考下
    2024-02-02
  • 淺談JSP是如何編譯成servlet并提供服務(wù)的

    淺談JSP是如何編譯成servlet并提供服務(wù)的

    JSP是Servlet的一種特殊形式,JSP頁(yè)面最終是編譯為Servlet執(zhí)行的,那么本文主要介紹了JSP是如何編譯成servlet并提供服務(wù)的,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-07-07
  • mybatis實(shí)現(xiàn)對(duì)數(shù)據(jù)的增刪查改實(shí)例詳解

    mybatis實(shí)現(xiàn)對(duì)數(shù)據(jù)的增刪查改實(shí)例詳解

    這篇文章主要介紹了mybatis實(shí)現(xiàn)對(duì)數(shù)據(jù)的增刪查改實(shí)例詳解的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2016-07-07
  • Java后端對(duì)接微信支付(小程序、APP、PC端掃碼)包含查單退款

    Java后端對(duì)接微信支付(小程序、APP、PC端掃碼)包含查單退款

    微信支付我們主要聚焦于這三種支付方式,其中JSPAI與APP主要與uniapp開(kāi)發(fā)微信小程序與APP對(duì)接,本文主要介紹了Java后端對(duì)接微信支付(小程序、APP、PC端掃碼)包含查單退款,具有一定的參考價(jià)值,感興趣的可以了解一下
    2021-12-12
  • 史上最難的一道Java面試題

    史上最難的一道Java面試題

    本文給大家分享一道史上最難的一道Java面試題,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧
    2018-03-03
  • SpringBoot使用AOP實(shí)現(xiàn)統(tǒng)一角色權(quán)限校驗(yàn)

    SpringBoot使用AOP實(shí)現(xiàn)統(tǒng)一角色權(quán)限校驗(yàn)

    這篇文章主要介紹了SpringBoot如何使用AOP實(shí)現(xiàn) 統(tǒng)一角色權(quán)限校驗(yàn),文中有詳細(xì)的代碼示例講解和操作流程,具有一定的參考價(jià)值,需要的朋友可以參考下
    2023-07-07
  • java中redis增刪查以及清理緩存的案例

    java中redis增刪查以及清理緩存的案例

    這篇文章主要介紹了java中redis增刪查以及清理緩存的案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-02-02
  • java+MongoDB實(shí)現(xiàn)存圖片、下載圖片的方法示例

    java+MongoDB實(shí)現(xiàn)存圖片、下載圖片的方法示例

    這篇文章主要介紹了java+MongoDB實(shí)現(xiàn)存圖片、下載圖片的方法,結(jié)合實(shí)例形式詳細(xì)分析了java結(jié)合MongoDB實(shí)現(xiàn)圖片的存儲(chǔ)與下載相關(guān)操作技巧,需要的朋友可以參考下
    2019-09-09
  • java instanceof操作符使用及原理解析

    java instanceof操作符使用及原理解析

    這篇文章主要介紹了java instanceof操作符使用及原理解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-12-12
  • springboot+mybatis+枚舉處理器的實(shí)現(xiàn)

    springboot+mybatis+枚舉處理器的實(shí)現(xiàn)

    在Spring?boot項(xiàng)目開(kāi)發(fā)中經(jīng)常遇到需要使用枚舉的場(chǎng)景,本文就介紹了springboot+mybatis+枚舉處理器的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-03-03

最新評(píng)論