|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
- @. k& ]- q2 q$ I& R- Tpublic void spreadMoney(int money){
9 t+ j, l% `- A& A$ x0 W // Randomly place money in moneySpace, l/ Z- [. }+ f3 t7 c
for(int i = 0; i < money; i++){
8 ]. w. }5 ~6 \2 w7 j
6 ?# e6 `: ^, X* ]" G# k, M) A // Choose coordinates1 F& `# ?0 m7 k) k# o1 i6 S
int x = (int)(Math.random()*(moneySpace.getSizeX()));
) P; K- Q7 ]+ q% }/ \ int y = (int)(Math.random()*(moneySpace.getSizeY()));% o" s+ d/ g# t, S8 s |& u
/ K2 m) m, Z) Z6 a. Y
// Get the value of the object at those coordinates
1 h* v8 R: @8 L1 T4 { int I;
6 O+ B7 X! ~2 R+ a& M( ] if(moneySpace.getObjectAt(x,y)!= null){" K$ T3 t9 c6 a& n! O
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
; o9 A; Q- D9 X( l3 R3 J% L" |8 }+ ` }
$ t" }- C3 H+ Z6 [ j- h0 k1 a) h7 m else{, ?5 ?9 H! H( F4 F7 g. R
I = 0;
& @$ {7 X4 ~% l }2 A2 c; L3 w+ j5 I: G& R' v* m/ q
// Replace the Integer object with another one with the new value) s! ]) Q/ p2 m, w5 p! {
moneySpace.putObjectAt(x,y,new Integer(I + 1));& X8 [- o7 N/ i) U/ E) P& s
}
, t5 ]6 g6 d3 P% ?7 g这里面if(moneySpace.getObjectAt(x,y)!= null){/ ] q/ V- E/ p& {
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
3 v6 @# w5 L( T; {, a }
, o- Y# _2 ~; i$ P: B else{
" a, k2 _6 H: p( I, r I = 0;
- B7 M" w# D/ Z0 Q, G是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?* B( R! C e. Z/ y+ B
初学者,还请见谅! |
|