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);
}
}
}
}
Since operands of operators are evaluated from left to right, the integer cast is occurring first in this statement
mines = (int) Math.random()*100;
causing the first term to be cast to 0
. This causes counter
to be decremented as soon as it is incremented resulting in the loop repeating itself ad infinitum. Enclose the operands in parenthesis:
mines = (int) (Math.random() * 100);