【问题标题】:Carrying over static variable [duplicate]继承静态变量[重复]
【发布时间】:2017-01-10 02:38:08
【问题描述】:

我正在尝试设置扫雷游戏,但是在将新的 'v' int 转移到棋盘文件中时遇到问题。下面是我做过的事情的列表。希望我下面说的有道理,原始的扫雷游戏基础游戏代码可以在底部找到。

  • 制作了一个包含以下内容的 Easy.java 文件:

    import java.util.Scanner;
    
    public class Easy{
        public static int b;
    
        public static void setVariable(int s)
        {
            b = s;
        }
    
        public static int getVariable()
        {
            return b;
        }  
    }
    
  • 在 Mines.java 的顶部添加了一个公共静态 int(在 private final int FRAME_HEIGHT = 290; 下)

  • 使用 Mines.java 文件的公共 void 运行添加到 main 方法:

    System.out.println("Please enter number for difficulty");
    System.out.println("1: Easy 2: Medium 3: Hard");
    Scanner kb = new Scanner(System.in);
    int difficulty = kb.nextInt();
    System.out.println("Difficulty set to: " + difficulty);                
    if(difficulty==1){
        Easy.setVariable(40);
        v = Easy.getVariable();
        System.out.println(v);
        System.out.println("Easy mode chosen");
    }
    
  • 我只是不确定如何链接 Board.java 文件以获取我尝试过的“v”的值

    int N_MINES = Mines.v;
    

但是,它没有看到我在上面的代码中设置的任何值,但是如果我将 v 设置为等于一个数字,它就会看到这个。在我之前执行“System.out.println(v);”的代码中它打印出正确的变量,但是 Board.java 没有看到这个数字。我该怎么办?

Board.java:

import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import java.util.Random;
import java.util.*;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Board extends JPanel {

    private final int NUM_IMAGES = 13;
    private final int CELL_SIZE = 15;

    private final int COVER_FOR_CELL = 10;
    private final int MARK_FOR_CELL = 10;
    private final int EMPTY_CELL = 0;
    private final int MINE_CELL = 9;
    private final int COVERED_MINE_CELL = MINE_CELL + COVER_FOR_CELL;
    private final int MARKED_MINE_CELL = COVERED_MINE_CELL + MARK_FOR_CELL;

    private final int DRAW_MINE = 9;
    private final int DRAW_COVER = 10;
    private final int DRAW_MARK = 11;
    private final int DRAW_WRONG_MARK = 12;

    int N_MINES = Mines.v;

    private int N_ROWS = 16;
    private int N_COLS = 16;

    private int[] field;
    private boolean inGame;
    private int mines_left;
    private Image[] img;

    private int all_cells;
    private JLabel statusbar;


    public Board(JLabel statusbar) {

        this.statusbar = statusbar;

        img = new Image[NUM_IMAGES];

        for (int i = 0; i < NUM_IMAGES; i++) {
            img[i] = (new ImageIcon(i + ".png")).getImage();
        }

        setDoubleBuffered(true);

        addMouseListener(new MinesAdapter());
        newGame();
    }

    private void newGame() {

        Random random;
        int current_col;

        int i = 0;
        int position = 0;
        int cell = 0;
        /*
        System.out.println("Please enter number for difficulty");
        System.out.println("1: Easy 2: Medium 3: Hard");
        Scanner kb = new Scanner(System.in);
        int difficulty = kb.nextInt();
        System.out.println("Difficulty set to: " + difficulty);                

        if(difficulty==1){
           N_MINES=N_MINES*1;
           System.out.println("Easy mode chosen");
        }

        if(difficulty==2){
           N_MINES=N_MINES*2;
           System.out.println("Medium mode chosen");
        }

        if(difficulty==3){
           N_MINES=N_MINES*3;
           System.out.println("Hard mode chosen");
        }        
        */
        random = new Random();
        inGame = true;
        mines_left = N_MINES;

        all_cells = N_ROWS * N_COLS;
        field = new int[all_cells];

        for (i = 0; i < all_cells; i++)
            field[i] = COVER_FOR_CELL;

        statusbar.setText(Integer.toString(mines_left));

        i = 0;
        while (i < N_MINES) {
            position = (int) (all_cells * random.nextDouble());

            if ((position < all_cells) &&
                (field[position] != COVERED_MINE_CELL)) {


                current_col = position % N_COLS;
                field[position] = COVERED_MINE_CELL;
                i++;

                if (current_col > 0) { 
                    cell = position - 1 - N_COLS;
                    if (cell >= 0)
                        if (field[cell] != COVERED_MINE_CELL)
                            field[cell] += 1;
                    cell = position - 1;
                    if (cell >= 0)
                        if (field[cell] != COVERED_MINE_CELL)
                            field[cell] += 1;

                    cell = position + N_COLS - 1;
                    if (cell < all_cells)
                        if (field[cell] != COVERED_MINE_CELL)
                            field[cell] += 1;
                }

                cell = position - N_COLS;
                if (cell >= 0)
                    if (field[cell] != COVERED_MINE_CELL)
                        field[cell] += 1;
                cell = position + N_COLS;
                if (cell < all_cells)
                    if (field[cell] != COVERED_MINE_CELL)
                        field[cell] += 1;

                if (current_col < (N_COLS - 1)) {
                    cell = position - N_COLS + 1;
                    if (cell >= 0)
                        if (field[cell] != COVERED_MINE_CELL)
                            field[cell] += 1;
                    cell = position + N_COLS + 1;
                    if (cell < all_cells)
                        if (field[cell] != COVERED_MINE_CELL)
                            field[cell] += 1;
                    cell = position + 1;
                    if (cell < all_cells)
                        if (field[cell] != COVERED_MINE_CELL)
                            field[cell] += 1;
                }
            }
        }
    }

    public void find_empty_cells(int j) {

        int current_col = j % N_COLS;
        int cell;

        if (current_col > 0) { 
            cell = j - N_COLS - 1;
            if (cell >= 0)
                if (field[cell] > MINE_CELL) {
                    field[cell] -= COVER_FOR_CELL;
                    if (field[cell] == EMPTY_CELL)
                        find_empty_cells(cell);
                }

            cell = j - 1;
            if (cell >= 0)
                if (field[cell] > MINE_CELL) {
                    field[cell] -= COVER_FOR_CELL;
                    if (field[cell] == EMPTY_CELL)
                        find_empty_cells(cell);
                }

            cell = j + N_COLS - 1;
            if (cell < all_cells)
                if (field[cell] > MINE_CELL) {
                    field[cell] -= COVER_FOR_CELL;
                    if (field[cell] == EMPTY_CELL)
                        find_empty_cells(cell);
                }
        }

        cell = j - N_COLS;
        if (cell >= 0)
            if (field[cell] > MINE_CELL) {
                field[cell] -= COVER_FOR_CELL;
                if (field[cell] == EMPTY_CELL)
                    find_empty_cells(cell);
            }

        cell = j + N_COLS;
        if (cell < all_cells)
            if (field[cell] > MINE_CELL) {
                field[cell] -= COVER_FOR_CELL;
                if (field[cell] == EMPTY_CELL)
                    find_empty_cells(cell);
            }

        if (current_col < (N_COLS - 1)) {
            cell = j - N_COLS + 1;
            if (cell >= 0)
                if (field[cell] > MINE_CELL) {
                    field[cell] -= COVER_FOR_CELL;
                    if (field[cell] == EMPTY_CELL)
                        find_empty_cells(cell);
                }

            cell = j + N_COLS + 1;
            if (cell < all_cells)
                if (field[cell] > MINE_CELL) {
                    field[cell] -= COVER_FOR_CELL;
                    if (field[cell] == EMPTY_CELL)
                        find_empty_cells(cell);
                }

            cell = j + 1;
            if (cell < all_cells)
                if (field[cell] > MINE_CELL) {
                    field[cell] -= COVER_FOR_CELL;
                    if (field[cell] == EMPTY_CELL)
                        find_empty_cells(cell);
                }
        }
    }

    @Override
    public void paintComponent(Graphics g) {

        int cell = 0;
        int uncover = 0;

        for (int i = 0; i < N_ROWS; i++) {
            for (int j = 0; j < N_COLS; j++) {

                cell = field[(i * N_COLS) + j];

                if (inGame && cell == MINE_CELL)
                    inGame = false;

                if (!inGame) {
                    if (cell == COVERED_MINE_CELL) {
                        cell = DRAW_MINE;
                    } else if (cell == MARKED_MINE_CELL) {
                        cell = DRAW_MARK;
                    } else if (cell > COVERED_MINE_CELL) {
                        cell = DRAW_WRONG_MARK;
                    } else if (cell > MINE_CELL) {
                        cell = DRAW_COVER;
                    }


                } else {
                    if (cell > COVERED_MINE_CELL)
                        cell = DRAW_MARK;
                    else if (cell > MINE_CELL) {
                        cell = DRAW_COVER;
                        uncover++;
                    }
                }

                g.drawImage(img[cell], (j * CELL_SIZE),
                    (i * CELL_SIZE), this);
            }
        }

        if (uncover == 0 && inGame) {
            inGame = false;
            statusbar.setText("Game won");
        } else if (!inGame)
            statusbar.setText("Game lost");
    }

    class MinesAdapter extends MouseAdapter {

        @Override
        public void mousePressed(MouseEvent e) {

            int x = e.getX();
            int y = e.getY();

            int cCol = x / CELL_SIZE;
            int cRow = y / CELL_SIZE;

            boolean rep = false;


            if (!inGame) {
                newGame();
                repaint();
            }


            if ((x < N_COLS * CELL_SIZE) && (y < N_ROWS * CELL_SIZE)) {

                if (e.getButton() == MouseEvent.BUTTON3) {

                    if (field[(cRow * N_COLS) + cCol] > MINE_CELL) {
                        rep = true;

                        if (field[(cRow * N_COLS) + cCol] <= COVERED_MINE_CELL) {
                            if (mines_left > 0) {
                                field[(cRow * N_COLS) + cCol] += MARK_FOR_CELL;
                                mines_left--;
                                statusbar.setText(Integer.toString(mines_left));
                            } else
                                statusbar.setText("No marks left");
                        } else {

                            field[(cRow * N_COLS) + cCol] -= MARK_FOR_CELL;
                            mines_left++;
                            statusbar.setText(Integer.toString(mines_left));
                        }
                    }

                } else {

                    if (field[(cRow * N_COLS) + cCol] > COVERED_MINE_CELL) {
                        return;
                    }

                    if ((field[(cRow * N_COLS) + cCol] > MINE_CELL) &&
                        (field[(cRow * N_COLS) + cCol] < MARKED_MINE_CELL)) {

                        field[(cRow * N_COLS) + cCol] -= COVER_FOR_CELL;
                        rep = true;

                        if (field[(cRow * N_COLS) + cCol] == MINE_CELL)
                            inGame = false;
                        if (field[(cRow * N_COLS) + cCol] == EMPTY_CELL)
                            find_empty_cells((cRow * N_COLS) + cCol);
                    }
                }

                if (rep)
                    repaint();
            }
        }
    }
}

Mines.java:

import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import java.util.*;

public class Mines extends JFrame {

    private final int FRAME_WIDTH = 250;
    private final int FRAME_HEIGHT = 290;
    public static int v;

    private final JLabel statusbar;

    public Mines() {

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(FRAME_WIDTH, FRAME_HEIGHT);
        setLocationRelativeTo(null);
        setTitle("Minesweeper");

        statusbar = new JLabel("");
        add(statusbar, BorderLayout.SOUTH);

        add(new Board(statusbar));

        setResizable(false);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {                
                JFrame ex = new Mines();
                ex.setVisible(true);

                System.out.println("Please enter number for difficulty");
                System.out.println("1: Easy 2: Medium 3: Hard");
                Scanner kb = new Scanner(System.in);
                int difficulty = kb.nextInt();
                System.out.println("Difficulty set to: " + difficulty);                

                if(difficulty==1){
                  Easy.setVariable(40);
                  v = Easy.getVariable();
                  System.out.println(v);
                  System.out.println("Easy mode chosen");
                }
                if(difficulty==2){
                  //Medium.setVariable(80);
                  //v = Medium.getVariable();
                  //System.out.println(v);
                  System.out.println("Medium mode chosen");                  
                  //Medium.main(args);
                }     
                if(difficulty==3){
                  Hard.main(args);
                } 
            }
        });
    }
}

【问题讨论】:

  • 您的静态变量是公开的,因此不需要 setter 或 getter 方法
  • 事实上,我看不出那个 Easy 类的目的......请出示 board 类。
  • @cricket_007 棋盘类太大,无法粘贴。如果单击链接,第一个 java 代码块是板类
  • 我在问题中看到过更长的代码。我觉得挺合适的至少一个minimal reproducible example
  • @cricket_007 如果我删除了 setter 和 getter 方法,我对如何处理它有点困惑。我可以将其保留为“ public static int b; ”

标签: java


【解决方案1】:

编辑 Board 类的构造函数以接受地雷的数量。

private int n_mines = 40; // default 

public Board(int mines, JLabel statusbar) {
    this.n_mines = mines;
    this.statusbar = statusbar;

制作new Board(20, new JLabel()); 让游戏更轻松

【讨论】:

  • 这样做可行,但我的困境是在这种情况下根据用户选择的内容(如 Mines.java 中所示)代码块将 20 更改为不同的值
  • 在移动了一些代码之后,我已经让它按照我需要的方式工作。谢谢!
  • 你应该选择不同的难度,然后用不同数量的地雷制作一个新的 Board 对象
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-10
  • 2019-04-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-07
相关资源
最近更新 更多