我有这个 Game.java 类
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 测试这个类,但是 Game 类有 newGame () 方法,该方法除其他外负责制作 upNumMine ();它增加了相邻地雷数量的值。我正在尝试测试该方法以验证分支
if (randJ - 1> 0) {
this.ground [Rand i] [randJ - 1]. upNumOfMine ();
}
问题是我不知道如何强制我的值为 randJ = 1 以测试框 [Randi] [randJ-1] 会发生什么,其中 randJ-1 = 0。
我能做到这一点的唯一方法是修改原始代码输入插入手动RandI和RandJ,但我认为不是正确的做法。