|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
. a4 q$ j4 l$ ]) |, Z4 ` L' dpublic void spreadMoney(int money){
8 o$ Z, `+ ^. k8 H' X) Q // Randomly place money in moneySpace& J6 Q0 x7 T$ N+ w4 ^9 v; t8 y
for(int i = 0; i < money; i++){
) B8 v+ r2 z7 Z8 E
2 N+ Q% e' E8 Q' E* H // Choose coordinates" a. L# O& E8 x
int x = (int)(Math.random()*(moneySpace.getSizeX()));
}# W3 z* w, C8 u8 |5 c, `# M int y = (int)(Math.random()*(moneySpace.getSizeY()));2 L# K- f* P3 q# N
0 a6 v- d2 q1 a4 {' {& B0 L5 Y
// Get the value of the object at those coordinates9 u8 d3 k0 m& k9 i9 Z
int I;$ _* U9 j4 ~5 ~ D' ]& I
if(moneySpace.getObjectAt(x,y)!= null){
. g& [& {( ?7 K, P4 J1 o+ G- @ I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
/ G" W6 O" w& f# a$ Y }" c4 K6 n9 @. f/ `! X+ o) j4 }
else{
5 V1 a2 X- Q. t: D7 D I = 0;8 z: T& \ z3 J2 K& P0 U2 F
}" l7 p2 b* o( K$ ~8 n
// Replace the Integer object with another one with the new value
$ l- m" {0 M; E+ f3 R moneySpace.putObjectAt(x,y,new Integer(I + 1));8 l% E7 X0 K+ K. F4 h [& G
}
2 ^* d% K) M& `% w0 J这里面if(moneySpace.getObjectAt(x,y)!= null){! G- x v0 k1 }: z
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();6 v% S4 s0 Y0 n$ ~
}
, t' K/ i, [2 Q0 w. ` else{6 m# y& l$ B/ c8 e+ x
I = 0;
; D' X# S" G8 { F) O; T. s是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?
b$ U* h4 z y; j/ \9 a) Q初学者,还请见谅! |
|