|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中# s, A2 h% E/ N, o; @
public void spreadMoney(int money){
2 ~" x! t$ R" } // Randomly place money in moneySpace
0 d. o* ^* ]" o- @% Z$ [3 [ for(int i = 0; i < money; i++){4 R5 S5 N! u9 A
9 m# U8 e+ t3 ~" u4 k4 K" c4 Z1 a
// Choose coordinates5 _/ o; l" X, r( s
int x = (int)(Math.random()*(moneySpace.getSizeX()));
6 J+ V3 ~: {, }; U int y = (int)(Math.random()*(moneySpace.getSizeY()));5 L1 o O& Y) }! P! S r8 @
* R* ~! h, b9 A/ x. x
// Get the value of the object at those coordinates
3 |/ E* v4 P( r& b1 s1 `+ c int I;) f$ N5 F- w4 C* [6 N+ s0 [
if(moneySpace.getObjectAt(x,y)!= null){
; [ t. K; M7 y& i) l I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();' j9 M4 U+ l$ A) _; h1 W
}* r- V0 l8 {1 f0 o
else{+ J+ L0 Y ?7 X: \2 ?
I = 0;
" n9 C4 V# T% H* f! X- b' _ }+ o7 ]7 Y; _" t/ g8 V5 p
// Replace the Integer object with another one with the new value$ P" L V2 n/ c) ]
moneySpace.putObjectAt(x,y,new Integer(I + 1));- E9 Z; Z4 Q" p" w& X" r/ l
}
7 x& d/ \7 ~, a. n: [这里面if(moneySpace.getObjectAt(x,y)!= null){! F. p8 \5 i B1 X
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
- E/ s- q2 P# |1 k5 _ }
6 p4 [& ?2 C/ S+ P! A: K( m else{2 a. D+ o' ~3 M! g2 H1 d* u" T
I = 0;
. J% F2 S2 E) X是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?
( y6 A3 Z* @' P# P初学者,还请见谅! |
|