-1
類
我這個Game.java類測試的Junit Randomic
package jmine;
import java.util.Random;
import jmine.exceptions.IllegalNumOfMine;
public class Game {
private Box[][] ground;
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;
Random randomizer = new Random();
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++){
if (i<this.width){
randI=i;
randJ=(this.width-1)-i;}
else{
randI=0;
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(true);
} 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(pY-1, pX);
}
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);
}
}
}
}
}
}
我一定要考該類使用JUnit但遊戲類有newGame()方法,尤其是負責制定upNumMine( );它增加了相鄰礦井的數量。 我試圖測試,以便在方法來驗證分支
if (randJ - 1> 0) {
this.ground [Rand i] [randJ - 1]. upNumOfMine();
}
的問題是,我不知道如何強制礦山的randJ = 1測試會發生什麼框中的值[Randi] [randJ-1],用randJ-1 = 0.
我能做到這一點的唯一方法就是修改原始代碼手動輸入insert into RandI和RandJ,但我不認爲是正確的方法來做到這一點。