refactor redundant method calls
This commit is contained in:
parent
f15438363d
commit
b22cad26a2
@ -1,144 +1,72 @@
|
|||||||
package com.shr4pnel.minesweeper;
|
package com.shr4pnel.minesweeper;
|
||||||
|
|
||||||
//class ValidTileBean {
|
|
||||||
// boolean top;
|
|
||||||
// boolean topRight;
|
|
||||||
// boolean right;
|
|
||||||
// boolean bottomRight;
|
|
||||||
// boolean bottom;
|
|
||||||
// boolean bottomLeft;
|
|
||||||
// boolean left;
|
|
||||||
// boolean topLeft;
|
|
||||||
//}
|
|
||||||
|
|
||||||
|
|
||||||
public class GridWrapper {
|
public class GridWrapper {
|
||||||
final boolean[][] grid = new boolean[30][16];
|
private static final int COLUMNS = 30;
|
||||||
|
private static final int ROWS = 16;
|
||||||
|
final boolean[][] grid = new boolean[COLUMNS][ROWS];
|
||||||
private int currentColumn;
|
private int currentColumn;
|
||||||
private int currentRow;
|
private int currentRow;
|
||||||
|
|
||||||
|
|
||||||
public GridWrapper() {
|
public GridWrapper() {
|
||||||
this.currentColumn = 0;
|
this.currentColumn = 0;
|
||||||
this.currentRow = 0;
|
this.currentRow = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
GridWrapper atColumn(int column) {
|
public GridWrapper atColumn(int column) {
|
||||||
this.currentColumn = column;
|
this.currentColumn = column;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
GridWrapper atRow(int row) {
|
public GridWrapper atRow(int row) {
|
||||||
this.currentRow = row;
|
this.currentRow = row;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
void setBomb() {
|
public void setBomb() {
|
||||||
grid[this.currentColumn][this.currentRow] = true;
|
if (isValid(currentColumn, currentRow)) {
|
||||||
|
grid[currentColumn][currentRow] = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean isBomb() {
|
public boolean isBomb() {
|
||||||
return grid[currentColumn][currentRow];
|
return isValid(currentColumn, currentRow) && grid[currentColumn][currentRow];
|
||||||
}
|
}
|
||||||
|
|
||||||
int adjacentBombCount() {
|
public int adjacentBombCount() {
|
||||||
boolean checkLeft, checkRight, checkUp, checkDown;
|
|
||||||
int count = 0;
|
int count = 0;
|
||||||
checkLeft = currentColumn > 0;
|
|
||||||
checkRight = currentColumn < 29;
|
|
||||||
checkUp = currentRow > 0;
|
|
||||||
checkDown = currentRow < 15;
|
|
||||||
|
|
||||||
if (checkLeft) {
|
if (isBombAt(currentColumn - 1, currentRow - 1)) {
|
||||||
count += left();
|
count++;
|
||||||
}
|
}
|
||||||
if (checkRight) {
|
if (isBombAt(currentColumn, currentRow - 1)) {
|
||||||
count += right();
|
count++;
|
||||||
}
|
}
|
||||||
if (checkUp) {
|
if (isBombAt(currentColumn + 1, currentRow - 1)) {
|
||||||
count += top();
|
count++;
|
||||||
}
|
}
|
||||||
if (checkDown) {
|
if (isBombAt(currentColumn - 1, currentRow)) {
|
||||||
count += bottom();
|
count++;
|
||||||
}
|
}
|
||||||
if (checkUp && checkLeft) {
|
if (isBombAt(currentColumn + 1, currentRow)) {
|
||||||
count += topLeft();
|
count++;
|
||||||
}
|
}
|
||||||
|
if (isBombAt(currentColumn - 1, currentRow + 1)) {
|
||||||
if (checkUp && checkRight) {
|
count++;
|
||||||
count += topRight();
|
|
||||||
}
|
}
|
||||||
if (checkDown && checkRight) {
|
if (isBombAt(currentColumn, currentRow + 1)) {
|
||||||
count += bottomRight();
|
count++;
|
||||||
}
|
}
|
||||||
if (checkDown && checkLeft) {
|
if (isBombAt(currentColumn + 1, currentRow + 1)) {
|
||||||
count += bottomLeft();
|
count++;
|
||||||
}
|
}
|
||||||
|
|
||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
private int top() {
|
private boolean isBombAt(int column, int row) {
|
||||||
return isValid() && currentRow > 0 ? (grid[currentColumn][currentRow - 1] ? 1 : 0) : 0;
|
return isValid(column, row) && grid[column][row];
|
||||||
}
|
}
|
||||||
|
|
||||||
private int topRight() {
|
private boolean isValid(int column, int row) {
|
||||||
return isValid() && currentColumn < 29 && currentRow > 0 ?
|
return column >= 0 && column < COLUMNS && row >= 0 && row < ROWS;
|
||||||
(grid[currentColumn + 1][currentRow - 1] ? 1 : 0) : 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
private int right() {
|
|
||||||
return isValid() && currentColumn < 29 ? (grid[currentColumn + 1][currentRow] ? 1 : 0) : 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
private int bottomRight() {
|
|
||||||
return isValid() && currentColumn < 29 && currentRow < 15 ?
|
|
||||||
(grid[currentColumn + 1][currentRow + 1] ? 1 : 0) : 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
private int bottom() {
|
|
||||||
return isValid() && currentRow < 15 ? (grid[currentColumn][currentRow + 1] ? 1 : 0) : 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
private int bottomLeft() {
|
|
||||||
return isValid() && currentColumn > 0 && currentRow < 15 ?
|
|
||||||
(grid[currentColumn - 1][currentRow + 1] ? 1 : 0) : 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
private int left() {
|
|
||||||
return isValid() && currentColumn > 0 ? (grid[currentColumn - 1][currentRow] ? 1 : 0) : 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
private int topLeft() {
|
|
||||||
return isValid() && currentColumn > 0 && currentRow > 0 ?
|
|
||||||
(grid[currentColumn - 1][currentRow - 1] ? 1 : 0) : 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
boolean isValid() {
|
|
||||||
return this.currentColumn >= 0 && this.currentColumn < 30 && this.currentRow >= 0 &&
|
|
||||||
this.currentRow < 16;
|
|
||||||
}
|
|
||||||
|
|
||||||
// ValidTileBean getBounds() {
|
|
||||||
// ValidTileBean validTiles = new ValidTileBean();
|
|
||||||
// validTiles.top = top() != 0;
|
|
||||||
// validTiles.topRight = topRight() != 0;
|
|
||||||
// validTiles.right = right() != 0;
|
|
||||||
// validTiles.bottomRight = bottomRight() != 0;
|
|
||||||
// validTiles.bottom = bottom() != 0;
|
|
||||||
// validTiles.bottomLeft = bottomLeft() != 0;
|
|
||||||
// validTiles.left = left() != 0;
|
|
||||||
// validTiles.topLeft = topLeft() != 0;
|
|
||||||
// return validTiles;
|
|
||||||
// }
|
|
||||||
|
|
||||||
void printGrid() {
|
|
||||||
int i, j;
|
|
||||||
for (i = 0; i < 30; ++i) {
|
|
||||||
for (j = 0; j < 16; ++j) {
|
|
||||||
System.out.printf(this.atColumn(i).atRow(j).isBomb() + " ");
|
|
||||||
}
|
|
||||||
System.out.println();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user