|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
+ J( i1 u* x3 T3 V( Z7 t% B% c: Lpublic void spreadMoney(int money){
6 _& `2 p3 D4 R: F# n // Randomly place money in moneySpace6 C9 ?. y, q) N
for(int i = 0; i < money; i++){9 r6 t9 o+ w& i4 r. \
/ W$ _0 Y) B& E5 J7 Q // Choose coordinates
. F8 V& c, ]9 C! y: @& p int x = (int)(Math.random()*(moneySpace.getSizeX()));
: \5 m/ K9 ~$ b: D! w) ~ int y = (int)(Math.random()*(moneySpace.getSizeY()));
$ c: P8 z: S1 D- A2 U7 s3 c4 X" I0 N" f# I4 @0 }- d7 U
// Get the value of the object at those coordinates
- @; m c+ u5 `8 _4 ~* y- D4 Q5 C int I; R# x$ t7 y& l/ K5 Z
if(moneySpace.getObjectAt(x,y)!= null){
5 x! D( n- b2 Y/ b+ p- N I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
" E% M2 q8 [' Q/ \ }" o, q+ X! r' E" P3 g. J
else{
+ G+ ]' ^$ ~9 |4 r* M9 L0 s I = 0;
1 @- \* q9 T; F/ b }
' w4 g* P) i3 p( y' C6 x* `) n3 I // Replace the Integer object with another one with the new value x3 Z6 H0 P* f1 o) Y
moneySpace.putObjectAt(x,y,new Integer(I + 1));5 B& j, I$ O& @! h1 \5 M
}2 M0 y* s! N$ L$ E$ l
这里面if(moneySpace.getObjectAt(x,y)!= null){% W6 s/ w# O" ]7 x
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();5 a2 B h6 W2 _7 B; L
}
- h% L3 P. G7 N. \- _, d, H5 ` else{
4 Y# N/ @4 K5 y+ Z I = 0;
$ _4 L" ? a5 w" s% U' R是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?
$ }. b0 w) k. \4 r: u0 g初学者,还请见谅! |
|