OK 버튼을 클릭 한 후 메시지 대화 상자가 다시 나타납니다
-
18-09-2019 - |
문제
이전에 좋은 도움을받은 후, 나는 광산 저 게임을 완료했습니다. 그러나 내가 알 수없는 한 가지 문제가 있습니다. 게임 자체는 잘 작동하지만 사용자가 난이도 설정 (초보자, 중간, 고급)을 변경하여 슬픔을주는 옵션이 있습니다. 사용자는 jmenubar에서 "게임"을 선택한 다음 드롭 다운 메뉴에서 "난이도 변경"을 선택합니다. 이를 위해서는 Jradiobuttons를 사용하여 사용자가 4 가지 옵션 중 하나를 선택할 수 있도록 JoptionMessagebox를 엽니 다. 그러나 4 번의 사용자 정의 - 아직 구현하지는 않았다). 대화 상자는 사용자가 난이도 모드를 처음으로 변경할 때 잘 작동하는 것 같습니다. 그러나 사용자가 두 번째 이상의 난이도를 변경하려고 시도하면 게임은 설정을 구현 한 다음 대화 상자가 다시 나타나서 사용자에게 어려움을 다시 선택하도록 촉구합니다. 이것은 때때로 떠나기 전에 두 번 반복되며, 다른 시간에는 최대 4 ~ 5 번까지 반복됩니다. 왜 이런 일이 일어나고 있는지 모르기 때문에 어떤 도움을 주셔서 감사합니다.
또한 난이도 설정이 어떻게 변화하는지 개선 할 수있는 방법에 대한 조언에 감사드립니다. 이 코드는 JPanel과 Minefield (버튼으로 만들어짐)를 제거하고 새로운 난이도 설정으로 버튼/Japanel을 다시 인스턴스화합니다. 이것을 더 효율적으로 만들 수있는 방법이 있습니까?
논리 클래스와 GUI 유리가 있습니다. 위의 문제는 모두 GUI 클래스에 있습니다.
private void selectDifficulty() {
group.add(b1);
group.add(b2);
group.add(b3);
group.add(b4);
b1.setSelected(true);
Object[] array = {new JLabel("Select Difficulty"),b1,b2,b3,b4};
JOptionPane.showMessageDialog(null, array);
if (b1.isSelected()) {
if (mode == 1){
rw = 9;
rh = 9;
mode = 1;
}else if(mode == 2){
rw = 15;
rh = 15;
mode = 1;
}else {
rw = 20;
rh = 20;
mode = 1;
}
start(9,9);
}else if (b2.isSelected()) {
if (mode == 1){
rw = 9;
rh = 9;
mode = 2;
}else if(mode == 2){
rw = 15;
rh = 15;
mode = 2;
}else {
rw = 20;
rh = 20;
mode = 2;
}
start(15,15);
}else if (b3.isSelected()) {
if (mode == 1){
rw = 9;
rh = 9;
mode = 3;
}else if(mode == 2){
rw = 15;
rh = 15;
mode = 3;
}else {
rw = 20;
rh = 20;
mode = 3;
}
start(20,20);
}
}
완전한 코드 : GUI 클래스
import java.awt.* ;
import java.awt.event.* ;
import javax.swing.* ;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
public class MineSweeperGUI extends JFrame implements MouseListener {
MinesweeperLogic logicClass = new MinesweeperLogic();
JButton newGameButton = new JButton ("New Game");
JMenuItem optionsButton = new JMenuItem ("Change Difficulty");
JRadioButton b1 = new JRadioButton ("Beginner: 9 X 9 Grid (10 Mines)");
JRadioButton b2 = new JRadioButton ("Intermediate: 15 X 15 Grid (36 Mines)");
JRadioButton b3 = new JRadioButton ("Advanced: 20 X 20 Grid (80 Mines)");
JRadioButton b4 = new JRadioButton ("Custom");
ButtonGroup group = new ButtonGroup();
JMenuItem aboutButton = new JMenuItem ("About Minesweeper");
JPanel p = new JPanel();
JPanel p2 = new JPanel();
JPanel p3 = new JPanel();
int w, h, rw = 0, rh = 0, mode = 1;
public void MineSweeper(int width, int height) {
//setupI();
w = width;
h = height;
logicClass.startNewGame(w, h);
GridLayout layout = new GridLayout (w, h);
p2.setLayout(new BorderLayout());
p.setLayout(layout);
p2.add(p, BorderLayout.CENTER);
for(int x = 0 ; x < w ; x++) {
for(int y = 0 ; y < h ; y++) {
logicClass.label[x][y] = new Button();
logicClass.label[x][y].setPreferredSize(new Dimension(20,20));
logicClass.label[x][y].setBackground(new Color(33,58,156));
logicClass.label[x][y].addMouseListener (this);
p.add(logicClass.label[x][y]);
}
}
JMenuBar mb = new JMenuBar();
JMenu m = new JMenu("Game");
JMenu m2 = new JMenu("Help");
m.add(optionsButton);
m2.add(aboutButton);
mb.add(m);
mb.add(m2);
p2.add(p3, BorderLayout.PAGE_START);
newGameButton.setPreferredSize (new Dimension(87, 20));
newGameButton.setFont(new Font("sansserif",Font.BOLD,11));
newGameButton.setForeground(Color.black);
newGameButton.setBackground(new Color(235,52,52));
Border thickBorder = new LineBorder(Color.black, 2);
newGameButton.setBorder(thickBorder);
p3.add(newGameButton);
newGameButton.addMouseListener(this);
optionsButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
selectDifficulty();
}
});
this.setJMenuBar(mb);
this.add(p2);
this.pack();
this.setVisible(true);
}
private void start(int width, int height){
p2.remove(p);
for(int x = 0 ; x < rw ; x++) {
for(int y = 0 ; y < rh ; y++) {
p.remove(logicClass.label[x][y]);
}
}
p2.remove(p3);
p3.remove(newGameButton);
this.remove(p2);
MineSweeper(width, height);
}
private void lose() {
JOptionPane.showMessageDialog(this, "GAME OVER - YOU LOSE! Starting New Game", "Game Over", JOptionPane.INFORMATION_MESSAGE);
newGame();
}
private void win() {
JOptionPane.showMessageDialog(this, "YOU WIN! Starting New Game", "CONGRATULATIONS", JOptionPane.INFORMATION_MESSAGE);
newGame();
}
private void newGame() {
if(mode==1){
rw = 9;
rh = 9;
start(9,9);
}else if(mode==2){
rw = 15;
rh = 15;
start(15,15);
}else{
rw = 20;
rh = 20;
start(20,20);
}
}
public void mouseClicked(MouseEvent e) {
if(e.getSource() == newGameButton) {
if(e.getButton() == e.BUTTON1) {
newGame();
}
}
for (int x = 0 ; x < w; x++) {
for (int y = 0 ; y < h; y++) {
if(e.getSource() == logicClass.label[x][y]) {
if(e.getButton() == e.BUTTON1) {
if(logicClass.openCell(x, y) == false) {
lose();
} else {
for (int q = 0; q < w; q++) {
for (int j = 0; j < h; j++) {
if (logicClass.label[q][j].getBackground()==Color.green) {
win();
}
}
}
}
}else if(e.getButton() == e.BUTTON3) {
logicClass.markCell(x, y);
}
}
}
}
}
private void selectDifficulty() {
group.add(b1);
group.add(b2);
group.add(b3);
group.add(b4);
b1.setSelected(true);
Object[] array = {new JLabel("Select Difficulty"),b1,b2,b3,b4};
JOptionPane.showMessageDialog(null, array);
if (b1.isSelected()) {
if (mode == 1){
rw = 9;
rh = 9;
mode = 1;
}else if(mode == 2){
rw = 15;
rh = 15;
mode = 1;
}else {
rw = 20;
rh = 20;
mode = 1;
}
start(9,9);
}else if (b2.isSelected()) {
if (mode == 1){
rw = 9;
rh = 9;
mode = 2;
}else if(mode == 2){
rw = 15;
rh = 15;
mode = 2;
}else {
rw = 20;
rh = 20;
mode = 2;
}
start(15,15);
}else if (b3.isSelected()) {
if (mode == 1){
rw = 9;
rh = 9;
mode = 3;
}else if(mode == 2){
rw = 15;
rh = 15;
mode = 3;
}else {
rw = 20;
rh = 20;
mode = 3;
}
start(20,20);
}
}
public static void main(String[]args) {
MineSweeperGUI guiClass = new MineSweeperGUI();
guiClass.MineSweeper(9,9);
}
public void mouseEntered(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
}
논리 클래스 : import java.awt.*;
public class MinesweeperLogic {
public int width, height;
private int w, h, maxBombs;
private boolean mine[][];
private boolean flag[][];
private boolean isClicked[][];
private boolean isZero[][];
private boolean marked;
MineSweeperGUI guiClass;
Button[][] label;
private int surBombs;
private String temp;
private double mineRatio;
public void startNewGame(int width, int height) {
w = width;
h = height;
label = new Button[w][h];
mine = new boolean[w][h];
flag = new boolean[w][h];
isClicked = new boolean[w][h];
isZero = new boolean[w][h];
if ( (w*h) <= 81) {
mineRatio = 0.13;
}else if ( (w*h) <= 255) {
mineRatio = 0.16;
}else {
mineRatio = 0.2;
}
maxBombs = (int) Math.floor (w * h * mineRatio);
for (int i = 0; i < maxBombs; i++) {
int x = (int) (Math.random() * w);
int y = (int) (Math.random() * h);
if (mine[x][y] == false) {
mine[x][y] = true;
isClicked[x][y] = false;
flag[x][y] = false;
isZero[x][y] = false;
} else {
i--;
}
}
}
int getWidth() {
return w;
}
int getHeight() {
return h;
}
boolean openCell(int x, int y) {
isClicked[x][y] = true;
if (mine[x][y] == true && flag[x][y] == false) {
lose();
return false;
} else if (getValue(x, y) > 0 && flag[x][y] == false) {
temp = Integer.toString (getValue(x, y));
label[x][y].setLabel (temp);
label[x][y].setBackground (new Color (111,184,252));
checkWin();
return true;
} else if (getValue(x, y) == 0 && flag[x][y] == false) {
for (int q = x-1; q <= x+1; q++) {
if(q < 0 || q >= w) {
continue;
}
for (int i = y-1; i <= y+1; i++) {
if(i < 0 || i >= h || flag[q][i] == true) { // makes sure that it wont have an error for buttons next to the wall
continue;
}
label[q][i].setBackground(new Color (111,184,252));
if (getValue(q, i) != 0) { // opens surrounding cells that have mines around them (max 8 cells)
temp = Integer.toString (getValue(q, i));
label[q][i].setLabel (temp);
isClicked[q][i] = true;
} else {
for (int k = x-1; k <= x+1; k++) {
if(k < 0 || k >= w) {
continue;
}
for (int m = y-1; m <= y+1; m++) {
if (m < 0 || m >= h || flag[k][m] == true) { // makes sure that it wont have an error for buttons next to the wall
continue;
}
if (isClicked[k][m] == false && getValue(k, m) == 0) { // recursively continues to open all surrounding cells with no mines around them
openCell(k, m);
}
}
}
}
}
}
checkWin();
return true;
} else {
return true;
}
}
boolean markCell(int x, int y) {
if (flag[x][y] == true) {
flag[x][y] = false;
label[x][y].setLabel ("");
label[x][y].setForeground (Color.black);
label[x][y].setFont (new Font (null, Font.PLAIN, 12));
return false;
}
if (isClicked[x][y] == false && flag[x][y] == false) {
flag[x][y] = true;
label[x][y].setFont (new Font ("sansserif", Font.BOLD, 14));
label[x][y].setLabel ("<|");
label[x][y].setForeground (Color.red);
}
if (mine[x][y] == true) {
return true;
} else {
return false;
}
}
boolean isOpen(int x, int y) {
if (isClicked[x][y] == false) {
return false;
} else {
return true;
}
}
boolean isMarked(int x, int y) {
if (flag[x][y] == true) {
return true;
} else {
return false;
}
}
int getValue(int x, int y) {
if (mine[x][y] == true) {
return -1;
} else {
return neighborBombs(x, y);
}
}
private int neighborBombs(int x, int y) { // checks surrounding 8 squares for number of bombs (it does include itself, but has already been checked for a bomb so it won't matter)
surBombs = 0;
for (int q = x-1; q <= x+1; q++) {
if (q < 0 || q >= w) {
continue;
}
for (int i = y-1; i <= y+1; i++) {
if (i < 0 || i >= h) { // makes sure that it wont have an error for buttons next to the wall
continue;
}
if (mine[q][i] == true) {
surBombs++;
}
}
}
return surBombs;
}
private void lose() {
for (int q = 0; q < w; q++) {
for (int j = 0; j < h; j++) {
if (mine[q][j] == true) {
if (label[q][j].getLabel().equals ("<|")) {
label[q][j].setBackground (Color.green);
} else {
label[q][j].setBackground (Color.red);
label[q][j].setLabel ("*");
label[q][j].setForeground (Color.black);
}
}
}
}
}
private void checkWin() {
int count = 0;
int count2 = 0;
for (int i = 0; i < w; i++){
for (int j = 0; j < h; j++) {
if (isClicked[i][j] == true){
count++;
}
if (mine[i][j] == true){
count2++;
}
}
}
if ( (count + count2) == (w * h) ){
win();
}
}
private void win() {
for (int q = 0; q < w; q++) {
for (int j = 0; j < h; j++) {
if (mine[q][j] == true) {
label[q][j].setBackground (Color.green);
label[q][j].setForeground (Color.red);
label[q][j].setFont (new Font ("sansserif", Font.BOLD, 14));
label[q][j].setLabel ("<|");
}
}
}
}
}
해결책
당신은 전화합니다 MineSweeper(width, height)
(방법이므로 소문자 여야합니다) 끝에 start
, 당신이 전화하는 selectDifficulty
.
MineSweeper
모든 구성 요소를 새로 만듭니다. optionsButton
새로 생성되지 않으므로 추가하십시오 또 다른 ActionListener
그것에. 모든 ActionListener
버튼을 누를 때 호출됩니다 selectDifficulty
점점 더 많은 시간이라고합니다.
가장 쉬운 해결 방법은 이것을 추가하는 것입니다 ActionListener
생성자로 MineSweeperGUI
, 그러나 가능한 경우 모든 구성 요소를 한 번만 작성하도록 소스를 리팩터링해야합니다. 최소한 전체 메뉴는 생성자로 이동해야합니다.
다른 팁
매번 메뉴 구성 요소 (jmenubar 및 children)를 설정하십시오. MineSweeper
호출되고 있습니다. 나는 그것이 생성자 인 BTW라고 생각했기 때문에 다른 Java 개발자들도 혼란 스러울 수 있습니다.
이거 때문입니다 actionPerformed
두 번 이상 호출 될 수 있습니다.
당신은 그것을 훨씬 복잡하게 만들고 있습니다. Java는 객체 지향 언어이며 해당 객체의 속성/동작에 따라 작업을 객체로 분리하려고합니다.
어쨌든, 난이도 대화에 대한 두 가지 제안.
- 메뉴 자체에 3 가지 어려움을 겪습니다. 따라서 드롭 다운 메뉴에는 초보자, 중간 또는 하드 모드가 있으며 jpanel 대화 상자를 엽니 다.
- 또는 방법을 유지하지만 몇 가지 변경이 필요합니다. 주로 메시지 대화 상자를 메인 GUI에서 나누고 새 클래스를 만듭니다.
JDialog
그 자체로. OptionsDialog의 생성자에서 부모 프레임에 대한 참조를 보냅니다. 어려움을 설정 한 후 부모의 메소드를 호출하여 난이도를 설정합니다.