如何在GUI Minesweeper游戏中更新剩余的地雷标签?

时间:2016-05-16 22:28:22

标签: java structure actionlistener minesweeper

我正在用Java制作扫雷游戏,我完成了游戏机制和视觉效果。但是,我试图找到一种方法来更新显示游戏中剩余的地雷的标签。当用户放置一个标志时,如果他们再次点击(有效地删除标志,无论是显示数字还是我的),计数应该减少一个,然后计数增加一。我有算法计算完成,但我找不到在程序中的ActionListener中调用它的方法,这需要完成,以便更新分数标签。

P.S。我基本上用高中CS课程和一些外部学习知识来创建这个扫雷游戏,所以这实际上可能是设计扫雷程序的一种可怕方式。关于设计等的一般编程技巧也将受到赞赏。

这里是程序的所有代码,分为四个类和一个接口:

主要方法类:

package minesweeper;

import javax.swing.JFrame;
import java.awt.Container;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.BorderLayout;


import java.awt.event.*;

public class mineBoard {

    static int bombsRemaining = 0;
    static int width = 600;
    static int height = 600;

    public static void main(String[]args){

        //  boolean gameOver = false;


            //instantiates all gui items
            JFrame frame = new JFrame();
            JButton newGameButton = new JButton("New Game");
            JPanel headingPanel = new JPanel();
            Container contentPane = new Container();
            JLabel bombsRemainLabel = new JLabel();

            //sets defaults of JFrame
            frame.setTitle("USA Minesweeper");
            frame.setSize(width,height);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true); 

            //sets defaults of container
            contentPane.setLayout(new BorderLayout());
            contentPane.setVisible(true);

            //adds the components to the heading panel
            headingPanel.add(newGameButton);
            headingPanel.add(bombsRemainLabel);
            contentPane.add(headingPanel, BorderLayout.PAGE_START);

            frame.add(contentPane);

            //function that occurs when the newGameButton is pressed; creates a fresh board
            newGameButton.addMouseListener(new MouseListener()
                {
                    public void mousePressed(MouseEvent e){

                            //removes previous bomb screen
                            contentPane.remove(contentPane.getComponentAt(width/2,height/2));

                            //generates new bomb screen
                            mineScreen screen = new mineScreen();

                            //calculates the total number of initial bombs
                            bombsRemaining = screen.countInitialBombs(screen.list);

                            //sets label for how many bombs remain
                            bombsRemainLabel.setText("Total Bombs: " + bombsRemaining);

                            //adds the new bomb screen to the container
                            contentPane.add(screen, BorderLayout.CENTER);
                            frame.add(contentPane);
                    }

                    //unused mouse methods
                    public void mouseEntered(MouseEvent e){}
                    public void mouseClicked(MouseEvent e){}
                    public void mouseExited(MouseEvent e){}
                    public void mouseReleased(MouseEvent e){}
                });

            //while(gameOver!=true){
            //  bombsRemaining = setBombsLabel(new mineScreen(), bombsRemaining);
            //  bombsRemainLabel.setText("Bombs Remaining: " + bombsRemaining);
            //}     
    }


    //detects when a button is pressed and deducts 1 from bombsRemaining; adds 1 if the button is pressed again
    /*public static int setBombsLabel(mineScreen screen, int bombsLeft){
        for(int i=0; i<screen.list.size(); i++){
            if (screen.list.get(i).getClicked() == 1){
                bombsLeft--;
                screen.list.get(i).setClicked(8);
            }
            if (screen.list.get(i).getClicked() == 9){
                bombsLeft++;
                screen.list.get(i).setClicked(10);
            }
        }
        return bombsLeft;

    }*/


}

主要小组类:

package minesweeper;


import java.awt.GridLayout;
import javax.swing.JPanel;
import java.util.Random;
import java.util.ArrayList;
import javax.swing.JButton;


public class mineScreen extends JPanel{

        private int w = 600;
        private int h = 600;
        int diffWidth = 16;
        int diffHeight = 16;
        Random gen = new Random();
        int bombChance = 0;
        int bombs = 0;
        mineButton testForMine = new mineButton();
        ArrayList<generalButton>list = new ArrayList<generalButton>(diffWidth*diffHeight);

        public mineScreen(){

            //creates the panel that holds the bomb grid
            setLayout(new GridLayout(diffWidth,diffHeight));

            //sets the analogous array list and fills bomb grid
            genBombs(list, diffWidth, diffHeight, bombs, gen);
            bombAmountSetter(list, diffWidth, diffHeight, testForMine, bombs);

            //adds the mine and nonMine buttons to the mineScreen panel
            addButtons(list, diffWidth, diffHeight);

            //sets the original revealed locations
            setOriginals(list, diffWidth, diffHeight, testForMine);

            //allows the finished bomb panel to be seen and adds it to the JFrame mineScreen
            setVisible(true);

        }

        //detects when a button is pressed and deducts 1 from bombsRemaining; adds 1 if the button is pressed again
        /*public static int setBombsLabel(ArrayList<generalButton>list, int bombsLeft){
            for(int i=0; i<list.size(); i++){
                if (list.get(i).getClicked() == 1){
                    bombsLeft--;
                    list.get(i).setClicked(8);
                }
                if (list.get(i).getClicked() == 9){
                    bombsLeft++;
                    list.get(i).setClicked(10);
                }
            }
            return bombsLeft;

        }*/

        public int countInitialBombs(ArrayList<generalButton>list){
            int BOMBS = 0;
            for (int i=0; i<list.size(); i++){
                if(list.get(i).getID() == 2){
                    BOMBS++;
                }
            }
            return BOMBS;
        }

        public static void genBombs(ArrayList<generalButton>list, int width, int height, int bombs, Random gen){
            for (int i=0; i<width*height; i++){
                if(gen.nextInt(5) == 4){
                    list.add(new mineButton());
                }else{
                    list.add(new nonMineButton(bombs));
                }
            }
        }

        public static void bombAmountSetter(ArrayList<generalButton>list, int width, int height, mineButton mine, int bombs){
            for (int i=0; i<width*height; i++){
                if(list.get(i).getID() == 1){
                    int total = setBombsNum(list, i, mine, bombs, width, height);
                    list.set(i, new nonMineButton(total));
                }
            }
        }

        public void addButtons(ArrayList<generalButton>list, int width, int height){
            for (int i=0; i<width*height; i++){
                this.add((JButton) list.get(i));
            }

        }

        public static void setOriginals(ArrayList<generalButton>list, int width, int height, mineButton mine){
            int y = (-width)-(width/3);
            while(y<width*3){
                for (int j = ((width*height)/2)-y; j<((width*height)/2)-y+(width/3); j++){
                    if(list.get(j).getID() == 1){
                        list.get(j).iconSetter();
                    }
                }
                y+=16;
            }   
        }
        public static int setBombsNum(ArrayList<generalButton>list, int i, mineButton mineB, int bTotal, int dWidth, int dHeight){

            if(i==0){
                bTotal = bombTopLeft(list,i,mineB,bTotal,dWidth);
            }
            else if(i>0 && i<dWidth-1){
                bTotal = bombTopRow(list,i,mineB,bTotal,dWidth);
            }
            else if(i==dWidth-1){
                bTotal = bombTopRight(list,i,mineB,bTotal,dWidth);
            }
            else if(i==(dWidth*dHeight)-dWidth){
                bTotal = bombBottomLeft(list,i,mineB,bTotal,dWidth);
            }
            else if(i>(dWidth*dHeight)-dWidth && i<(dWidth*dHeight)-1){
                bTotal = bombBottomRow(list,i,mineB,bTotal,dWidth);
            }
            else if(i==(dWidth*dHeight)-1){
                bTotal = bombBottomRight(list,i,mineB,bTotal,dWidth);
            }
            else if(i==dWidth||i==dWidth*2||i==dWidth*3||i==dWidth*4
                    ||i==dWidth*5||i==dWidth*6||i==dWidth*7||i==dWidth*8
                    ||i==dWidth*9||i==dWidth*10||i==dWidth*11||i==dWidth*12
                    ||i==dWidth*13||i==dWidth*14||i==dWidth*15){
                bTotal = bombLeftCol(list,i,mineB,bTotal,dWidth);
            }
            else if(i==((dWidth*2)-1)||i==((dWidth*3)-1)||i==((dWidth*4)-1)
                    ||i==((dWidth*5)-1)||i==((dWidth*6)-1)||i==((dWidth*7)-1)
                    ||i==((dWidth*8)-1)||i==((dWidth*9)-1)||i==((dWidth*10)-1)
                    ||i==((dWidth*11)-1)||i==((dWidth*12)-1)||i==((dWidth*13)-1)
                    ||i==((dWidth*14)-1)||i==((dWidth*15)-1)||i==((dWidth*16)-1)){
                bTotal = bombRightCol(list,i,mineB,bTotal,dWidth);
            }
            else{
                bTotal = bombElsewhere(list,i,mineB,bTotal,dWidth);
            }
            return bTotal;

        }


        /*public static int setBombsNum(ArrayList<generalButton>list, int i, mineButton mineB, int bTotal, int dWidth){

            if(i==0){
                bTotal = bombTopLeft(list,i,mineB,bTotal,dWidth);
            }
            else if(i>0 && i<15){
                bTotal = bombTopRow(list,i,mineB,bTotal,dWidth);
            }
            else if(i==15){
                bTotal = bombTopRight(list,i,mineB,bTotal,dWidth);
            }
            else if(i==240){
                bTotal = bombBottomLeft(list,i,mineB,bTotal,dWidth);
            }
            else if(i>240 && i<255){
                bTotal = bombBottomRow(list,i,mineB,bTotal,dWidth);
            }
            else if(i==255){
                bTotal = bombBottomRight(list,i,mineB,bTotal,dWidth);
            }
            else if(i==16||i==32||i==48||i==64||i==80||i==96||i==112||i==128||i==144||i==160||i==176||i==192||i==208||i==224||i==240){
                bTotal = bombLeftCol(list,i,mineB,bTotal,dWidth);
            }
            else if(i==31||i==47||i==63||i==79||i==95||i==111||i==127||i==143||i==159||i==175||i==191||i==207||i==223||i==239||i==255){
                bTotal = bombRightCol(list,i,mineB,bTotal,dWidth);
            }
            else{
                bTotal = bombElsewhere(list,i,mineB,bTotal,dWidth);
            }
            return bTotal;

        }*/
        public static int bombTopLeft(ArrayList<generalButton>list, int i, mineButton mineB, int bTotal, int dWidth){
            if (list.get(i+1).getID() == 2){
                bTotal = bTotal + 1;
            }
            if (list.get(i+dWidth).getID() == 2){
                bTotal = bTotal + 1;
            }
            if (list.get(i+dWidth+1).getID() == 2){
                bTotal = bTotal + 1;
            }
            return bTotal;
        }
        public static int bombTopRight(ArrayList<generalButton>list, int i, mineButton mineB, int bTotal, int dWidth){
            if (list.get(i-1).getID() == 2){
                bTotal = bTotal + 1;
            }
            if (list.get(i+dWidth).getID() == 2){
                bTotal = bTotal + 1;
            }
            if (list.get(i+dWidth-1).getID() == 2){
                bTotal = bTotal + 1;
            }
            return bTotal;
        }
        public static int bombTopRow(ArrayList<generalButton>list, int i, mineButton mineB, int bTotal, int dWidth){
            if (list.get(i+1).getID() == 2){
                bTotal = bTotal + 1;
            }
            if (list.get(i-1).getID() == 2){
                bTotal = bTotal + 1;
            }
            if (list.get(i+dWidth).getID() == 2){
                bTotal = bTotal + 1;
            }
            if (list.get(i+dWidth+1).getID() == 2){
                bTotal = bTotal + 1;
            }
            if (list.get(i+dWidth-1).getID() == 2){
                bTotal = bTotal + 1;
            }
            return bTotal;
        }
        public static int bombBottomRight(ArrayList<generalButton>list, int i, mineButton mineB, int bTotal, int dWidth){
            if (list.get(i-1).getID() == 2){
                bTotal = bTotal + 1;
            }
            if (list.get(i-dWidth).getID() == 2){
                bTotal = bTotal + 1;
            }
            if (list.get(i-dWidth-1).getID() == 2){
                bTotal = bTotal + 1;
            }
            return bTotal;
        }
        public static int bombBottomLeft(ArrayList<generalButton>list, int i, mineButton mineB, int bTotal, int dWidth){
            if (list.get(i+1).getID() == 2){
                bTotal = bTotal + 1;
            }
            if (list.get(i-dWidth).getID() == 2){
                bTotal = bTotal + 1;
            }
            if (list.get(i-dWidth+1).getID() == 2){
                bTotal = bTotal + 1;
            }
            return bTotal;
        }
        public static int bombBottomRow(ArrayList<generalButton>list, int i, mineButton mineB, int bTotal, int dWidth){
            if (list.get(i+1).getID() == 2){
                bTotal = bTotal + 1;
            }
            if (list.get(i-1).getID() == 2){
                bTotal = bTotal + 1;
            }
            if (list.get(i-dWidth).getID() == 2){
                bTotal = bTotal + 1;
            }
            if (list.get(i-dWidth+1).getID() == 2){
                bTotal = bTotal + 1;
            }
            if (list.get(i-dWidth-1).getID() == 2){
                bTotal = bTotal + 1;
            }
            return bTotal;
        }
        public static int bombLeftCol(ArrayList<generalButton>list, int i, mineButton mineB, int bTotal, int dWidth){
            if (list.get(i+1).getID() == 2){
                bTotal = bTotal + 1;
            }
            if (list.get(i+dWidth).getID() == 2){
                bTotal = bTotal + 1;
            }
            if (list.get(i+dWidth+1).getID() == 2){
                bTotal = bTotal + 1;
            }
            if (list.get(i-dWidth).getID() == 2){
                bTotal = bTotal + 1;
            }
            if (list.get(i-dWidth+1).getID() == 2){
                bTotal = bTotal + 1;
            }
            return bTotal;
        }
        public static int bombRightCol(ArrayList<generalButton>list, int i, mineButton mineB, int bTotal, int dWidth){
            if (list.get(i-1).getID() == 2){
                bTotal = bTotal + 1;
            }
            if (list.get(i+dWidth).getID() == 2){
                bTotal = bTotal + 1;
            }
            if (list.get(i+dWidth-1).getID() == 2){
                bTotal = bTotal + 1;
            }
            if (list.get(i-dWidth).getID() == 2){
                bTotal = bTotal + 1;
            }
            if (list.get(i-dWidth-1).getID() == 2){
                bTotal = bTotal + 1;
            }
            return bTotal;
        }
        public static int bombElsewhere(ArrayList<generalButton>list, int i, mineButton mineB, int bTotal, int dWidth){
            if (list.get(i+1).getID() == 2){
                bTotal = bTotal + 1;
            }
            if (list.get(i-1).getID() == 2){
                bTotal = bTotal + 1;
            }
            if (list.get(i+dWidth).getID() == 2){
                bTotal = bTotal + 1;
            }
            if (list.get(i-dWidth).getID() == 2){
                bTotal = bTotal + 1;
            }
            if (list.get(i+dWidth+1).getID() == 2){
                bTotal = bTotal + 1;
            }
            if (list.get(i+dWidth-1).getID() == 2){
                bTotal = bTotal + 1;
            }
            if (list.get(i-dWidth+1).getID() == 2){
                bTotal = bTotal + 1;
            }
            if (list.get(i-dWidth-1).getID() == 2){
                bTotal = bTotal + 1;
            }
            return bTotal;
        }


        public int getHeight(){
            return h;
        }
        public int getWidth(){
            return w;
        }
}

我的按钮类:

package minesweeper;


import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import javax.swing.ImageIcon;
import javax.swing.JButton;



public class mineButton extends JButton implements MouseListener, generalButton{

    ImageIcon bombIcon, flag;
    int clickCount = 1;
//  int clicked = 0;

    public mineButton(){
        bombIcon = new ImageIcon("C:\\Users\\User\\Desktop\\ECLIPSE_JAVA\\minesweeper_image_files\\mineSweepBomb.png");
        flag = new ImageIcon("C:\\Users\\User\\Desktop\\ECLIPSE_JAVA\\minesweeper_image_files\\flag.jpg");
        setVisible(true);
        addMouseListener(this);


    }
    /*public void setClicked(int c){
        clicked = c;
    }
    public int getClicked(){
        return clicked;
    }*/
    public int getID(){
        return 2;
    }
    public int iconSetter(){
        return 1;
    }
    public void mousePressed(MouseEvent e){
        if (clickCount == 1){
            setIcon(flag);
        //  clicked++;
        }
        if (clickCount == 2)
            setIcon(bombIcon);
        clickCount++;
    }


    //unused mouse methods
    public void mouseEntered(MouseEvent e){}
    public void mouseClicked(MouseEvent e){}
    public void mouseExited(MouseEvent e){}
    public void mouseReleased(MouseEvent e){}


}

非矿井按钮类:

package minesweeper;

import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import javax.swing.ImageIcon;
import javax.swing.JButton;



public class nonMineButton extends JButton implements MouseListener, generalButton{

        ImageIcon flag, numZero, numOne, numTwo, numThree, numFour, numFive, numSix, numSeven, numEight;
        int numBombs = 0;
        int clickCount = 1;
    //  int clicked = 0;

        public nonMineButton(int bombTotal){
            numZero = new ImageIcon("C:\\Users\\User\\Desktop\\ECLIPSE_JAVA\\minesweeper_image_files\\numZero.jpg");
            numOne = new ImageIcon("C:\\Users\\User\\Desktop\\ECLIPSE_JAVA\\minesweeper_image_files\\mineSweepOne.png");
            numTwo = new ImageIcon("C:\\Users\\User\\Desktop\\ECLIPSE_JAVA\\minesweeper_image_files\\mineSweepTwo.png");
            numThree = new ImageIcon("C:\\Users\\User\\Desktop\\ECLIPSE_JAVA\\minesweeper_image_files\\mineSweepThree.png");
            numFour = new ImageIcon("C:\\Users\\User\\Desktop\\ECLIPSE_JAVA\\minesweeper_image_files\\mineSweepFour.png");
            numFive = new ImageIcon("C:\\Users\\User\\Desktop\\ECLIPSE_JAVA\\minesweeper_image_files\\mineSweepFive.png");
            numSix = new ImageIcon("C:\\Users\\User\\Desktop\\ECLIPSE_JAVA\\minesweeper_image_files\\mineSweepSix.png");
            numSeven = new ImageIcon("C:\\Users\\User\\Desktop\\ECLIPSE_JAVA\\minesweeper_image_files\\mineSweepSeven.png");
            numEight = new ImageIcon("C:\\Users\\User\\Desktop\\ECLIPSE_JAVA\\minesweeper_image_files\\mineSweepEight.png");
            numBombs = bombTotal;
            flag = new ImageIcon("C:\\Users\\User\\Desktop\\ECLIPSE_JAVA\\minesweeper_image_files\\flag.jpg");
            setVisible(true);
            this.addMouseListener(this);
        }
    /*  public void setClicked(int c){
            clicked = c;
        }
        public int getClicked(){
            return clicked;
        }*/
        public int getID(){
            return 1;
        }

        public void mousePressed(MouseEvent e){
            if (clickCount == 1){
                setIcon(flag);
            //  clicked++;
            }
            if (clickCount == 2){
                iconSetter();
            //  clicked++;
            }
            clickCount++;
        }

        public int iconSetter(){
            if(numBombs == 0){
                setIcon(numZero);
            }
            if(numBombs == 1){
                setIcon(numOne);
            }
            if(numBombs == 2){
                setIcon(numTwo);
            }
            if(numBombs == 3){
                setIcon(numThree);
            }
            if(numBombs == 4){
                setIcon(numFour);
            }
            if(numBombs == 5){
                setIcon(numFive);
            }
            if(numBombs == 6){
                setIcon(numSix);
            }
            if(numBombs == 7){
                setIcon(numSeven);
            }
            if(numBombs == 8){
                setIcon(numEight);
            }
            return 1;
        }

        //unused mouse methods
        public void mouseClicked(MouseEvent e){}
        public void mouseEntered(MouseEvent e){}
        public void mouseExited(MouseEvent e){}
        public void mouseReleased(MouseEvent e){}
}

接口:

package minesweeper;

interface generalButton{
    int getID();
//  int getClicked();
//  void setClicked(int c);
    int iconSetter();
}

0 个答案:

没有答案