|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中0 ]+ t; q: u9 f3 u
public void spreadMoney(int money){
9 b& K' N5 `* d2 h. ^ // Randomly place money in moneySpace
/ z0 ^" d: G% B' Y, Z for(int i = 0; i < money; i++){
' _, o7 ?3 V2 I% v' Z9 N* m
2 ]1 }3 ], o( i4 n+ O, q+ A# c // Choose coordinates4 c; g% `7 H! _$ |9 V w4 [
int x = (int)(Math.random()*(moneySpace.getSizeX()));) @+ o( P6 v5 b* w' n
int y = (int)(Math.random()*(moneySpace.getSizeY()));
& L0 Q; q. a1 h+ O
, V L" g9 Q& i" f# t // Get the value of the object at those coordinates5 }7 S- s. ?! a" A$ n m8 i* X( {
int I;
8 |: i$ p/ J( K( U' ~ if(moneySpace.getObjectAt(x,y)!= null){
( n# a, }5 s/ L2 L3 N0 @% D I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();) ]& u4 D7 W, Q* I9 Q) t
}
# Y3 g" w. O. f( A& X else{
( o+ t4 i" h9 X9 `3 u I = 0;
$ F9 q! N1 D1 m/ M4 | }5 Q) Q; r4 V# P5 W
// Replace the Integer object with another one with the new value5 @% S; P9 x9 C, H& ` y, i
moneySpace.putObjectAt(x,y,new Integer(I + 1));
* M2 i v2 y& d! `9 K }
, F* a" `; x$ M5 T2 O- R9 j这里面if(moneySpace.getObjectAt(x,y)!= null){( z7 @4 n1 k0 u9 w+ `9 t
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
0 L7 _: C: ]5 U( ^/ p; x2 i }5 d9 {# t) }8 L$ e
else{& Y. A- b1 t+ B
I = 0;
! h% t* a+ x! l- y! C是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?5 l/ V& a# A* q, v3 m
初学者,还请见谅! |
|