|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中: } h3 I$ p) @
public void spreadMoney(int money){
4 ^9 F' I1 W' H" K // Randomly place money in moneySpace' H$ c8 q5 E4 T+ i
for(int i = 0; i < money; i++){4 I! A" D( d$ |0 h3 J1 Z
- e4 {0 D% E3 o- A0 J+ s* u
// Choose coordinates
7 T/ M2 a2 r1 }: v: A' _; w int x = (int)(Math.random()*(moneySpace.getSizeX()));; D+ ^6 D5 P9 f0 n7 k: W7 W7 o5 L+ V
int y = (int)(Math.random()*(moneySpace.getSizeY()));
: W9 B1 O4 H( O" b8 p- r+ B/ G E$ |' u5 E/ [' a" \3 X4 C
// Get the value of the object at those coordinates: r6 S9 t4 _3 y) D9 w9 }7 Z
int I;% g" `' B3 x) A% I/ i" i' z& I
if(moneySpace.getObjectAt(x,y)!= null){/ {7 A) l9 w7 T2 E& I* }3 T0 A0 }% i
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
7 U F) @2 ]" q$ `# x) l }
7 W- q8 X& c8 ?! ~0 Z8 U) S- \ else{
/ u9 e1 [% N/ d" v/ z5 Y9 M( N# z I = 0;+ z$ H( e3 b5 u- {$ G1 @, P5 f
}
: s; t6 q7 `8 P0 K! a // Replace the Integer object with another one with the new value
+ I! T" G) Y K4 r! Q4 i& V9 V( @4 X moneySpace.putObjectAt(x,y,new Integer(I + 1));( T; p9 f4 v/ O$ k* k% V
}
\1 n2 v% |3 z) |2 n( x$ S这里面if(moneySpace.getObjectAt(x,y)!= null){1 {- w8 v# k9 X* S7 P
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();4 h9 h! r k! d0 J( l1 Z7 N I3 b
}
) d) ]6 v6 L& G. B1 d else{
- D0 N- S. W& }4 s9 y I = 0;
7 F. g7 O( N( V6 \- j/ ^! [6 w是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?8 }- h! f! }) W, W; v) z: u
初学者,还请见谅! |
|