|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
; g2 ]6 V6 S/ O3 G4 X/ b2 ^ V( cpublic void spreadMoney(int money){9 w, d" v' i0 J7 t$ u/ j; U
// Randomly place money in moneySpace
8 ], U4 s1 B' Q4 i' N. j& S for(int i = 0; i < money; i++){1 ?1 w6 c! t$ o
2 ~6 K6 n! _* M6 } // Choose coordinates$ W- p+ r6 ~$ o) L" S$ F1 X% C: U
int x = (int)(Math.random()*(moneySpace.getSizeX()));
) t, m8 r2 a! }3 B4 b- W int y = (int)(Math.random()*(moneySpace.getSizeY()));
1 b6 ^4 |2 O; A
% {# ^# Q5 f* S V o0 ? // Get the value of the object at those coordinates
0 ^9 M, O* N5 ]4 g int I;
9 E; A$ S* q& ?' }9 N) y if(moneySpace.getObjectAt(x,y)!= null){* f; r! v6 W! e+ `: G' T/ a& w# X
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();2 C, t% E* f3 I+ h. H6 Z
}: Q2 B6 q" A2 u0 g7 v. |
else{
! m4 b/ |& @1 `4 B% y, E6 C I = 0;3 C' [ {( \# s# d9 y( |/ T3 @4 q8 n, _
}
1 M9 S* o9 K1 V! H" E // Replace the Integer object with another one with the new value
, C! z, e& s, V" E moneySpace.putObjectAt(x,y,new Integer(I + 1));3 p/ I8 i3 K5 a' b+ i. ^; u9 V
}
, t" i& q7 x9 b; D- b3 C& v这里面if(moneySpace.getObjectAt(x,y)!= null){
5 z+ a. d8 G! u* T* S7 K9 Q I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
# ^& c4 Y5 U, e9 W }
/ {: A( a3 g G' T else{& |8 Q8 b/ ]" N7 J H/ }- _
I = 0;2 F- C6 K; E# `6 O: g& s
是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?
* u5 i% {* c7 l* L. t% }初学者,还请见谅! |
|