001/*
002 * Minesweeper.java
003 */
004package minesweeper;
005
006import org.apache.logging.log4j.*;
007
008/**
009 * DESCRIBE {@link Minesweeper} HERE.
010 *
011 * @author 2017-2018 APCS
012 * @author ADD @author TAG FOR EVERYONE WHO CONTRIBUTED TO THIS FILE
013 * @author David C. Petty // https://github.com/wps-dpetty
014 */
015public class Minesweeper {
016    /**
017     * LONG name of this project.
018     */
019    public static final String LONG = "Minesweeper";
020    /**
021     * SHORT name of this project.
022     */
023    public static final String SHORT = "MS";
024    /**
025     * log4j {@link Logger}.
026     */
027    private static Logger logger = LogManager.getLogger(SHORT);
028
029    private enum gameState {RUNNING, OVER_WON, OVER_LOST}
030
031    ;
032    private gameState stateOfGame;
033    private int numberFlags;
034    private static Tile[][] gameArray;
035
036    /**
037     * No-args constructor: game is running, do not start with any flags.
038     */
039    public Minesweeper() {
040        logger.info("{}:", getClass().getName());
041        stateOfGame = gameState.RUNNING;
042        numberFlags = 0;
043        gameArray = this.getMines();
044    }
045
046    /**
047     * Adds to a flag when a flag tile is uncovered. Invoked by {@link Tile}.
048     * Tile class. Currently private.
049     */
050    private void addFlag() {
051        numberFlags++;
052    }
053
054    public static Tile[][] getGameArray() {
055        return gameArray;
056    }
057
058    public static Tile[][] getMines() {
059        boolean[][] mineSpots = (new RandPlace(16, 30, 99)).generate2D();
060        Tile[][] tileArray = new Tile[16][30];
061        for (int i = 0; i < 16; i++)
062            for (int k = 0; k < 30; k++) {
063                tileArray[i][k] = new Tile(i, k, mineSpots[i][k]);
064
065            }
066        for (Tile[] rows : tileArray) {
067            for (Tile tile : rows) {
068                if (!tile.getIsMine())
069                    tile.setTileValue();
070            }
071        }
072        return tileArray;
073    }
074
075    /**
076     * A tile has been triggered - this method will handle it (WORK IN PROGRESS)
077     *
078     * @param other NOT SURE WHAT OTHER IS
079     */
080    public void tileHandled(Tile other) {
081/*
082        Tile.State handling = other.getState();
083        if (Tile.State.MINE.equals(handling)) {
084            //TODO: handle mine-hit
085        }
086        else if (Tile.State.FLAG.equals(handling)) {
087            addFlag();
088        }
089        else {
090            //TODO: handle showing of tile
091        }
092*/
093        winCondition();
094    }
095
096    /**
097     * Has the state of the game changed yet?
098     */
099    public void winCondition() {
100        for (int i = 0; i < gameArray.length; i++)
101            for (int k = 0; k < gameArray[0].length; k++) {
102                if (gameArray[i][k].getIsClicked() && gameArray[i][k].getIsMine())
103                    stateOfGame = gameState.OVER_LOST;
104                if (gameArray[i][k].getIsClicked() && !(gameArray[i][k].getIsMine()))
105                    stateOfGame = gameState.OVER_WON;
106            }
107
108
109    }
110}