|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中, s/ j+ Q, v- v, }2 Z
public void spreadMoney(int money){
5 _+ k# N- ]$ T4 h$ U: n // Randomly place money in moneySpace
2 @: j9 h9 F. H* ?# t. B for(int i = 0; i < money; i++){
) K0 o0 M7 }- |" d
. i* w* S; I1 N0 d | // Choose coordinates
, c$ C" U8 w6 j, C+ c! D int x = (int)(Math.random()*(moneySpace.getSizeX()));& f2 Y3 N) \+ D7 j7 E' y
int y = (int)(Math.random()*(moneySpace.getSizeY()));, Y. W6 n4 z' ]2 e7 i
) q- s$ [! |* S4 `4 e$ e& S. e" x( c1 b
// Get the value of the object at those coordinates
/ X; h8 c- g/ _ l int I;
' X; X* Z( M# B K( G6 s& ? if(moneySpace.getObjectAt(x,y)!= null){
0 _' K% l B& c- g( \ I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
$ p. @( @! l2 y7 p }
" i7 A# Z& N3 B2 z0 Q3 j else{# U3 V" B" \: ]2 ?# g
I = 0;3 W) J, q( u5 C. j( X3 u* S6 y
}
. W( r4 z, ?! r1 H3 T7 J- O // Replace the Integer object with another one with the new value4 B; a! q" q6 \ \
moneySpace.putObjectAt(x,y,new Integer(I + 1));
9 z* d: H0 c: K }
O, k! \2 S. S6 ]9 _7 Z+ b$ u这里面if(moneySpace.getObjectAt(x,y)!= null){
6 P; K8 A/ s) }# k9 V I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
6 P, R3 ?7 R" ^5 s }3 @* B' B3 ]# M' y& [: @
else{" f3 T6 ]# v, o' ?+ y5 v0 U
I = 0;
' `6 a" p& ^6 L# D是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?
) k! W/ n8 X% @7 W4 \( E1 q4 R) a初学者,还请见谅! |
|