001package minesweeper;
002
003//import java.awt.*;
004
005import java.awt.event.*;
006import javax.swing.*;
007
008import org.apache.logging.log4j.*;
009
010/**
011 * DESCRIBE {@link Tile} HERE.
012 *
013 * @author 2017-2018 APCS
014 * @author ADD @author TAG FOR EVERYONE WHO CONTRIBUTED TO THIS FILE
015 * @author Kevin Khaghani // 1907024@wpsstudent.com
016 * @author David C. Petty // https://github.com/wps-dpetty
017 */
018public class Tile extends JButton implements ActionListener {
019    /**
020     * log4j {@link Logger}.
021     */
022    private static Logger logger = LogManager.getLogger(Minesweeper.SHORT);
023
024    private int row;
025    private int column;
026    private boolean isMine, isClicked;
027    private int tileValue;
028
029    public Tile(int r, int c, boolean m) {
030        logger.info(this);
031        addActionListener(this);
032        row = r;
033        column = c;
034        tileValue = 0;
035        isMine = m;
036        isClicked = false;
037    }
038
039    public void actionPerformed(ActionEvent e) {
040        //System.out.print(row + " " + column);
041        Main.startTimer(false);
042    }
043
044    public boolean getIsClicked() {
045        return isClicked;
046    }
047
048    public int getRow() {
049        return row;
050    }
051
052    public int getCol() {
053        return column;
054    }
055
056    public boolean getIsMine() {
057        return isMine;
058    }
059
060    /**
061     * Method for Main to use to read the value of the tile and, in turn, display it on the eventual board
062     *
063     * @return the value on the tile
064     */
065    public int getTileValue() { //for now, if it's 0, no mines around, do NOT display 0- display a blank tile.
066        return tileValue;
067    }
068
069    /**
070     * Allows Main (or wherever the board is initialized) to modify the value based on nearby mines
071     *
072     * @param a = the value to which tileValue will be set
073     */
074    public void setTileValue(int a) {
075        tileValue = a;
076    }
077
078    /**
079     * Allows getMines() class in Minesweeper to assign correct tile value after all the mines are created
080     * to all the non-mine tiles
081     */
082    public void setTileValue() {
083        int count = 0;
084        Tile[][] gameArray = Minesweeper.getGameArray();
085        for (int i = row - 1; i < row + 2; i++) {
086            for (int j = column - 1; j < column + 2; column++) {
087                if (i >= 0 && i < 16 && j >= 0 && j < 16 && i != row && j != column && gameArray[i][j].getIsMine()) {
088                    count++;
089                }
090            }
091        }
092        tileValue = count;
093    }
094
095    /**
096     * Another way to modify the tileValue, incrementing by one- use this instead of the other if it suits the board-init code better
097     */
098    public void incrementTile() {
099        tileValue++;
100    }
101
102    public String toString() {
103        return "Tile: mine=" + isMine + ", value=" + tileValue;
104    }
105}