|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中+ b: l' b( ?8 o1 d: z
public void spreadMoney(int money){7 p8 y4 c9 F& Y) F
// Randomly place money in moneySpace2 S8 l" s3 P/ h+ Z6 U. k
for(int i = 0; i < money; i++){
( ?. j- a5 |+ F3 ~' i! G1 Q+ F7 z0 A! B+ x
// Choose coordinates
0 O. H) ?9 K) V2 o* y( p& f4 X int x = (int)(Math.random()*(moneySpace.getSizeX()));9 ?' I* N; T0 h0 B1 W
int y = (int)(Math.random()*(moneySpace.getSizeY()));6 D; D3 O: J6 C1 W1 a8 ?9 ?' J3 } S6 O
3 Q o1 o- T- Q0 l/ D( A
// Get the value of the object at those coordinates
% j* |8 D/ V* o. `& n D/ S int I;
7 r2 |+ T" `! t if(moneySpace.getObjectAt(x,y)!= null){
+ E% `9 F8 O- ] I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
1 }. d# G6 Q( N' l1 A }
+ D, F. r2 ^# V7 ?( S% K6 O else{
$ B$ X6 e7 r$ u# _, j0 z. c5 ] I = 0;
) b! W1 B8 r7 d }8 q1 j' n7 z! N% V/ J. a
// Replace the Integer object with another one with the new value7 y3 i+ D( M" I9 ^/ H6 L7 e
moneySpace.putObjectAt(x,y,new Integer(I + 1));! E5 n/ q/ b0 K
}5 F4 G* b3 {. ]6 E' ]4 G; H, a
这里面if(moneySpace.getObjectAt(x,y)!= null){5 ?8 b. K) @3 ^$ ~8 Y
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();2 a- y9 ?; ^) S: N: D5 g2 k' l- c
}
7 R& U; Z% j9 M2 X else{
' O4 q& b) r0 ?" I" @ [ I = 0;
" G5 {0 J1 |' i; O! H是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?
; J3 R) [; @- |5 w) e初学者,还请见谅! |
|