Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Container;
- import java.awt.GridLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JMenu;
- import javax.swing.JMenuBar;
- import javax.swing.JMenuItem;
- import javax.swing.JPanel;
- public class Minesweeper extends JFrame implements Runnable
- {
- final int rows = 9;
- final int columns = 9;
- final JButton [][] mineArray;
- public Minesweeper()
- {
- super("Minesweeper");
- Container big = getContentPane();
- mineArray = null;
- big.setLayout(new GridLayout(9,9));
- for(int x = 1; x == rows; x++)
- {
- for(int y = 1; y == columns; y++)
- {
- mineArray[x][y] = new MineButton(x, y, false);
- big.add(mineArray[x][y]);
- }
- }
- System.out.println(mineArray[1][1]);
- }
- public static void main(String[] args)
- {
- Minesweeper ms = new Minesweeper();
- javax.swing.SwingUtilities.invokeLater(ms);
- }
- // makes menu bar - a file option that opens to a quit option
- private void makeMenu()
- {
- JMenu fileMenu = new JMenu("File");
- JMenuItem quitItem = new JMenuItem("Quit");
- quitItem.addActionListener(e -> System.exit(0));
- fileMenu.add(quitItem);
- JMenuItem resetItem = new JMenuItem("Reset");
- fileMenu.add(resetItem);
- // RESET here
- JMenuBar menuBar = new JMenuBar();
- menuBar.add(fileMenu);
- setJMenuBar(menuBar);
- }
- public void end()
- {
- System.out.println("GAME OVER");
- }
- public class MineButton extends JButton
- {
- private int x;
- private int y;
- private boolean isMine;
- private boolean isFlagged;
- private int numMines;
- public MineButton(int _x, int _y, boolean _isMine)
- {
- isMine = _isMine;
- isFlagged = false;
- }
- public void setFlag()
- {
- isFlagged = !(isFlagged);
- }
- public boolean hasMine()
- {
- return isMine;
- }
- public boolean getFlag()
- {
- return isFlagged;
- }
- public void setAdjacentMineCount()
- {
- int count = 0;
- // if(x > 0 && (mineArray[x-1][y]).hasMine()) { count++; }
- // if(x < 8 && (mineArray[x+1][y]).hasMine()) { count++; }
- // if(y > 0 && (mineArray[x][y - 1]).hasMine()) { count++; }
- // if(y < 8 && (mineArray[x][y + 1]).hasMine()) { count++; }
- System.out.println(mineArray[x-1][y]);
- setText(count > 0 ? "" + count : "");
- numMines = count;
- }
- public void reveal()
- {
- if(isMine)
- {
- //End the game idk
- }
- setLabel(Integer.toString(numMines));
- System.out.println(numMines);
- }
- }
- public void run()
- {
- setSize(500,500);
- setTitle("Minesweeper");
- setDefaultCloseOperation(EXIT_ON_CLOSE);
- makeMenu();
- setVisible(true);
- }
- class ClickListener implements ActionListener
- {
- // MineButton click = new MineButton("5", 0);
- public void actionPerformed(ActionEvent e)
- {
- //if(isRightMouseButton(e))
- if(4 == 5)
- {
- //getFlag();
- // this is the flag function - we want to implement something similar to grenredyellowstopthing
- }
- else
- {
- //if(a.getFlag())
- {
- return;
- }
- //else
- //{
- //a.reveal();
- //}
- }
- }
- }
- /*
- * Game Structure:
- * begin with all unlabeled buttons
- * when the first button is clicked, make sure that button isn't a mine. Maybe make sure that several buttons reveal themselves?
- * randomly assign the positions of the (10?) bombs
- * fill the number labels of each so that they represent how many mines each is touching
- * when the user clicks on a non-revealed block, have the game end if it's a mine. Otherwise, have it reveal the block that is
- * clicked and any surrounding blocks with a value of 0. We need to set off a chain reaction of some kind here?
- * When the user right clicks on a non-revealed block, mark the block with a flag.
- * Turn off the (left)clickability for any flagged block. Right clicking again unflags.
- * Have a counter for the number of flags used in the game and the number of flags left.
- * The game ends when all non-mine blocks are revealed or a mine is clicked.
- * If the user loses, display where all the mines were that hadn't been clicked. Have the clicked mine turn red.
- */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement