|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
2 ?9 O6 n( N$ w: @8 G' dpublic void spreadMoney(int money){
* i* Q1 n1 J) H! | // Randomly place money in moneySpace; R1 Y- _4 H# |6 ^$ ]/ C
for(int i = 0; i < money; i++){7 c1 R3 i3 I) p5 a
x M) x3 k- _, H2 E // Choose coordinates) v( C8 c& @, N* Q: A1 F: k3 Q
int x = (int)(Math.random()*(moneySpace.getSizeX()));( s3 s \" d7 {( x! m
int y = (int)(Math.random()*(moneySpace.getSizeY()));( X: [1 z u% j" [9 r- I5 D
4 R/ m3 m8 l# U, T // Get the value of the object at those coordinates
# |+ Q& V) S8 a2 b2 I int I;% s4 c& }' U( k3 }: j& ]
if(moneySpace.getObjectAt(x,y)!= null){
, B& H- G W9 `. E0 h/ P I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
; G4 g! e4 J6 R }
% g9 c6 c# V' `" p1 A2 n% d else{1 F% h `2 ?9 N5 i
I = 0;3 R$ ?2 P8 r0 f' g! H1 x% g1 m6 y8 ~
}' j% M- M; T& s) F0 a- n
// Replace the Integer object with another one with the new value5 ~. Z" _4 d: o! t/ u+ t
moneySpace.putObjectAt(x,y,new Integer(I + 1));9 t4 [1 w4 G0 e9 t1 o) _
}$ j! w* t3 t( z% K, m2 c) F# w
这里面if(moneySpace.getObjectAt(x,y)!= null){
) C! i$ k- s7 D& n: f I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();6 e- D! t! b3 K: }% b
}# t5 \' [5 J$ B* _
else{, r' d) |" u! r, J+ X" D' C
I = 0;
5 I; T7 [. S' s: r* O是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?
5 a- M: L/ k% w初学者,还请见谅! |
|