Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am having trouble with a java program that is supposed to create a minefield g

ID: 3656082 • Letter: I

Question

I am having trouble with a java program that is supposed to create a minefield game in which there is a start and finish point and two mines hidden under random boardsquares(" It should cause two randomly chosen minefield buttons to be designated as Start and Finish, and two randomly chosen minefield buttons to have mines hidden below them."). the player is supposed to create a path between the start and finish point without stepping on a mine. there are two panels, one that holds the game board and one that holds the done and clear buttons. the clear button starts a new game and the done button checks to see if the player encountered a mine and a message that indicates whether they one or lost. I am having a lot of troubling even getting my buttons to change color when I click on them. Any help or ideas would be welcome since i have no idea how i would even go about setting up the clear button. here is my code so far.... import java.awt.Color; import java.awt.Dimension; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.BorderFactory; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class minefield2 extends JFrame implements ActionListener{ private JPanel gameBoard; private JButton[] boardSquares = new JButton[25]; private JPanel otherPanel; private JPanel mainPanel; private JButton clear; private JButton done; private JFrame mainFrame; public minefield2(){ super(); gameBoard = new JPanel(); otherPanel = new JPanel(); mainPanel = new JPanel(); mainFrame = new JFrame(); gameBoard.setLayout(new GridLayout(5,5,0,0)); otherPanel.setLayout(new GridLayout(1,2,0,0)); mainPanel.setLayout(new GridLayout(2,1,20,20)); mainPanel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5)); for(int index=0; index<25; index++){//create a new JBUTTON boardSquares[index] = new JButton();//creates the board buttons boardSquares[index].setPreferredSize(new Dimension(80,80));//setting their size boardSquares[index].setActionCommand("" + index);//giving the buttons a property....O,1,2, ETC. boardSquares[index].addActionListener(this); gameBoard.add(boardSquares[index]); } clear = new JButton("Clear"); clear.setPreferredSize(new Dimension(80,80)); clear.setActionCommand("Clear");// clear.addActionListener(this);//Registers an instance of the event handler //class as a listener on one or more components. clear.setVisible(true); done = new JButton("Done"); done.setPreferredSize(new Dimension(80,80)); done.setActionCommand("Done");// done.addActionListener(this); done.setVisible(true); mainPanel.add(gameBoard); mainPanel.add(otherPanel); mainFrame.setContentPane(mainPanel); otherPanel.add(clear); otherPanel.add(done); mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); mainFrame.pack(); mainFrame.setVisible(true); } public void clearbutton(){ } public void actionPerformed(ActionEvent e){ //code that reacts to the action... int bomb = (int) (Math.random() * 100) % 25; int start = (int) (Math.random() * 100)%; int finish = (int) (Math.random()*100) % 25; Object s = e.getSource();//finds the object in which the event occured for(int i=0; i< boardSquares.length; i++){ if(e.getActionCommand()..equalsIgnoreCase("Clear") ){ boardSquares[i].setBackground(Color.magenta); } else if(e.getActionCommand().equalsIgnoreCase("Done")){ boardSquares[i].setBackground(Color.magenta); } else{ String A = e.getActionCommand(); int a = Integer.parseInt(A); boardSquares[a].setBackground(Color.blue); } } } public static void main(String[] args) { minefield2 a = new minefield2(); } }

Explanation / Answer

//import java.awt.Color; import info.gridworld.actor.ActorWorld; import info.gridworld.actor.Rock; import info.gridworld.actor.Actor; import info.gridworld.grid.Grid; import info.gridworld.grid.Location; public class Mine extends Rock { private boolean _hasBomb = true; private boolean _isFlagged = false; private boolean _isRevealed = false; private int _numberOfAdjacentBombCounter = 0; private int _numberOfAdjacentBombs = 0; private double rand = Math.random(); public Mine(MineField F) { if(rand >= .1) _hasBomb = false; } public void setNumberOfAdjacentBombs(int n) { _numberOfAdjacentBombs = n; } // access to private variables public int numberOfAdjacentBombs() { return _numberOfAdjacentBombs; } //public boolean gameOver() //{ // return _gameOver; //} public boolean hasBomb() { return _hasBomb; } public void Reveal() { _isRevealed = true; if(!_hasBomb && adjacentBombCounter() == 0) { for(Actor neighbor : getGrid().getNeighbors(getLocation())) { Mine neighborMine = ((Mine) neighbor); if(!neighborMine._isRevealed && !neighborMine._hasBomb) neighborMine.Reveal(); } } } public void flag() { _isFlagged = true; } public void unflag() { _isFlagged = false; } public int adjacentBombCounter() { _numberOfAdjacentBombCounter = 0; for(Actor neighbor : getGrid().getNeighbors(getLocation())) { Mine neighborMine = ((Mine) neighbor); if(neighborMine._hasBomb) _numberOfAdjacentBombCounter++; } return _numberOfAdjacentBombCounter; } public String getImageSuffix() { if(_isRevealed && _hasBomb) return "Explode"; if(_isRevealed && !_hasBomb) return "" + adjacentBombCounter(); if(_isFlagged) return "Flag"; return "NoClick"; } } MineField.java: //import java.awt.Color; import info.gridworld.actor.ActorWorld; import info.gridworld.actor.Actor; import info.gridworld.grid.Grid; import info.gridworld.grid.Location; public class MineField extends ActorWorld { public boolean _gameOver = false; public MineField() { //super(new BoundedGrid (rows, columns)); Grid gr = getGrid(); int row = gr.getNumRows(); int column = gr.getNumCols(); //fill the grid with mines for (int r = 0; r