Results 1 to 6 of 6
  1. #1
    Shin is offline Member
    Join Date
    Oct 2012
    Posts
    7
    Rep Power
    0

    Default I badly need help on this minesweeper game

    Hey guys im new here and in java also. i just wanted to put a menu on this code

    Java Code:
    package project;
    
    import java.awt.AWTEvent;
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Component;
    import java.awt.GridLayout;
    import java.awt.Insets;
    import java.awt.Menu;
    import java.awt.Point;
    import java.awt.Toolkit;
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Random;
    
    import java.awt.event.*;
    
    import javax.swing.*;
    
    
    
    public class minesweeper extends JPanel implements AWTEventListener, ActionListener {
    
    
      
    
       
        
    
    	  public static enum State {
    	    Clicked, Marked, Initial, WrongMarked
    	  }
    
    	  public static enum GameState {
    	    NotStarted, Playing, Finished
    	  }
    
    	  private static final int  BombCounts = 8;
    	  private int          ROWS      = 6, COLUMNS = 6, TOTAL = ROWS * COLUMNS;
    	  private JPanel        pnlMain      = new JPanel(new GridLayout(ROWS, COLUMNS));
    	  private JLabel        lblBombCount  = new JLabel(BombCounts + "");
    	  private JLabel        lblTimer    = new JLabel("0");
    	  private boolean        isColorCheatOn  = false;
                JMenuBar menuBar;
                JMenu menu, submenu;
                JMenuItem menuItem;
    
    
    	  private GameState  state  = GameState.NotStarted;
    
    	  public minesweeper() {
    	    setLayout(new BorderLayout());
    	    add(pnlMain, BorderLayout.CENTER);
    	    createButtons();
    
    	    Toolkit.getDefaultToolkit().addAWTEventListener(this, AWTEvent.KEY_EVENT_MASK);
    
              
    
    
    	  }
    
    
    
    	  public void restart() {
    	    state = GameState.NotStarted;
    	    lblTimer.setText("0");
    	    pnlMain.removeAll();
    	    createButtons();
    	    pnlMain.updateUI();
    	    lblBombCount.setText("" + BombCounts);
    	    lblBombCount.updateUI();
    	  }
    
    
    
    	  public void createButtons() {
    	    List<Point> lstBombsLocation = new ArrayList<Point>();
    
    	    for (int row = 0; row < ROWS; row++) {
    	      for (int col = 0; col < COLUMNS; col++) {
    	        JButton btn = getButton(lstBombsLocation, TOTAL, new Point(row, col) {
    	          @Override
    	          public String toString() {
    	            return (int) getX() + ", " + (int) getY();
    	          }
    
    	          @Override
    	          public boolean equals(Object obj) {
    	            return ((Point) obj).getX() == getX() && ((Point) obj).getY() == getY();
    	          }
    	        });
    	        pnlMain.add(btn);
    	      }
    	    }
    	    while (lstBombsLocation.size() < BombCounts) {
    	      updateBomds(lstBombsLocation, pnlMain.getComponents());
    	    }
    	    for (Component c : pnlMain.getComponents()) {
    	      updateBombCount((GameButton) c, pnlMain.getComponents());
    	    }
    
    	  }
    
    	  public void updateBomds(List<Point> lstBombsLocation, Component[] components) {
    
    	    Random r = new Random();
    	    for (Component c : components) {
    	      Point location = ((GameButton) c).getPosition();
    	      int currentPosition = new Double(((location.x) * COLUMNS) + location.getY()).intValue();
    	      int bombLocation = r.nextInt(TOTAL);
    	      if (bombLocation == currentPosition) {
    	        ((GameButton) c).setBomb(true);
    	        lstBombsLocation.add(((GameButton) c).getPosition());
    	        return;
    	      }
    	    }
    	  }
    
    	  public GameButton getButton(List<Point> lstBombsLocation, int totalLocations, Point location) {
    	    GameButton btn = new GameButton(location);
    	    btn.setMargin(new Insets(0, 0, 0, 0));
    	    btn.setFocusable(false);
    	    if (lstBombsLocation.size() < BombCounts) {
    	      if (Bomb()) {
    	        btn.setBomb(true);
    	        lstBombsLocation.add(location);
    	      }
    	    }
    	    btn.addMouseListener(new MouseAdapter() {
    	      @Override
    	      public void mouseClicked(MouseEvent mouseEvent) {
    	        if (state != GameState.Playing) {
    	          state = GameState.Playing;
    
    	        }
    	        if (((GameButton) mouseEvent.getSource()).isEnabled() == false) {
    	          return;
    	        }
    	        if (mouseEvent.getButton() == MouseEvent.BUTTON1) {
    	          if (((GameButton) mouseEvent.getSource()).getState() == State.Marked) {
    	            ((GameButton) mouseEvent.getSource()).setState(State.Initial);
    	            lblBombCount.setText((Long.parseLong(lblBombCount.getText()) + 1) + "");
    	            ((GameButton) mouseEvent.getSource()).updateUI();
    	            return;
    	          }
    	          ((GameButton) mouseEvent.getSource()).setState(State.Clicked);
    	          if (((GameButton) mouseEvent.getSource()).Bomb()) {
    	            blastBombs();
    	            return;
    	          } else {
    	            if (((GameButton) mouseEvent.getSource()).getBombCount() == 0) {
    	              updateSurroundingZeros(((GameButton) mouseEvent.getSource()).getPosition());
    	            }
    	          }
    	          if (!checkGameState()) {
    	            ((GameButton) mouseEvent.getSource()).setEnabled(false);
    	          }
    	        } else if (mouseEvent.getButton() == MouseEvent.BUTTON3) {
    	          if (((GameButton) mouseEvent.getSource()).getState() == State.Marked) {
    	            ((GameButton) mouseEvent.getSource()).setState(State.Initial);
    	            lblBombCount.setText((Long.parseLong(lblBombCount.getText()) + 1) + "");
    	          } else {
    	            ((GameButton) mouseEvent.getSource()).setState(State.Marked);
    	            lblBombCount.setText((Long.parseLong(lblBombCount.getText()) - 1) + "");
    	          }
    	        }
    	        ((GameButton) mouseEvent.getSource()).updateUI();
    	      }
    	    });
    	    return btn;
    	  }
    
    	public boolean checkGameState() {
    	    boolean isWin = false;
    	    for (Component c : pnlMain.getComponents()) {
    	      GameButton b = (GameButton) c;
    	      if (b.getState() != State.Clicked) {
    	        if (b.Bomb()) {
    	          isWin = true;
    	        } else {
    	          return false;
    	        }
    	      }
    	    }
    	    if (isWin) {
    	      state = GameState.Finished;
    	      for (Component c : pnlMain.getComponents()) {
    	        GameButton b = (GameButton) c;
    	        if (b.Bomb()) {
    	          b.setState(State.Marked);
    	        }
    	        b.setEnabled(false);
    
    	      }
    	      JOptionPane.showMessageDialog(this, "You win the game :D", "Congrats", JOptionPane.INFORMATION_MESSAGE, null);
    	    }
    	    return isWin;
    	  }
    
    	  private void updateSurroundingZeros(Point currentPoint) {
    	    Point[] points = getSurroundings(currentPoint);
    
    	    for (Point p : points) {
    	      GameButton b = getButtonAt(pnlMain.getComponents(), p);
    	      if (b != null && b.getBombCount() == 0 && b.getState() != State.Clicked && b.getState() != State.Marked && b.Bomb() == false) {
    	        b.setState(State.Clicked);
    	        updateSurroundingZeros(b.getPosition());
    	        b.updateUI();
    	      }
    	      if (b != null && b.getBombCount() > 0 && b.getState() != State.Clicked && b.getState() != State.Marked && b.Bomb() == false) {
    	        b.setEnabled(false);
    	        b.setState(State.Clicked);
    	        b.updateUI();
    	      }
    	    }
    	  }
    
    	  public void blastBombs() {
    	    int blastCount = 0;
    	    for (Component c : pnlMain.getComponents()) {
    	      ((GameButton) c).setEnabled(false);
    	      ((GameButton) c).transferFocus();
    	      if (((GameButton) c).Bomb() && ((GameButton) c).getState() != State.Marked) {
    	        ((GameButton) c).setState(State.Clicked);
    	        ((GameButton) c).updateUI();
    	        blastCount++;
    	      }
    	      if (((GameButton) c).Bomb() == false && ((GameButton) c).getState() == State.Marked) {
    	        ((GameButton) c).setState(State.WrongMarked);
    	      }
    	    }
    	    lblBombCount.setText("" + blastCount);
    	    lblBombCount.updateUI();
    	    state = GameState.Finished;
    	    JOptionPane.showMessageDialog(this, "You lose the game :(", "Game Over", JOptionPane.ERROR_MESSAGE, null);
    	    for (Component c : pnlMain.getComponents()) {
    	      GameButton b = (GameButton) c;
    	      b.setEnabled(false);
    	    }
    	  }
    
    	  public boolean Bomb() {
    	    Random r = new Random();
    	    return r.nextInt(ROWS) == 1;
    	  }
    
    	  public static void main(String[] args) {
    	    JFrame fr = new JFrame("MineSweeper");
    
                fr.setLayout(new BorderLayout());
    	    fr.add(new minesweeper());
    	    fr.setResizable(false);
    	    fr.setSize(250, 350);
    	    fr.setLocationRelativeTo(null);
    	    fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	    fr.setVisible(true);
               
    
      }
    
    
    
    	  class GameButton extends JButton {
    	    private boolean  Bomb    = false;
    	    private Point  position  = null;
    	    private int    bombCount  = 0;
    	    private State  state    = State.Initial;
    
    	    public void setState(State state) {
    	      this.state = state;
    	      if (getBombCount() == 0 && !Bomb) {
    	        setEnabled(false);
    	      }
    	    }
    
    	    public State getState() {
    	      return state;
    	    }
    
    	    public int getBombCount() {
    	      return bombCount;
    	    }
    
    	    public void setBombCount(int bombCount) {
    	      this.bombCount = bombCount;
    	    }
    
    	    public GameButton(Point position) {
    	      setPosition(position);
    	      setText(position.toString());
    	    }
    
    	    public Point getPosition() {
    	      return position;
    	    }
    
    	    public void setPosition(Point position) {
    	      this.position = position;
    	    }
    
    	    public boolean Bomb() {
    	      return Bomb;
    	    }
    
    	    public void setBomb(boolean Bomb) {
    	      this.Bomb = Bomb;
    	    }
    
    	    @Override
    	    public String getText() {
    	      if (state == State.Initial) {
    	        return "";
    	      }
    	      if (state == State.Marked) {
    	        return "F"
    ;
    	      }
    	      if (state == State.Clicked) {
    	        if (Bomb) {
    	          return "<html><font size='7'><b>☺</b></font></html>";
    	        } else {
    	          if (getBombCount() > 0)
    	            return getBombCount() + "";
    	          else
    	            return "";
    	        }
    	      }
    	      if (state == State.WrongMarked) {
    	        return "X";
    	      }
    	      return super.getText();
    	    }
    
    	    @Override
    	    public Color getBackground() {
    	      if (isColorCheatOn && Bomb) {
    	        return Color.BLACK;
    	      }
    	      if (state == State.Clicked) {
    	        if (Bomb) {
    	          return Color.RED;
    	        }
    	        if (getBombCount() > 0) {
    	          return Color.BLUE;
    	        }
    	      }
    	      if (isEnabled()) {
    	        return Color.GRAY;
    	      } else {
    	        return super.getBackground();
    	      }
    	    }
    	  }
    
    	  public Point[] getSurroundings(Point cPoint) {
    	    int cX = (int) cPoint.getX();
    	    int cY = (int) cPoint.getY();
    	    Point[] points = { new Point(cX - 1, cY - 1), new Point(cX - 1, cY), new Point(cX - 1, cY + 1), new Point(cX, cY - 1), new Point(cX, cY + 1),
                                             new Point(cX + 1, cY - 1), new Point(cX + 1, cY), new Point(cX + 1, cY + 1) };
    	    return points;
    	  }
    
    	  public void updateBombCount(GameButton btn, Component[] components) {
    	    Point[] points = getSurroundings(btn.getPosition());
    
    	    for (Point p : points) {
    	      GameButton b = getButtonAt(components, p);
    	      if (b != null && b.Bomb()) {
    	        btn.setBombCount(btn.getBombCount() + 1);
    	      }
    	    }
    	    btn.setText(btn.getBombCount() + "");
    	  }
    
    	  public GameButton getButtonAt(Component[] components, Point position) {
    	    for (Component btn : components) {
    	      if ((((GameButton) btn).getPosition().equals(position))) {
    	        return (GameButton) btn;
    	      }
    	    }
    	    return null;
    	  }
    
    	  public void eventDispatched(AWTEvent event) {
    	    if (KeyEvent.class.isInstance(event) && ((KeyEvent) (event)).getID() == KeyEvent.KEY_RELEASED) {
    
    	      if (((KeyEvent) (event)).getKeyCode() == KeyEvent.VK_F1) {
    	        restart();
    	      }
    	      if (((KeyEvent) (event)).getKeyCode() == KeyEvent.VK_F2) {
    	        isColorCheatOn = !isColorCheatOn;
    	        if (state == GameState.Playing) {
    	          pnlMain.updateUI();
    	        }
    	      }
    	      }
    	    }
     public void setJMenuBar(Menu j) {
    
    
        JMenu File = new JMenu("File");
       add(File);
       JMenuItem restart = new JMenuItem("Restart");
       JMenuItem instruction = new JMenuItem("Instruction");
       JMenuItem about = new JMenuItem("About");
       File.add(restart);
       File.add(instruction);
       File.add(about);
    
    
    }
    
    	  public void actionPerformed(ActionEvent actionEvent) {
    
    	    }
    	  }
    Last edited by Shin; 10-14-2012 at 04:26 PM.

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: I badly need help on this minesweeper game

    Hello and welcome! Please use [code][/code] when posting code.

    I also see you have a lot of goofy syntax - for example, your setJMenuBar() takes a jMenuBar instance, but then doesn't appear to do anything with it.

  3. #3
    Shin is offline Member
    Join Date
    Oct 2012
    Posts
    7
    Rep Power
    0

    Default Re: I badly need help on this minesweeper game

    yeah, thats my problem. i cant show the j menu bar on the jpanel including the game.

  4. #4
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: I badly need help on this minesweeper game

    I mean, I don't see that you ever added the JMenuBar to the window itself. Do that.

  5. #5
    Shin is offline Member
    Join Date
    Oct 2012
    Posts
    7
    Rep Power
    0

    Default Re: I badly need help on this minesweeper game

    How? please be specific :(, i just did copy paste this code.

  6. #6
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: I badly need help on this minesweeper game

    i just did copy paste this code.
    Well thats your first problem right there.


Similar Threads

  1. [suggestions] minesweeper
    By temyong in forum New To Java
    Replies: 7
    Last Post: 12-30-2010, 03:10 AM
  2. urgent...minesweeper problem
    By neha_bat in forum AWT / Swing
    Replies: 8
    Last Post: 08-16-2010, 05:17 PM
  3. Replies: 2
    Last Post: 08-05-2010, 04:49 AM
  4. MineSweeper!
    By CrushOverwrite in forum New To Java
    Replies: 17
    Last Post: 02-24-2010, 02:34 PM
  5. Minesweeper problem! need help!
    By jeffrey in forum New To Java
    Replies: 1
    Last Post: 10-02-2009, 04:59 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •