|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
5 v' }9 [& N3 b+ v$ p1 dpublic void spreadMoney(int money){
d. ^$ Y8 J. t7 x // Randomly place money in moneySpace' ~' n; J0 W9 t" i. T$ E! H
for(int i = 0; i < money; i++){
, c, u0 q8 F$ E, ~/ p8 r
) k4 K/ `5 V' }) X3 @# }2 j // Choose coordinates
6 A& _* y9 y4 c. n( R; g1 w int x = (int)(Math.random()*(moneySpace.getSizeX()));
7 g9 y* V1 H. } int y = (int)(Math.random()*(moneySpace.getSizeY()));
( ]" b' f( C" c3 S. m4 ?) M4 m
6 B. ^: }. B9 w4 @ o5 b // Get the value of the object at those coordinates
9 S! P" `! J" d, e int I;
( f4 C. Z5 Z: Q! l9 f; I if(moneySpace.getObjectAt(x,y)!= null){$ _& L4 f. j' C! ?
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
6 J. U' t7 e! _3 |9 \* z% g }
3 E$ P" g2 j- j' X- v" @3 W else{/ j9 M& c+ @) p$ {' p
I = 0;1 G( ]% [3 b. h% g% }
}
+ M+ V: F3 O, B: c | // Replace the Integer object with another one with the new value
D# T5 t5 V$ | W. w moneySpace.putObjectAt(x,y,new Integer(I + 1));
/ W8 u' l7 {* l }' c' }" c0 ]) l0 d
这里面if(moneySpace.getObjectAt(x,y)!= null){
4 u# b6 [6 C' ]# }' t I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
* {7 i1 E9 w# Q }4 H. Z. F# B; A$ i$ m# [
else{
" i% K4 a1 H4 f t3 [ I = 0;
- F1 J5 F( v$ w3 I3 S' T3 \是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?
; A9 Q6 }9 y% j2 p0 j0 d初学者,还请见谅! |
|