战舰AI程序问题

时间:2015-05-16 07:18:06

标签: java arrays global-variables

我正在制作一个程序试图赢得我老师创建的战舰游戏(教师创建.jar文件here)当我添加多维数组(boolean [] [] used)作为全局变量时在我必须回到手动运行游戏之前,游戏只会自动运行几次迭代。我无法弄清楚这个数组的问题是什么,可能是某种溢出错误?

    public playerE() {
    this.playerName = "playerE";
  }
    //array used to keep track of used spaces  
     public boolean[][] used = new boolean[10][10];

  @Override
  public  void run() {
      while (game.inProgress()) {

            int col = (int) (Math.random()*10);
            int row = (int) (Math.random()*10); 
            boolean hit;

    //randomly fire at spaces in a checker board pattern.
            if (col%2 == 0 && row%2 != 0 && used[col][row] != true){
                hit = game.shoot(col, row);
                used[col][row] = true;

                //go into hunt mode
                if (hit == true){
                    huntNorth(col,row,hit);
                    huntSouth(col,row,hit);
                    huntEast(col,row,hit);
                    huntWest(col,row,hit);
                }   
    //randomly fire at spaces in a checker board pattern.       
            }else if(col%2 != 0 && row%2 == 0 && used[col][row] != true){
                hit = game.shoot(col, row);
                used[col][row] = true;

                //go into hunt mode
                if (hit == true){
                    huntNorth(col,row,hit);
                    huntSouth(col,row,hit);
                    huntEast(col,row,hit);
                    huntWest(col,row,hit);
                }       

            }

    }   //while game is in progress   

}//run method


//hunt for north blocks until it misses
public boolean huntNorth(int col, int row, boolean hit){

    try{
    row--;
    while (hit == true){
    hit = game.shoot(col, row--);
    used[col][row] = true;
    }
    return hit;
    }catch(Exception e){
    hit = false;
    return hit;
    }


}
//hunt for south blocks until it misses
public boolean huntSouth(int col, int row, boolean hit){

    try{
        row++;
        while (hit == true){
        hit = game.shoot(col, row++);
        used[col][row] = true;
        }
        return hit;
        }catch(Exception e){
        hit = false;
        return hit;
        }


}
//hunt for east blocks until it misses
public boolean huntEast(int col, int row, boolean hit){

    try{
        col--;
        while (hit == true){
        hit = game.shoot(col--, row);
        used[col][row] = true;
        }
        return hit;
        }catch(Exception e){
        hit = false;
        return hit;
        }


}
//hunt for west blocks until it misses
public boolean huntWest(int col, int row, boolean hit){

    try{
        col++;
        while (hit == true){
        hit = game.shoot(col++, row);
        used[col][row] = true;
        }
        return hit;
        }catch(Exception e){
        hit = false;
        return hit;
        }


}
}// class




  [1]: http://jeff.cis.cabrillo.edu/~jbergamini/12jdata/as14/as14.jar

0 个答案:

没有答案