|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
' J# x9 W/ R" e( i0 K2 Npublic void spreadMoney(int money){+ ~( Q" u* _% a
// Randomly place money in moneySpace
9 E" e( m, |; B+ d3 t% K for(int i = 0; i < money; i++){4 ^7 V. f& v, E8 T; {6 ]
: |. W6 X8 Y8 O/ Q7 C
// Choose coordinates
* j' Q! n( t$ r3 D int x = (int)(Math.random()*(moneySpace.getSizeX()));. P. j6 \0 t2 y2 h- {) ^
int y = (int)(Math.random()*(moneySpace.getSizeY()));
# |$ A+ n [9 x; o: ~( {! f, K4 Q: J
) P1 s; o; {5 G$ x, U9 ^4 ` // Get the value of the object at those coordinates% ], V4 k- V: @! X5 x8 j
int I;4 o3 ~9 T: z# V# O
if(moneySpace.getObjectAt(x,y)!= null){6 i9 Y' }. c A: G( n; W
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
, `, i s/ T1 H/ F! ]9 U) m$ @ }$ u, ~6 b) d' B) A# ?' @1 ^
else{% _" g0 d6 x* |- F" P5 H( y
I = 0;) x4 v$ @, y B4 j0 t4 l
}7 N( p$ ]" w7 ~, z. E! K! U
// Replace the Integer object with another one with the new value
2 \% \( x' b7 N( c0 q7 s! }! z7 {4 X moneySpace.putObjectAt(x,y,new Integer(I + 1));
, o6 F5 o( ?2 [! _+ }3 a5 x( | }
6 l5 ]9 r0 a0 r# a7 W" n F$ d这里面if(moneySpace.getObjectAt(x,y)!= null){
" {; I4 r7 Z4 m" F$ j3 \2 i/ O I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
1 k5 _8 i6 t1 v) w% D }
( G, X* G( B, Z# e else{
* b- ^, e" }5 ^" m0 E' m6 p I = 0;
: T2 O" ?( I5 j是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?
. T$ T6 ]# S( y5 ~% r) V初学者,还请见谅! |
|