Java版的掃雷游戲源碼


package com.xz.sl;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class Saolei extends JFrame{

    final int ROW = 20;
    final int COL = 20;
    final int LEI = 30;
    JButton[][] buttons = new JButton[ROW][COL];
    int[][] counts = new int[ROW][COL];
    final int LCODE = 11; //雷的編碼
    
    Container container = new Container();
    
    public Saolei() {
        init();
        
        
    }
    
    private void init() {
        JButton restBtn = new JButton("重來");
        restBtn.setOpaque(true);
        restBtn.setBackground(Color.PINK);
        restBtn.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                for(int i=0;i<ROW;i++) {
                    for(int j=0;j<COL;j++) {
                     buttons[i][j].setText("");
                     buttons[i][j].setEnabled(true);
                     buttons[i][j].setBackground(Color.YELLOW);
                     counts[i][j] = 0;
                     
                    }
                }
                mailei();
                jslsl();
            }
        });
        
        
        setLayout(new BorderLayout());
        add(restBtn,BorderLayout.NORTH);
        add(container,BorderLayout.CENTER);
        container.setLayout(new GridLayout(ROW,COL));
        for(int i=0;i<ROW;i++) {
            for(int j=0;j<COL;j++) {
                JButton button = new JButton();
                button.setMargin(new Insets(0, 0, 0, 0));
                button.setBackground(Color.YELLOW);
                button.setOpaque(true);
                buttons[i][j] = button;
                button.addActionListener(new ActionListener() {
                    
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        JButton button = (JButton) e.getSource();
                        if(button.equals(restBtn)) {
                            
                        }else {
                            int count = 0;
                            for(int i=0;i<ROW;i++) {
                                for(int j=0;j<COL;j++) {
                                    if(button.equals(buttons[i][j])) {
                                        count = counts[i][j];
                                        //踩到雷了
                                        if(count == LEI) {
                                            clcl();
                                        }else {
                                            openCell(i,j);
                                            checkWin();
                                        }
                                        return;
                                    }
                                }
                            }
                        }
                    }

                    private void checkWin() {
                        for(int i=0;i<ROW;i++) {
                            for(int j=0;j<COL;j++) {
                                //說明還沒有贏
                                if(buttons[i][j].isEnabled() == true && counts[i][j] != LEI) {
                                    return;
                                }
                            }
                        }
                        
                        JOptionPane.showMessageDialog(container, "恭喜您贏了...");
                        
                    }

                    private void openCell(int i,int j ) {
                        
                        //如果格子已經打開,直接返回
                        if(buttons[i][j].isEnabled() == false) {
                            return ;
                        }
                        
                        buttons[i][j].setText(counts[i][j]+"");
                        buttons[i][j].setEnabled(false);
                        buttons[i][j].setBackground(Color.CYAN);
                        
                        if(counts[i][j] == 0) {
                            //左上角
                            if(i > 0 && j > 0 && counts[i-1][j-1] != LEI) {
                                openCell(i-1, j-1);
                            }
                            
                            if(i > 0 && j > 0  && counts[i-1][j] != LEI) {
                                openCell(i-1, j);
                            }
                            
                            if(i > 0 && j < 19  && counts[i-1][j+1] != LEI) {
                                openCell(i-1, j+1);
                            }
                            
                            if(i > 0 && j > 0  && counts[i][j-1] != LEI) {
                                openCell(i, j-1);
                            }
                            
                            if(i > 0 && j < 19  && counts[i][j+1] != LEI) {
                                openCell(i, j+1);
                            }
                            
                            if(i < 19 && j > 0  && counts[i+1][j-1] != LEI) {
                                openCell(i+1, j-1);
                            }
                            
                            if(i < 19 && j > 0 && counts[i+1][j] != LEI) {
                                openCell(i+1, j);
                            }
                            
                            if(i < 19 && j < 19  && counts[i+1][j+1] != LEI) {
                                openCell(i+1, j+1);
                            }
                        }else {
                            buttons[i][j].setText(counts[i][j]+"");
                        }
                    }

                    
                });
                container.add(button);
            }
        }
        mailei();
        //計算周邊的雷的數量
        jslsl();
        
        setVisible(true);
        setTitle("掃雷游戲");
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(600,700);
    }

    private void jslsl() {
        for(int i=0;i<ROW;i++) {
            for(int j=0;j<COL;j++) {
                int count = 0;
                if(counts[i][j] == LEI) {
                    continue;
                }
                
                //左上角
                if(i > 0 && j > 0 && counts[i-1][j-1] == LEI) {
                    count++;
                }
                
                if(i > 0 && j > 0  && counts[i-1][j] == LEI) {
                    count++;
                }
                
                if(i > 0 && j < 19  && counts[i-1][j+1] == LEI) {
                    count++;
                }
                
                if(i > 0 && j > 0  && counts[i][j-1] == LEI) {
                    count++;
                }
                
                if(i > 0 && j < 19  && counts[i][j+1] == LEI) {
                    count++;
                }
                
                if(i < 19 && j > 0  && counts[i+1][j-1] == LEI) {
                    count++;
                }
                
                if(i < 19 && j > 0 && counts[i+1][j] == LEI) {
                    count++;
                }
                
                if(i < 19 && j < 19  && counts[i+1][j+1] == LEI) {
                    count++;
                }
                
                counts[i][j] = count;
//                buttons[i][j].setText(counts[i][j]+" ");
                
           }
        }
    }

    private void clcl() {
        for(int i=0;i<ROW;i++) {
            for(int j=0;j<COL;j++) {
                int c = counts[i][j];
                if(c == LEI) {
                    buttons[i][j].setText("X");
                    buttons[i][j].setBackground(Color.RED);
                    buttons[i][j].setEnabled(false);
                }else {
                    buttons[i][j].setText(c+"");
                    buttons[i][j].setEnabled(false);
                }
            }
        }
    }
    
    private void mailei() {
        Random random = new Random();
        int randRow,randCol;
        for(int i=0;i<LEI;i++) {
            randRow = random.nextInt(ROW);
            randCol = random.nextInt(COL);
            if(counts[randRow][randCol] == LEI) {
                i--;
            }else {
                counts[randRow][randCol] = LEI;
//                buttons[randRow][randCol].setText(LEI+"");
            }
        }
    }
    
    
    public static void main(String[] args) {
        new Saolei();
    }
    
}

以上代碼純屬練習用,沒有經過任何的封裝,有興趣的小伙伴可以自行封裝一下哦。

打賞

免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號  © 2018-2021 CODEPRJ.COM