示例#1
1
  private void winning() {
    String[] options = {"Try again", "Go back to Start", "Quit"};
    InformationFrame.stopClock();
    int n =
        JOptionPane.showOptionDialog(
            rootPane,
            "You won!"
                + "\n╔══╗░░░░╔╦╗░░╔═════╗"
                + "\n║╚═╬════╬╣╠═╗║░▀░▀░║"
                + "\n╠═╗║╔╗╔╗║║║╩╣║╚═══╝║"
                + "\n╚══╩╝╚╝╚╩╩╩═╝╚═════╝",
            "Smileys  c:  ☺  ☻  ت ヅ  ツ  ッ  シ Ü  ϡ  ﭢ" + "\nWhat would you like to do now?",
            JOptionPane.YES_NO_CANCEL_OPTION,
            JOptionPane.QUESTION_MESSAGE,
            null,
            options,
            options[2]);

    if (n == 2) {
      // System.out.println("n = " + n);
      System.exit(0);
    } else if (n == 1) {
      InformationFrame.dispose();
      main(null);
      this.dispose();
    } else if (n == 0) {
      String difficulty = MainManager.getMainGrid().getDifficulty();
      MainPanel.removeAll();
      constructMinesweeper(difficulty.toLowerCase());
    }
  }
示例#2
0
  /**
   * Losing Function. This method contains everything required to end a game, and also give the user
   * options afterwards.
   */
  private void losing() {
    InformationFrame.stopClock();
    timerStart = false;

    for (int r = 0; r < ButtonGrid.length; r++) {
      for (int c = 0; c < ButtonGrid[1].length; c++) {
        if (MainManager.getMainGrid().getMines(r, c) == 9) {
          ButtonGrid[r][c].setSelected(true);
          // prepare special icons

          ImageIcon MineIcon;
          MineIcon =
              new ImageIcon(
                  "C://Users/Joseph/Downloads/GitHub/"
                      + "Outside/2013/Minesweeper/src/Images/MineImage.png");

          // prepare resize
          Image MineImage = MineIcon.getImage(); // transform it

          int maxSize = Math.max(ButtonGrid[r][c].getHeight(), ButtonGrid[r][c].getWidth()) / 2;

          Image rescaledImage;
          ImageIcon imageIcon;

          rescaledImage =
              MineImage.getScaledInstance(
                  maxSize, maxSize, Image.SCALE_SMOOTH); // scale it the smooth way
          imageIcon = new ImageIcon(rescaledImage); // transform it back

          JLabel test1 = new JLabel(imageIcon);

          ButtonGrid[r][c].add(test1);
          // ButtonGrid[r][c].repaint();
          resetFont("", r, c);
        }
      }
    }

    /*
    JOptionPane.showMessageDialog(rootPane, "You lose. Sorry."
            + "\n╔╦╦╦╦╦╦╦╦╦╦╦╦╗"
            + "\n╠╬╬╬╬╬╬╬╬╬╬╬╬╣"
            + "\n╠╬╬╬╬╬╬╬╬╬╬╬╬╣"
            + "\n╠╬╬█╬╬╬╬╬╬█╬╬╣"
            + "\n╠╬╬╬╬╬╬╬╬╬╬╬╬╣"
            + "\n╠╬╬╬╬╬╬╬╬╬╬╬╬╣"
            + "\n╠╬╬╬╬╬╬╬╬╬╬╬╬╣"
            + "\n╠╬██████████╬╣"
            + "\n╠╬█╬╬╬╬╬╬╬╬█╬╣"
            + "\n╚╩╩╩╩╩╩╩╩╩╩╩╩╝", "☹", JOptionPane.YES_NO_CANCEL_OPTION);
            * */
    // Custom button text
    String[] options = {"Try again", "Go back to Start", "Quit"};
    int n =
        JOptionPane.showOptionDialog(
            rootPane,
            "You lose. Sorry."
                + "\n╔╦╦╦╦╦╦╦╦╦╦╦╦╗"
                + "\n╠╬╬╬╬╬╬╬╬╬╬╬╬╣"
                + "\n╠╬╬╬╬╬╬╬╬╬╬╬╬╣"
                + "\n╠╬╬█╬╬╬╬╬╬█╬╬╣"
                + "\n╠╬╬╬╬╬╬█╬╬╬╬╬╣"
                + "\n╠╬╬╬╬╬╬╬╬╬╬╬╬╣"
                + "\n╠╬╬╬╬╬╬╬╬╬╬╬╬╣"
                + "\n╠╬██████████╬╣"
                + "\n╠╬█╬╬╬╬╬╬╬╬█╬╣"
                + "\n╚╩╩╩╩╩╩╩╩╩╩╩╩╝"
                + "\n What would you like to do?",
            "☹",
            JOptionPane.YES_NO_CANCEL_OPTION,
            JOptionPane.QUESTION_MESSAGE,
            null,
            options,
            options[2]);

    if (n == 2) {
      // System.out.println("n = " + n);
      System.exit(0);
    } else if (n == 1) {
      InformationFrame.dispose();
      main(null);
      this.dispose();
    } else if (n == 0) {
      String difficulty = MainManager.getMainGrid().getDifficulty();
      MainPanel.removeAll();
      constructMinesweeper(difficulty.toLowerCase());
    }

    for (int y = 0; y < ButtonGrid.length; y++) {
      for (int x = 0; x < ButtonGrid[1].length; x++) {
        ButtonGrid[y][x].setFocusable(false);
      }
    }

    System.out.println("You lose");
  }
示例#3
0
  /**
   * Semi-Robust constructor helper method. Initializes and constructs the grid based on the
   * difficulty setting.
   *
   * @param difficulty is the difficulty level
   */
  private void constructMinesweeper(String difficulty) {
    MainManager = new GameControl(difficulty);
    if (InformationFrame == null) {
      InformationFrame = new InformationGUI();
    }
    InformationFrame.resetClock();
    timerStart = false;

    minesLeft = MainManager.getMainGrid().getCurrentSetting().getMines();
    InformationFrame.setMines(minesLeft);

    ButtonGrid =
        new MinesweeperButton[MainManager.getMainGrid().getLength(false)]
            [MainManager.getMainGrid().getLength(true)];
    // produce a GUI grid

    for (int y = 0; y < MainManager.getMainGrid().getLength(false); y++) {
      for (int x = 0; x < MainManager.getMainGrid().getLength(true); x++) {
        ButtonGrid[y][x] = new MinesweeperButton(" ");
        MouseListener MouseClick;
        MouseMotionListener MouseMove;

        MouseMove =
            new MouseMotionListener() {
              @Override
              public void mouseDragged(MouseEvent evt) {
                // throw new UnsupportedOperationException("Not supported yet.");
                // System.out.println("Button detected dragging: " + e.getX() + ", " + e.getY());
                bothWereDown = false;
              }

              @Override
              public void mouseMoved(MouseEvent evt) {
                // throw new UnsupportedOperationException("Not supported yet.");
                // System.out.println("Button detected movement: " + e.getX() + ", " + e.getY());
              }
            };

        MouseClick =
            new MouseListener() {
              @Override
              public void mouseClicked(MouseEvent evt) {
                if (SwingUtilities.isLeftMouseButton(evt)) {
                  System.out.println("Left click Successful");
                } else if (SwingUtilities.isRightMouseButton(evt)) {
                  System.out.println("Right click Succesful");
                }
              }

              @Override
              public void mousePressed(MouseEvent evt) {
                int y = 0;
                int x = 0;
                outerloop:
                for (y = 0; y < ButtonGrid.length; y++) {
                  for (x = 0; x < ButtonGrid[1].length; x++) {
                    if (((MinesweeperButton) evt.getSource()).equals(ButtonGrid[y][x])) {
                      break outerloop;
                      // coordinates saved
                    }
                  }
                }

                System.out.println("Press Successful");
                int both = B1DM | B3DM;
                bothWereDown = (evt.getModifiersEx() & both) == both;

                if (bothWereDown) {
                  // action if both buttons pressed
                  int r = getButtonCoordinates(evt)[0];
                  int c = getButtonCoordinates(evt)[1];

                  // makeNeighborCoordinates(r, c);
                  int[] row_neighbor = new int[8];
                  int[] col_neighbor = new int[8];

                  row_neighbor[0] = r + 1;
                  col_neighbor[0] = c;
                  row_neighbor[1] = r + 1;
                  col_neighbor[1] = c + 1;
                  row_neighbor[2] = r;
                  col_neighbor[2] = c + 1;
                  row_neighbor[3] = r - 1;
                  col_neighbor[3] = c + 1;
                  row_neighbor[4] = r - 1;
                  col_neighbor[4] = c;
                  row_neighbor[5] = r - 1;
                  col_neighbor[5] = c - 1;
                  row_neighbor[6] = r;
                  col_neighbor[6] = c - 1;
                  row_neighbor[7] = r + 1;
                  col_neighbor[7] = c - 1;

                  flaggedNeighbors = 0;

                  for (int s = 0; s < 8; s++) // iterate all sides
                  {
                    if (row_neighbor[s] >= 0
                        && row_neighbor[s] < ButtonGrid.length // within y
                        && col_neighbor[s] >= 0
                        && col_neighbor[s] < ButtonGrid[1].length // within x
                        && !ButtonGrid[row_neighbor[s]][col_neighbor[s]].isSelected()
                        && ButtonGrid[row_neighbor[s]][col_neighbor[s]].getIsFlagged()) {
                      flaggedNeighbors++;
                    } else if (row_neighbor[s] >= 0
                        && row_neighbor[s] < ButtonGrid.length // within y
                        && col_neighbor[s] >= 0
                        && col_neighbor[s] < ButtonGrid[1].length // within x
                        && !ButtonGrid[row_neighbor[s]][col_neighbor[s]].isSelected()) {
                      ButtonGrid[row_neighbor[s]][col_neighbor[s]].getModel().setArmed(true);
                      ButtonGrid[row_neighbor[s]][col_neighbor[s]].getModel().setPressed(true);
                    }
                  }

                  System.out.println("Flagged Neighbors: " + flaggedNeighbors);
                  System.out.println("Simultaneous click successful");
                }
              }

              @Override
              public void mouseReleased(MouseEvent evt) {
                if (!timerStart) {
                  timerStart = true;
                  InformationFrame.startClock();
                }

                AbstractButton abstractButton = (AbstractButton) evt.getSource();

                int y = 0;
                int x = 0;
                outerloop:
                for (y = 0; y < ButtonGrid.length; y++) {
                  for (x = 0; x < ButtonGrid[1].length; x++) {
                    if (abstractButton.equals(ButtonGrid[y][x])) {
                      break outerloop;
                      // coordinates saved
                    }
                  }
                }
                System.out.println(y + " " + x);

                // prepare special icons
                ImageIcon FlagIcon;
                FlagIcon =
                    new ImageIcon(
                        "C://Users/Joseph/Downloads/GitHub/Outside/2013/Minesweeper/src/Images/FlagImage.png");
                ImageIcon MineIcon;
                MineIcon =
                    new ImageIcon(
                        "C://Users/Joseph/Downloads/GitHub/Outside/2013/Minesweeper/src/Images/MineImage.png");

                // prepare resize
                Image MineImage = MineIcon.getImage(); // transform it
                Image FlagImage = FlagIcon.getImage();

                int maxSize = Math.max(abstractButton.getHeight(), abstractButton.getWidth()) / 2;

                Image rescaledImage;
                ImageIcon imageIcon;

                int bothMask = MouseEvent.BUTTON1_DOWN_MASK | MouseEvent.BUTTON3_DOWN_MASK;
                if (bothWereDown && !MainManager.getMainGrid().getDisplay(y, x).equals("_")) {
                  System.out.println("Both down");
                  int row = getButtonCoordinates(evt)[0];
                  int col = getButtonCoordinates(evt)[1];

                  int[] row_neighbor = new int[8];
                  int[] col_neighbor = new int[8];

                  row_neighbor[0] = row + 1;
                  col_neighbor[0] = col;
                  row_neighbor[1] = row + 1;
                  col_neighbor[1] = col + 1;
                  row_neighbor[2] = row;
                  col_neighbor[2] = col + 1;
                  row_neighbor[3] = row - 1;
                  col_neighbor[3] = col + 1;
                  row_neighbor[4] = row - 1;
                  col_neighbor[4] = col;
                  row_neighbor[5] = row - 1;
                  col_neighbor[5] = col - 1;
                  row_neighbor[6] = row;
                  col_neighbor[6] = col - 1;
                  row_neighbor[7] = row + 1;
                  col_neighbor[7] = col - 1;

                  MinesweeperButton saveTypingButton;
                  for (int s = 0; s < 8; s++) {
                    if (row_neighbor[s] >= 0
                        && row_neighbor[s] < ButtonGrid.length
                        && col_neighbor[s] >= 0
                        && col_neighbor[s] < ButtonGrid[1].length
                        && !(saveTypingButton = ButtonGrid[row_neighbor[s]][col_neighbor[s]])
                            .getIsFlagged()
                        /*&& !saveTypingButton.isSelected()*/
                        && flaggedNeighbors == Integer.parseInt(ButtonGrid[y][x].getText())) {

                      MainManager.getMainGrid().selectBox(row_neighbor[s], col_neighbor[s]);
                      // safeButtonsLeft--;
                      // System.out.println("Safe Buttons Left: " + safeButtonsLeft);
                      String displayText =
                          MainManager.getMainGrid().getDisplay(row_neighbor[s], col_neighbor[s]);
                      if (displayText.equals("9")) {
                        rescaledImage =
                            MineImage.getScaledInstance(
                                maxSize, maxSize, Image.SCALE_SMOOTH); // scale it the smooth way
                        imageIcon = new ImageIcon(rescaledImage); // transform it back

                        JLabel test1 = new JLabel(imageIcon);

                        saveTypingButton.setBackground(Color.red);
                        saveTypingButton.add(test1);

                        // initiate losing
                        losing();
                      }
                      saveTypingButton.setText(displayText);

                      resetFont(displayText, row_neighbor[s], col_neighbor[s]);
                    }
                  }
                }

                // right click flag
                if (SwingUtilities.isRightMouseButton(evt)
                    && !SwingUtilities.isLeftMouseButton(evt)
                    && !abstractButton.isSelected()) {
                  if (!ButtonGrid[y][x].getIsFlagged()) {
                    rescaledImage =
                        FlagImage.getScaledInstance(maxSize, maxSize, Image.SCALE_SMOOTH);
                    imageIcon = new ImageIcon(rescaledImage);
                    JLabel iconLabel = new JLabel(imageIcon);
                    abstractButton.add(iconLabel);
                    ButtonGrid[y][x].setIsFlagged(true);
                    MainManager.getMainGrid().flagBox(y, x);
                    // safeButtonsLeft--;
                    // System.out.println("Safe Buttons Left: " + safeButtonsLeft);
                    resetFont("", y, x);
                    minesLeft--;
                    InformationFrame.setMines(minesLeft);
                  } else {
                    ButtonGrid[y][x].setIsFlagged(false);
                    MainManager.getMainGrid().flagBox(y, x);
                    // safeButtonsLeft--;
                    // System.out.println("Safe Buttons Left: " + safeButtonsLeft);
                    ButtonGrid[y][x].removeAll();
                    resetFont("", y, x);
                    minesLeft++;
                    InformationFrame.setMines(minesLeft);
                  }
                } else if (SwingUtilities.isLeftMouseButton(evt)
                    && !SwingUtilities.isRightMouseButton(evt)
                    && !ButtonGrid[y][x].getIsFlagged()) {
                  // leftClick
                  MainManager.getMainGrid().selectBox(y, x);
                  // safeButtonsLeft--;
                  // System.out.println("Safe Buttons Left: " + safeButtonsLeft);
                  String displayText = MainManager.getMainGrid().getDisplay(y, x);
                  if (displayText.equals("9")) {
                    rescaledImage =
                        MineImage.getScaledInstance(
                            maxSize, maxSize, Image.SCALE_SMOOTH); // scale it the smooth way
                    imageIcon = new ImageIcon(rescaledImage); // transform it back

                    JLabel test1 = new JLabel(imageIcon);

                    abstractButton.setBackground(Color.red);
                    abstractButton.add(test1);

                    // intiate losing
                    losing();
                  }
                  abstractButton.setText(displayText);

                  resetFont(displayText, y, x);

                  // break; // apparently that made all the differnece lol
                } else {
                  abstractButton.setSelected(false);
                }

                // update everything
                safeButtonsLeft =
                    MainManager.getMainGrid().getLength(true)
                            * MainManager.getMainGrid().getLength(false)
                        - MainManager.getMainGrid().getCurrentSetting().getMines();
                int count = 0;

                for (int r = 0; r < ButtonGrid.length; r++) {
                  for (int c = 0; c < ButtonGrid[1].length; c++) {
                    // check for zeros
                    String displayText = MainManager.getMainGrid().getDisplay(r, c);
                    if (!(displayText.equals("_")) && !displayText.equals("!")) {
                      ButtonGrid[r][c].setSelected(true);
                      ButtonGrid[r][c].setText(displayText);
                      /*
                      if(!MainManager.getMainGrid().getDisplay(r, c).equals("9"))
                      {
                          safeButtonsLeft--;
                          System.out.println("Safe Buttons Left: " + safeButtonsLeft);
                      }
                      * */
                      // System.out.println("Safe Squares: " +
                      // MainManager.getMainGrid().getSafeSquares());
                      // count++;
                      // System.out.println("Count: " + count);
                      resetFont(displayText, r, c);
                    }

                    // if(ButtonGrid[r][c].isSelected())
                    if (!MainManager.getMainGrid().getDisplay(r, c).equals("_")
                        && !MainManager.getMainGrid().getDisplay(r, c).equals("!")) {
                      count++;
                    }
                    System.out.println("Count: " + count);
                    ButtonGrid[r][c].getModel().setArmed(false);
                    ButtonGrid[r][c].getModel().setPressed(false);
                  }
                }

                if (MainManager.getMainGrid().getLength(true)
                            * MainManager.getMainGrid().getLength(false)
                        - MainManager.getMainGrid().getCurrentSetting().getMines()
                    == count) {
                  winning();
                }
                requestFocusInWindow(); // works now
              }

              @Override
              public void mouseEntered(MouseEvent evt) {
                // throw new UnsupportedOperationException("Not supported yet.");
                AbstractButton abstractButton = (AbstractButton) evt.getSource();
                abstractButton.getModel().setArmed(true);
              }

              @Override
              public void mouseExited(MouseEvent evt) {
                // throw new UnsupportedOperationException("Not supported yet.");
                AbstractButton abstractButton = (AbstractButton) evt.getSource();
                // abstractButton.getModel().setArmed(false);
                // abstractButton.getModel().setPressed(false);
              }
            };

        ButtonGrid[y][x].addMouseListener(MouseClick);
        ButtonGrid[y][x].addMouseMotionListener(MouseMove);
        MainPanel.add(ButtonGrid[y][x]);

        System.out.println(y + ", " + x); // debug
      }
    } // end for loop

    // sets panel and frame
    this.setResizable(false); // MUST PUT THIS LINE BEFORE BOUNDS AND SIZES ARE SET

    FrameSize =
        new Dimension(
            SizeSettings.valueOf(difficulty.toUpperCase()).x,
            SizeSettings.valueOf(difficulty.toUpperCase()).y);
    PanelSize =
        new Dimension(
            SizeSettings.valueOf(difficulty.toUpperCase()).x,
            SizeSettings.valueOf(difficulty.toUpperCase()).y);

    this.setSize(FrameSize);
    this.setLocation(300, 0);

    MainPanel.setPreferredSize(PanelSize);

    MainPanel.setLayout(
        new GridLayout(
            MainManager.getMainGrid().getLength(true), MainManager.getMainGrid().getLength(false)));
    this.setVisible(true);

    this.pack();
  }