Minesweeper Mines no se colocará ni aparecerá [cerrado]
Frecuentes
Visto 184 veces
1
Why will this code not load buttons when run? The question is very vague I know but I'm very new to Java and trying to understand lots of things at once. This code is based off of my two previous successful attempts to code TicTacToe and Connect Four both of which worked.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.applet.Applet;
import java.awt.event.ActionListener;
public class Minesweeper extends Applet implements ActionListener {
// initializing all data types
JButton[] a; // The grid boxes
int counter = 1; // Obsolete
char[] letter; // Array of mine locations
int[] numbers; // Array of numbers; values and locations
boolean explode = false;
String name1;
String name2;
int mines;
public void init() {
// The code below initializes and locates the grid
setLayout(new GridLayout(10, 10));
a = new JButton[100];
// The code below fills the grid with buttons that can be clicked
for (int counter = 0; counter < 100; counter++) {
a[counter] = new JButton();
a[counter].setText(" ");
a[counter].setBackground(Color.white);
a[counter].addActionListener(this);
add(a[counter]);
}
}
public void nit() {
numbers = new int[100];
letter = new char[100];
for (counter = 0; counter < 10; counter++) {
mines = (int) Math.random() * 100;
if (letter[mines] == '*') {
counter--;
} else {
letter[mines] = '*';
}
}
for (counter = 0; counter < 100; counter++) {
numbers[counter] = 0;
}
for (int search = 0; search < 10; search++) {
for (int searchb = 0; searchb < 10; searchb++) {
if (letter[search * 10 + searchb] == '*') {
if (search != 0) {
numbers[((search - 1) * 10) + searchb]++;
}
if (search != 9) {
numbers[((search + 1) * 10) + searchb]++;
}
if (searchb != 0) {
numbers[((search * 10) + searchb) - 1]++;
}
if (searchb != 9) {
numbers[((search * 10) + searchb) + 1]++;
}
if ((search != 0) && (searchb != 0)) {
numbers[(((search - 1) * 10) + searchb) - 1]++;
}
if ((search != 9) && (searchb != 9)) {
numbers[(((search + 1) * 10) + searchb) + 1]++;
}
if ((search != 0) && (searchb != 9)) {
numbers[(((search - 1) * 10) + searchb) + 1]++;
}
if ((search != 9) && (searchb != 0)) {
numbers[(((search + 1) * 10) + searchb) - 1]++;
}
}
}
}
for (int counter = 0; counter < 100; counter++) {
letter[counter] = (char) ('0' + numbers[counter]);
JOptionPane.showMessageDialog(null, " " + letter[counter]);
}
}
// ActionEvent e is the click
public void actionPerformed(ActionEvent e) {
int pickedsquare = 0, coloring, pickedcolumn = 0;
JButton b = (JButton) e.getSource();
counter++;
b.setText("Test");
for (int f = 0; f < 100; f++) {
JOptionPane.showMessageDialog(null, " " + letter[f]);
if (a[f].getText() == "Test") {
pickedsquare = f;
JOptionPane.showMessageDialog(null, " " + letter[f]);
name1 = " " + letter[pickedsquare];
a[f].setText(name1);
break;
}
}
if (letter[pickedsquare] == '*')
explode = true;
if (explode == true) {
JOptionPane.showMessageDialog(null, "You are dead!");
for (int counterb = 0; counterb <= 99; counterb++) {
a[counterb].setEnabled(false);
}
}
if (counter == 89) {
JOptionPane.showMessageDialog(null, "You have swept all mines!");
for (int counterb = 0; counterb <= 99; counterb++) {
a[counterb].setEnabled(false);
}
}
}
}
1 Respuestas
3
Since operands of operators are evaluated from izquierda a derecha, the integer cast is occurring first in this statement
mines = (int) Math.random()*100;
causing the first term to be cast to 0
. Esto causa counter
to be decremented as soon as it is incremented resulting in the loop repeating itself indefinidamente. Enclose the operands in parenthesis:
mines = (int) (Math.random() * 100);
respondido 27 nov., 13:23
Implemented that code, didn't see a noticeable difference and same issues are occuring - user3042985
You removed the loop completely from the init
method where the problem existed. Now the answer does not relate to the modified code from the question - Reimeo
Implemented that fixed some other problems and now the game is working, thanks very much for your time - user3042985
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas java jbutton minesweeper or haz tu propia pregunta.
Yes, thats the name for my second method, not very meaningful naming but the Jbuttons wouldn't even appear if it was included in the first method. Not 100% sure why, trying to understand now - user3042985
right now are your JButtons appearing? I was under the impression that they werent - Cruncher
JButtons are appearing now, I moved the letter array to the second method and they appeared. Not sure why that made a difference. Problem now is that the jButtons simply have "test" written on them when clicked, nothing more. - user3042985