我試圖做一個吃豆子,所以即時通訊使用數組[8] [8],其中我有一個PacMan,2幽靈,牆壁和吃豆子可以吃的點。隨着pacman吃點你開始得到空的空間。隨機函數不工作,因爲我希望
因此,我決定做一個函數隨機ramdonly分配水果只有空的空間,但問題是,更多的空格,你得到的水果開始瘋狂,並出現在我的數組。儘管我有空的空間,但我希望總能獲得成果。
這裏是我使用的方法,請幫助。
public void AppearFruit()
{
for(int i= 0; i<_world.length; i++){
for(int j= 0; j<_world.length; j++){
Random as = new Random();
int fru = as.nextInt(20);
if(fru==10){
if(_world[i][j] instanceof Empty){
_world[i][j] = new Fruit(i,j);
}
}
}
}
}
public void DisappearFruit()
{
for(int i= 0; i<_world.length; i++){
for(int j= 0; j<_world.length; j++){
if (_world[i][j] instanceof Fruit){
if(contfru>=3) {
_mundo[i][j] = new Empty(i,j);
contfru=0;
}
}
}
}
}
看起來像功課,感覺就像作業...是功課嗎?不是,但認真,你能用英文至少發佈你的代碼嗎? – 2012-02-13 08:11:02
是的,我可以,在這裏: – Sabaspro 2012-02-13 08:14:24
對不起,我們不知道如何以及何時調用這兩種方法,很難說出發生了什麼。順便說一句,方法名稱不應以大寫字母開頭。在你的循環中,罪人循環應該測試條件j <_world [i] .length。這隻適用於你的數組是「方形」的。 – 2012-02-13 08:21:08