|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中4 N1 q. j9 p( U& i. b. F( V! G
public void spreadMoney(int money){
3 s+ ^6 e- Z7 D: c // Randomly place money in moneySpace( a( U \0 m1 f2 Z
for(int i = 0; i < money; i++){
. V+ G# F S! T: i4 x* Y5 F
! O0 `7 H# t, l# d // Choose coordinates) ~0 l8 ]% c& u T/ h/ _
int x = (int)(Math.random()*(moneySpace.getSizeX()));
' r" z0 I, ]8 {' M5 q# d; R7 z int y = (int)(Math.random()*(moneySpace.getSizeY()));1 o% l' a( s+ U+ y% a( _
) \4 J' p% V) }1 u% x+ G/ M // Get the value of the object at those coordinates3 `, V+ V `; B# h. c5 r" N
int I;* I: w% s/ Q; n
if(moneySpace.getObjectAt(x,y)!= null){; G; T0 h, {* \/ C' o' s; y
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();' J1 b, Z' v! e. o
}& E, Z% |7 Q# M1 ] V/ Z
else{' d( c. A" p& |: K) R! T
I = 0;( J8 ~& R/ M6 g1 Y I9 b
}3 c( H: u7 H7 n- Q' J1 L" p0 T
// Replace the Integer object with another one with the new value4 |/ X9 { Q) s! p
moneySpace.putObjectAt(x,y,new Integer(I + 1));
: j# y0 M* S: w- e7 K9 q4 }5 t6 J }
- X, g1 y+ ]* E% U这里面if(moneySpace.getObjectAt(x,y)!= null){
* ~. d! o& j! S I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();. t; u5 A9 |$ o7 E+ a, m6 \
}
, p2 s" k# C! e else{
: c! A0 q) d; T" u1 D# M/ r; x, U I = 0;) k y, Q0 @! {6 ^5 A8 d% m
是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?
6 ~0 {2 i3 I, f L初学者,还请见谅! |
|