1

I am trying to make an actual timer that keeps track of the time in my Minesweeper game. However, I don't know how to code my actionPerformed method in order to set the timer start and the timer end. I want the timer to start once the player clicks on a button in the mine field, and I want the timer to end when the player wins or loses. I did do a gameEnd method that determines when the player wins or loses, but like I said, I just don't know how to code the actionPerformed method.

import java.util.Scanner;
import java.util.Random;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.Timer;
import javax.swing.border.BevelBorder;
import javax.swing.border.EtchedBorder;

public class Minesweeper extends JFrame implements ActionListener {
  public static Scanner myScanner = new Scanner (System.in);
  public static Random random = new Random ();

  JPanel pan1 = new JPanel ();
  JPanel pan2 = new JPanel ();
  JPanel pan3 = new JPanel ();
  JPanel pan1_1 = new JPanel ();
  JPanel pan1_2 = new JPanel ();
  JPanel pan1_3 = new JPanel ();
  JPanel pan1_3_D = new JPanel ();

  JButton button [][];
  JButton Save = new JButton ("Save");
  JButton Load = new JButton ("Load");
  JButton New = new JButton ("New");
  JButton One = new JButton ("1");
  JButton Two = new JButton ("2");
  JButton Three = new JButton ("3");
  JButton R = new JButton ("R");

  JLabel instruction = new JLabel ("Instruction Panel");//changeable
  JLabel timeLabel = new JLabel ();
  JLabel score = new JLabel ("Score");//it is only temporary

  static Timer timer;

  public Minesweeper (int row, int column) {
    button = new JButton [row][column];

    GridLayout gl_pan1 = new GridLayout (1, 3);
    GridLayout gl_pan2 = new GridLayout (row, column);
    GridLayout gl = new GridLayout (2, 1);//gl is for the 3 sub-panels in pan1
    GridLayout gl_pan1_3_D = new GridLayout (1, 4);

    pan1.setLayout (gl_pan1);
    pan2.setLayout (gl_pan2);
    pan1_1.setLayout (gl);
    pan1_2.setLayout (gl);
    pan1_3.setLayout (gl);
    pan1_3_D.setLayout (gl_pan1_3_D);

    setTitle ("Minesweeper");

    JPanel frameBorder = (JPanel)getContentPane ();
    frameBorder.setLayout (new BoxLayout (getContentPane (), BoxLayout.Y_AXIS));
    frameBorder.setBorder (new BevelBorder (BevelBorder.LOWERED));

    pan1.setBorder (new EtchedBorder (EtchedBorder.LOWERED));
    pan2.setBorder (new EtchedBorder (EtchedBorder.LOWERED));
    pan3.setBorder (new EtchedBorder (EtchedBorder.LOWERED));

    Save.setMinimumSize (new Dimension (25, 50));
    Save.setPreferredSize (new Dimension (25, 50));
    Save.setMaximumSize (new Dimension (25, 50));
    Load.setMinimumSize (new Dimension (25, 50));
    Load.setPreferredSize (new Dimension (25, 50));
    Load.setMaximumSize (new Dimension (25, 50));
    New.setMinimumSize (new Dimension (25, 50));
    New.setPreferredSize (new Dimension (25, 50));
    New.setMaximumSize (new Dimension (25, 50));

    for (int r = 0; r < row; r++) {
      for (int c = 0; c < column; c++) {
        button [r][c] = new JButton ();
        button [r][c].setMinimumSize (new Dimension (20, 40));
        button [r][c].setPreferredSize (new Dimension (20, 40));
        button [r][c].setMaximumSize (new Dimension (20, 40));
        pan2.add (button [r][c]);
        button [r][c].addActionListener (this);
      }
    }

    pan1_1.add (Save);
    pan1_1.add (Load);
    pan1_2.add (timeLabel);
    pan1_2.add (score);
    pan1_3.add (New);
    pan1_3_D.add (One);
    pan1_3_D.add (Two);
    pan1_3_D.add (Three);
    pan1_3_D.add (R);
    pan1_3.add (pan1_3_D);

    Save.addActionListener (this);
    Load.addActionListener (this);
    New.addActionListener (this);
    One.addActionListener (this);
    Two.addActionListener (this);
    Three.addActionListener (this);
    R.addActionListener (this);

    pan1.add (pan1_1);
    pan1.add (pan1_2);
    pan1.add (pan1_3);
    pan3.add (instruction);

    frameBorder.add (pan1);
    frameBorder.add (pan2);
    frameBorder.add (pan3);

    setContentPane (frameBorder);
    pack ();
    setVisible (true);
  }

  private void setTimerText (String sTime) {
    timeLabel.setText (sTime);
  }

  public void actionPerformed (ActionEvent e) {

  }

public static void main (String args []) throws Exception {
    int row = 10;
    int column = 10;

    new Minesweeper (row, column);

    int current_row = 5;
    int current_col = 5;
    int percentage = 10; //this is subject to change
    int number_of_mines = (int)(row*column*percentage*0.01);
    int mine_position [] = new int [number_of_mines];

    boolean revealed [][] = new boolean [row][column]; //whether the button is revealed or not
    boolean mines [][] = new boolean [row][column]; //whether there is a mine or not
    int revealed_number [][] = new int [row][column]; //the number that should appear on the revealed button

    int counter = 0; //this is for the gameEnd method
    boolean end = false; //whether the game is ended or not
}

public static void gameEnd (int row, int column, boolean revealed [][], boolean mines [][], int number_of_mines, int counter, boolean end) {
    for (int r = 0; r < row; r++) {
      for (int c = 0; c < column; c++) {
        if (mines [r][c] == false && revealed [r][c] == true) {
          counter++;
        }
    
        else if (mines [r][c] == true && revealed [r][c] == true) {
          System.out.println ("Sorry, you lost because you clicked on a mine.");
          end = true;
          break;
        }
      }
    }

    if (counter == (row*column-number_of_mines)) {
      System.out.println ("Congratulations! You won the game!");
      end = true;
    }
  }
}
m02ph3u5
  • 3,022
  • 7
  • 38
  • 51
Eileen Li
  • 43
  • 6

1 Answers1

0
timer.scheduleAtFixedRate(task, 1000, 1000);

where task is an instance of a class overriding java.util.TimerTask.

In its run() method (abstract in TimerTask), you would update the timer label (keep an eye on multithreading!), and in endGame, you call

timer.cancel();
Community
  • 1
  • 1
Aconcagua
  • 24,880
  • 4
  • 34
  • 59