package jmine;
import java.util.Random;
import jmine.exceptions.IllegalNumOfMine;
public class Game {
private int width;
private int height;
private int numOfMine;
private boolean gameOver;
public Game(int pWidth, int pHeight, int pNumOfMine) throws IllegalNumOfMine {
if (pNumOfMine > (pWidth * pHeight)) {
throw new IllegalNumOfMine();
}
this.
ground = new Box[pHeight
][pWidth
];
this.width = pWidth;
this.height = pHeight;
this.numOfMine = pNumOfMine;
this.gameOver = false;
this.newGame();
}
public void newGame() {
this.gameOver=false;
for (int i = 0; i < this.width; i++) {
for (int j = 0; j < this.height; j++) {
this.
ground[j
][i
] = new Box();
}
}
int randI = 0;
int randJ = 0;
for (int i = 0; i < this.numOfMine; i++) {
do {
randI = randomizer.nextInt(this.height);
randJ = randomizer.nextInt(this.width);
} while (this.ground[randI][randJ].isMine());
this.ground[randI][randJ].setMine(true);
if (randI - 1 >= 0 && randJ - 1 >= 0) {
this.ground[randI - 1][randJ - 1].upNumOfMine();
}
if (randI - 1 >= 0) {
this.ground[randI - 1][randJ].upNumOfMine();
}
if (randI - 1 >= 0 && randJ + 1 < this.width) {
this.ground[randI - 1][randJ + 1].upNumOfMine();
}
if (randJ - 1 >= 0) {
this.ground[randI][randJ - 1].upNumOfMine();
}
if (randJ + 1 < this.width) {
this.ground[randI][randJ + 1].upNumOfMine();
}
if (randI + 1 < this.height && randJ - 1 >= 0) {
this.ground[randI + 1][randJ - 1].upNumOfMine();
}
if (randI + 1 < this.height) {
this.ground[randI + 1][randJ].upNumOfMine();
}
if (randI + 1 < this.height && randJ + 1 < this.width) {
this.ground[randI + 1][randJ + 1].upNumOfMine();
}
}
}
public Box[][] getGround
() {
return this.ground;
}
public int getHeight() {
return this.height;
}
public int getNumOfMine() {
return this.numOfMine;
}
public int getWidth() {
return this.width;
}
public boolean getGameOver() {
return this.gameOver;
}
public void flag(int pX, int pY) {
if (this.ground[pX][pY].isCover()) {
if (this.ground[pX][pY].isFlagged()) {
this.ground[pX][pY].setFlagged(false);
} else {
this.ground[pX][pY].setFlagged(true);
}
}
}
public boolean isWinner(){
for(int i=0; i<this.width; i++){
for(int j=0; j<this.height; j++){
if(this.ground[i][j].isCover()){
if(!this.ground[i][j].isMine()){
return false;
}
}
}
}
return true;
}
public void unconver(int pX, int pY) {
if (this.ground[pX][pY].isCover() && !this.ground[pX][pY].isFlagged()) {
if (this.ground[pX][pY].isMine()) {
this.gameOver = true;
} else {
this.ground[pX][pY].setCover(false);
if (this.ground[pX][pY].getNumOfMine() == 0) {
if (pX - 1 >= 0 && pY - 1 >= 0) {
this.unconver(pX - 1, pY - 1);
}
if (pX - 1 >= 0) {
this.unconver(pX - 1, pY);
}
if (pX - 1 >= 0 && pY + 1 < this.width) {
this.unconver(pX - 1, pY + 1);
}
if (pY - 1 >= 0) {
this.unconver(pX, pY - 1);
}
if (pY + 1 < this.width) {
this.unconver(pX, pY + 1);
}
if (pX + 1 < this.height && pY - 1 >= 0) {
this.unconver(pX + 1, pY - 1);
}
if (pX + 1 < this.height) {
this.unconver(pX + 1, pY);
}
if (pX + 1 < this.height && pY + 1 < this.width) {
this.unconver(pX + 1, pY + 1);
}
}
}
}
}
}