示例#1
0
  /**
   * Method to print the grid as it would appear for a normal gameplay
   *
   * @param grid - Grid to be processed and printed
   */
  public static void consolePrintGame(MineGrid grid) {

    String format = "%-3s";

    System.out.print("   ");
    for (int i = 0; i < grid.getWidth(); i++) { // loop to print the column numbers
      System.out.print(String.format(format, i));
    }
    System.out.println();
    for (int row = 0; row < grid.getWidth(); row++) { // loop to print the grid contents
      System.out.print(String.format(format, row)); // printing the row number
      for (int col = 0; col < grid.getWidth(); col++) {
        MineCell cell = grid.getCell(row, col);

        if (cell.isVisible() && cell.getNeighbouringMines() == 0) {
          System.out.print(String.format(format, "~"));
        } else if (cell.isVisible()) {
          System.out.print(String.format(format, grid.getCell(row, col).getNeighbouringMines()));
        } else {
          System.out.print(String.format(format, "."));
        }
      }
      System.out.println();
    }
    System.out.println();
  }
示例#2
0
  /**
   * Method to print the game when the game has ended
   *
   * @param grid : Grid to be printed.
   */
  public static void consolePrintGameOver(MineGrid grid) {

    /*
     * @ - successfully flagged mine
     * X - unsuccessfully flagged mine
     * . - not clicked
     * # - mine not flagged or clicked
     * ~ - clicked and revealed but not adjacent to a mine
     * 'number' - number of neighbouring mines
     */
    String format = "%-3s";

    System.out.print("``````-------Game Over!!!-------``````\n\n   ");
    for (int i = 0; i < grid.getWidth(); i++) { // loop to print the column numbers
      System.out.print(String.format(format, i));
    }
    System.out.println();
    for (int row = 0; row < grid.getWidth(); row++) { // loop to print the grid contents
      System.out.print(String.format(format, row)); // printing the row number
      for (int col = 0; col < grid.getWidth(); col++) {
        MineCell cell = grid.getCell(row, col);
        if (cell.hasMine() && cell.isFlagged()) { // if its flagged and has a mine
          System.out.print(String.format(format, "@"));
        } else if (!cell.hasMine() && cell.isFlagged()) { // if its flagged but does not have a mine
          System.out.print(String.format(format, "X"));
        } else if (cell.hasMine() && cell.isClicked()) { // if its flagged but does not have a mine
          System.out.print(String.format(format, "X"));
        } else if (cell.hasMine()) {
          System.out.print(String.format(format, "#"));
        } else if (cell.isVisible()
            && cell.getNeighbouringMines() == 0) { // if its visible and has neighbouring mines
          System.out.print(String.format(format, "~"));
        } else if (cell.isVisible()) {
          System.out.print(
              String.format(
                  format,
                  grid.getCell(row, col)
                      .getNeighbouringMines())); // if its visible with no neighbouring mines
        } else {
          System.out.print(String.format(format, ".")); // if its not visible
        }
      }
      System.out.println();
    }
    System.out.println();
  }
示例#3
0
  /**
   * Method to implement a left click on the grid
   *
   * @param newGrid - Grid to be processed
   * @param row - The row number in the grid
   * @param col - The column number in the grid
   */
  public static void leftClick(MineGrid newGrid, int row, int col) {

    /* Need to check:
     * (a) if the cell has a mine first, if so, game is over and all mines are revealed
     * (b) if the cell is next to a mine, just reveal the number of mines adjacent to it,
     * 		i.e, if neighbouring mines is not equal to one, set visibility of cell to true
     * (c) if the cell has no adjacent mines then recursively check and neighbouring cells
     * 		until you get to a cell with a number greater than one.
     */

    // (a)
    if (newGrid.getCell(row, col).hasMine()) {
      newGrid.getCell(row, col).setClicked();
      newGrid.setLoseStatus(true);
    }

    // (b) and (c)
    else {
      recursiveReveal(newGrid, row, col);
      return;
    }
  }
示例#4
0
  /**
   * Method to recursively set the visibility status of cells with no neighbouring mines to true
   *
   * @param newGrid Grid to be processed to reveal cells
   * @param col Column number
   * @param row Row number
   */
  private static void recursiveReveal(MineGrid newGrid, int row, int col) {
    MineCell cell = newGrid.getCell(row, col);

    // base case, if the cell has already been clicked, return
    if (cell.isClicked()) return;
    cell.setVisibility(true);
    cell.setClicked();

    // base case, when the cell has a number, STOP and RETURN
    if (cell.hasNeighbouringMines()) {
      return;
    } else {
      /* 1.1-if it is not in the last row
       *	check the cell below
       *
       * 1.2-if it is not in first col
       * 	check the bottom left cell
       *
       * 1.3-if it is not in last col
       *  check the bottom right cell
       *
       * 2.1-if it is not in the 1st row
       *	check the top cell
       *
       * 2.2-if it is not in the first col
       * 	check the top left cell
       *
       * 2.3-if it is not in the last col
       * 	check the top right cell
       *
       * 3.1-if it is not in the 1st col
       * 	check left cell
       *
       * 3.2-if it is not in the last col
       * 	check the right cell
       */

      int gridLen = newGrid.getWidth();

      // 1.1
      if (row != gridLen - 1) {
        recursiveReveal(newGrid, row + 1, col);
        // 1.2
        if (col != 0) {
          recursiveReveal(newGrid, row + 1, col - 1);
        }
        // 1.3
        if (col != gridLen - 1) {
          recursiveReveal(newGrid, row + 1, col + 1);
        }
      }
      // 2.1
      if (row != 0) {
        recursiveReveal(newGrid, row - 1, col);
        // 2.2
        if (col != 0) {
          recursiveReveal(newGrid, row - 1, col - 1);
        }
        // 2.3
        if (col != gridLen - 1) {
          recursiveReveal(newGrid, row - 1, col + 1);
        }
      }
      // 3.1
      if (col != 0) {
        recursiveReveal(newGrid, row, col - 1);
      }
      // 3.2
      if (col != gridLen - 1) {
        recursiveReveal(newGrid, row, col + 1);
      }
    }
  }