Android Open Source - android-simlple-minefield Mine Grid Utils






From Project

Back to project page android-simlple-minefield.

License

The source code is released under:

Apache License

If you think the Android project android-simlple-minefield listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.trabo.minefield.utils;
//  w  w w .  jav a2s.com
import android.graphics.Point;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @author Andriy Petruk <andrii.petruk{at}gmail.com>
 * @date 23.06.14.
 */
public final class MineGridUtils {
    private static final Random sRandom = new Random();
    public static final int MINE_CELL = -1;

    public static int[][] generateMineField(int height, int width, int numBombs) {
        int[][] mineFieldData = new int[height][width];
        int counter = 0;

        while (counter < numBombs) {
            int index = sRandom.nextInt(width * height);
            int y = index / width;
            int x = index % width;

            if (mineFieldData[y][x] != MINE_CELL) {
                mineFieldData[y][x] = MINE_CELL;
                counter++;
            }
        }

        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                if (mineFieldData[y][x] == MINE_CELL) {
                    incrementCell(mineFieldData, y, x + 1, width, height);
                    incrementCell(mineFieldData, y, x - 1, width, height);
                    incrementCell(mineFieldData, y + 1, x, width, height);
                    incrementCell(mineFieldData, y + 1, x + 1, width, height);
                    incrementCell(mineFieldData, y + 1, x - 1, width, height);
                    incrementCell(mineFieldData, y - 1, x, width, height);
                    incrementCell(mineFieldData, y - 1, x + 1, width, height);
                    incrementCell(mineFieldData, y - 1, x - 1, width, height);
                }
            }
        }

        return mineFieldData;
    }

    private static void incrementCell(int[][] mineFieldData, int y, int x, int width, int height) {
        if (!isValidPoint(x, y, width, height) || mineFieldData[y][x] == MINE_CELL) {
            return;
        }
        mineFieldData[y][x]++;
    }

    public static List<Point> getNeighbourPointsInGrid(Point cell, int width, int height) {
        List<Point> result = new ArrayList<Point>(8);

        if (isValidPoint(cell.x - 1, cell.y, width, height)) {
            result.add(new Point(cell.x - 1, cell.y));
        }

        if (isValidPoint(cell.x + 1, cell.y, width, height)) {
            result.add(new Point(cell.x + 1, cell.y));
        }

        if (isValidPoint(cell.x, cell.y - 1, width, height)) {
            result.add(new Point(cell.x, cell.y - 1));
        }

        if (isValidPoint(cell.x, cell.y + 1, width, height)) {
            result.add(new Point(cell.x, cell.y + 1));
        }

        if (isValidPoint(cell.x - 1, cell.y, width, height)) {
            result.add(new Point(cell.x - 1, cell.y - 1));
        }

        if (isValidPoint(cell.x + 1, cell.y - 1, width, height)) {
            result.add(new Point(cell.x + 1, cell.y - 1));
        }

        if (isValidPoint(cell.x - 1, cell.y + 1, width, height)) {
            result.add(new Point(cell.x - 1, cell.y + 1));
        }

        if (isValidPoint(cell.x + 1, cell.y + 1, width, height)) {
            result.add(new Point(cell.x + 1, cell.y + 1));
        }

        return result;
    }

    public static boolean isValidPoint(int x, int y, int width, int height) {
        return y >= 0 && x >= 0 && x < width && y < height;
    }
}




Java Source Code List

com.trabo.minefield.CellOnTouchListener.java
com.trabo.minefield.GameActivity.java
com.trabo.minefield.HighScoresActivity.java
com.trabo.minefield.MainActivity.java
com.trabo.minefield.MineFieldListener.java
com.trabo.minefield.MineModel.java
com.trabo.minefield.MinefieldAdapter.java
com.trabo.minefield.MinefieldView.java
com.trabo.minefield.utils.AppContract.java
com.trabo.minefield.utils.AppUtils.java
com.trabo.minefield.utils.MineGridUtils.java