|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中% i) {* n- I7 ^' U
public void spreadMoney(int money){
# `. k8 ~+ \ F$ b+ E& p // Randomly place money in moneySpace' Y Z, I! H* U: e! R' [1 _; S2 I8 d, d
for(int i = 0; i < money; i++){% i y7 R9 U6 w# _0 `( S$ m
! X0 K7 X7 m/ ?& |0 L // Choose coordinates
. O+ c* j6 k' l+ e8 U1 o! ?; t( O int x = (int)(Math.random()*(moneySpace.getSizeX()));+ j6 l0 ~: ^4 {' X' p8 o9 p S
int y = (int)(Math.random()*(moneySpace.getSizeY()));6 S* O2 \- w( j1 q: ~
! q" |2 L' ]1 i7 G // Get the value of the object at those coordinates
8 W' i3 x1 A1 ?7 R) ~ int I;' r, A4 {' S0 j6 L
if(moneySpace.getObjectAt(x,y)!= null){
9 @+ `$ [4 y' D0 J# J- L) s/ U( R I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
% S. A5 l+ L, M7 V0 q: } }' O/ ]6 r6 V- [; @2 K8 h+ P
else{2 s" ]0 P; v9 c$ t) M7 n- e
I = 0;2 z% Y# x1 [* W! r" G
}
0 Y8 Y X! l$ q // Replace the Integer object with another one with the new value- f) ~9 T" u E% e
moneySpace.putObjectAt(x,y,new Integer(I + 1));
4 \3 D, Y! o- w, U; X! z }
8 c, F' ]& L8 W# M这里面if(moneySpace.getObjectAt(x,y)!= null){& }0 B/ Q( o! ?! _; e
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();* F6 V2 O6 o/ n$ w a# }. Y. u! O
}
7 Q3 ~8 b) R2 N& Z0 q6 @8 O2 A else{
( E0 ^( @' D$ l% _' v' a I = 0;
% p0 K8 Q0 @8 S* h% k7 l9 U是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?
0 w% A8 s. G$ m; @初学者,还请见谅! |
|