|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中& z |$ X2 l- i4 l0 m
public void spreadMoney(int money){
& S6 v* u6 C' \8 y, R( ^ // Randomly place money in moneySpace4 i+ {$ E6 Q8 \- g5 p9 p2 g' {; T
for(int i = 0; i < money; i++){$ c* T. m: F: \8 m
: ^, D I' S p( [, o/ {% i) a$ S2 d
// Choose coordinates( D1 F- p: F. g1 F# P) O
int x = (int)(Math.random()*(moneySpace.getSizeX()));3 O0 M" }% c5 {9 _
int y = (int)(Math.random()*(moneySpace.getSizeY()));
: K- S- o3 R) ~1 w8 H1 u3 {( O* K+ h# T
// Get the value of the object at those coordinates% G) b7 I. t2 C! \, M3 }! |* `3 |
int I;
6 c: i4 V+ |$ ?1 T if(moneySpace.getObjectAt(x,y)!= null){$ [- s2 c: a" g" J0 D6 m5 N
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue(); S3 n* }) @3 h _
}
8 V- I- A+ r9 h+ ^4 ? else{
+ s& A! h/ i7 V2 J$ m" f; L* m I = 0;- ?/ p9 N: {% C3 p7 O" q
}
6 I% y8 r* m* N5 |- y // Replace the Integer object with another one with the new value4 _% B9 V+ k, f2 b
moneySpace.putObjectAt(x,y,new Integer(I + 1));. g; v2 f1 G5 p
}
: C* a& t0 W3 p# j. w2 [这里面if(moneySpace.getObjectAt(x,y)!= null){( J& u# U2 @0 r% ?5 t; I8 p$ x
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();$ k- U" G; _7 O0 |
}
6 ]: _! h2 C- b% O: @4 L7 U else{/ P; l) k- D9 Z! y; E
I = 0;
, y8 O# e2 {" r. B O是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?# H8 }* d' V* V# ?# i% g. [
初学者,还请见谅! |
|