|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
! O$ x, B* Q7 \. ypublic void spreadMoney(int money){2 h S1 |; w; G: Y
// Randomly place money in moneySpace- J6 `% v% Z' f+ r k; d
for(int i = 0; i < money; i++){
. f% N1 @/ X8 |. Z9 r; U* r% Q$ j
' ]& [2 P( `5 e' ~( L* a& k // Choose coordinates& E1 a/ {9 F* }0 p$ @
int x = (int)(Math.random()*(moneySpace.getSizeX()));
- o M6 m( b- L# ]7 O, n) Z( } int y = (int)(Math.random()*(moneySpace.getSizeY()));
" _: b$ i. V1 b* J7 |2 X3 `5 Y- @* U" f
( Z* `, O, n( I( |) V" H$ m // Get the value of the object at those coordinates! n: X! A2 x7 t" C6 b
int I;
& d$ k$ t! v8 x: T8 o. q$ j c! @ if(moneySpace.getObjectAt(x,y)!= null){! w/ l# i+ @& u! L
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();; ~9 @! V4 R1 t9 g+ a' g
}. ] O5 s! i) s
else{
% P( X. B1 K; j- b& V I = 0;
7 O. v! M- B8 l }) w4 [! L1 U7 _$ ~' c8 F3 b, ?% }
// Replace the Integer object with another one with the new value' I$ r" k$ Y- F' Q
moneySpace.putObjectAt(x,y,new Integer(I + 1));. ?1 G7 l5 x% f3 X4 f% }% X8 ]+ i
}+ t- T8 d1 B& f+ h3 d( H% O& t
这里面if(moneySpace.getObjectAt(x,y)!= null){
5 M; q5 R- D1 F' ?( K4 h I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
7 b/ V8 W4 ]( s) G4 o }; G( p1 [0 j. s3 O" G+ b
else{
$ _% g6 E- ?" P1 M" X8 t: b$ a I = 0;
3 P+ ]4 L# S; F是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?
; |( u3 O. m% e. G, V0 g初学者,还请见谅! |
|