2013-03-09 179 views
-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,但我不认为是正确的方法来做到这一点。

回答

2

您可以将一个Randomizer对象注入Game类。然后你可以模拟随机器并测试游戏类。