|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
' ^' B: n- u% H# F: H! \. G3 j$ epublic void spreadMoney(int money){
$ E2 [6 F8 Z" ~ // Randomly place money in moneySpace
9 A, D6 {, s9 T' P2 e for(int i = 0; i < money; i++){( F5 V4 V/ e$ b4 s, p
5 b' t. m* ~, m( ? O3 _ // Choose coordinates
# ]( B$ `1 v/ K# y int x = (int)(Math.random()*(moneySpace.getSizeX()));* P6 ]8 y/ R+ G' ^" x$ v; O! {
int y = (int)(Math.random()*(moneySpace.getSizeY()));8 p, N9 ~2 I. K$ A2 [2 R. L5 r& E
0 ~. U+ e) |) [: y4 L w // Get the value of the object at those coordinates
1 k( p2 k6 u* ]5 U( Z( o& ` int I;
5 _/ v2 n, m4 z4 h9 b2 _( p if(moneySpace.getObjectAt(x,y)!= null){
9 ^: [9 @7 Z( ]" U I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
- X2 I% a9 S1 y5 L) i }% l5 C4 e& _7 G. y
else{, o1 W6 p# b* S
I = 0;9 N7 y6 h2 O" K
}
# Z. e. U! g9 @4 Z1 j // Replace the Integer object with another one with the new value
# U% `. A! \$ z. I moneySpace.putObjectAt(x,y,new Integer(I + 1));- F4 P" G5 j1 K/ d8 h9 F
}
" z2 |# X0 b7 o @" G这里面if(moneySpace.getObjectAt(x,y)!= null){% m. Q5 t& J+ ?* x
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();( a2 M: p7 Q0 e) b7 O: H
}
/ X/ ?. |' C M( I5 W, Z( ~ else{
/ C/ A. f. I' y9 m) N, o# w3 N I = 0;
: R7 u9 G5 W. r& m/ R1 g. [是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?2 b7 K) r; { I g" k" m$ v
初学者,还请见谅! |
|