001package minesweeper;
002
003/**
004 * RandPlace- looks convoluted; is solely for deciding where mines are- randomly placing them, if you will
005 *
006 * @author Kevin Khaghani // 1907024@wpsstudent.com
007 */
008public class RandPlace {
009    private int row, col, numTrue;
010    private boolean[] truthArr;
011
012    /**
013     * This whole class is to create a boolean array
014     *
015     * @param r,      rows of the array
016     * @param c,      columns of the array
017     * @param number; for Minesweeper this is the number of mines to be placed.
018     * @throws IllegalArgumentException if numTrue is ever be greater than (row * col)
019     */
020    public RandPlace(int r, int c, int number) {
021        if (numTrue > row * col) {
022            throw new IllegalArgumentException("Error: array cannot contain that many values");
023        }
024        row = r;
025        col = c;
026        numTrue = number;
027    }
028
029    /**
030     * numContains counts the number of "true" or "false" (truth) in an array
031     *
032     * @param truth, a boolean state, "true" or "false"
033     * @param array, any boolean[] array
034     * @return the number of times truth appears in the array
035     */
036    public int numContains(boolean truth, boolean[] array) {
037        int count = 0;
038        for (int i = 0; i < array.length; i++) {
039            if (truth == array[i]) {
040                count++;
041            }
042        }
043        return count;
044    }
045
046    public int numContains(boolean truth, boolean[][] array) {
047        int count = 0;
048        for (int i = 0; i < array.length; i++) {
049            for (int j = 0; j < array[i].length; j++) {
050                if (truth == array[i][j]) {
051                    count++;
052                }
053            }
054        }
055        return count;
056    }
057
058    /**
059     * Bread and butter of the class: creates a valid array (one long one) of [row*col] length, with [numTrue] "true" values
060     *
061     * @return the array
062     */
063    public boolean[] valid() {
064        boolean[] validArr = new boolean[row * col];
065        for (int i = 0; i < validArr.length; i++) {
066            validArr[i] = false;
067        } //all values in validArr initialized to false
068
069        for (int i = 0; i < numTrue; i++) {
070            int slotsAvailable = numContains(false, validArr); //4x4, 5 should return 16 at i=0
071
072            int randIndex = (int) (Math.random() * slotsAvailable); //0-15
073
074            for (int j = 0; j < validArr.length; j++) {
075                if (j == randIndex) {
076                    if (validArr[j] == false) {
077                        validArr[j] = true;
078                    } else {
079                        i--;
080                    }
081                }
082            }
083        }
084        truthArr = validArr;
085        return validArr;
086    }
087
088    /**
089     * Returns the number of "true" values in the truth array
090     *
091     * @return number of "true" values in the array
092     */
093    public int arrTruth() {
094        return numContains(true, truthArr);
095    }
096
097    /**
098     * Generates a usable 2D array from the 1D array truthArr- convoluted? Granted.
099     *
100     * @return the 2D array
101     */
102    public boolean[][] convert2D() {
103        boolean[][] truth2D = new boolean[row][col];
104
105        for (int i = 0; i < row; i++) {
106            for (int j = 0; j < col; j++) {
107                truth2D[i][j] = truthArr[i * col + j];
108            }
109        }
110
111        return truth2D;
112    }
113
114    /**
115     * Generates a usable 2D array from any passed boolean[] array- such as the one valid() makes.
116     *
117     * @return the 2D array
118     */
119    public boolean[][] generate2D() {
120        boolean[] arr = this.valid();
121        boolean[][] truth2D = new boolean[row][col];
122
123        for (int i = 0; i < row; i++) {
124            for (int j = 0; j < col; j++) {
125                truth2D[i][j] = arr[i * col + j];
126            }
127        }
128
129        return truth2D;
130    }
131}