|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中9 s0 O% q* D& J7 d( G( J4 m
public void spreadMoney(int money){
+ f1 U- D7 P& E5 g5 u; X // Randomly place money in moneySpace
2 X) C9 ~3 a" u( J* K for(int i = 0; i < money; i++){) ~4 q$ O; s/ z) @
1 l8 x) M" }" S) |/ F& V // Choose coordinates
5 [' Z# h P' t3 h9 Y* d int x = (int)(Math.random()*(moneySpace.getSizeX()));
! e7 C) T' b) ? `5 d! _! G" g/ v int y = (int)(Math.random()*(moneySpace.getSizeY()));
5 n( j4 G$ \4 b2 j8 h
/ e7 g8 @' t& O( k1 s2 U // Get the value of the object at those coordinates
b+ P+ B" @; p int I;
& _# N- D9 m9 R$ {1 i& ^- y if(moneySpace.getObjectAt(x,y)!= null){$ X$ g& }: ?9 e: `9 T1 Z+ @
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
, i. K+ l5 b G6 ? }8 N6 \% ^6 J* G/ C9 H+ [
else{ a x( f! n0 D' c4 b& c; k* S. h, I
I = 0;
: t |: Y$ Q6 u0 R }. n6 C! Q3 m/ S6 s& {1 e8 U
// Replace the Integer object with another one with the new value& u1 ]- O1 d: J. @& [# Q+ L
moneySpace.putObjectAt(x,y,new Integer(I + 1));! U4 X# J5 I+ X+ p8 h6 T7 i- |
}
/ K5 X: A4 r% J& Q/ C2 L( c这里面if(moneySpace.getObjectAt(x,y)!= null){" S: p- {: t. L" l) v& R9 C. e
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue(); K1 b! Y/ r" [0 t% x; V
}* e3 b7 V+ F2 ~8 i
else{
5 M1 [ |: Z, _" Q6 W I = 0;
+ r' m% r: H/ \* @4 u6 o. m是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?: m2 G+ B8 [: z
初学者,还请见谅! |
|