|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中8 K8 d( J! d6 H- o5 M: T" w$ g+ F7 h
public void spreadMoney(int money){
/ H) X, ~- r3 o5 a8 b // Randomly place money in moneySpace3 R6 f& N8 a1 a8 P8 s3 u
for(int i = 0; i < money; i++){
# O1 G9 R5 r- ]& v9 v1 ]+ d
5 U; ]4 j' s3 p! a9 u" d // Choose coordinates
2 [: S$ c. T# P5 n8 c int x = (int)(Math.random()*(moneySpace.getSizeX()));
& f$ g) v/ C0 S4 G int y = (int)(Math.random()*(moneySpace.getSizeY()));
. J1 X3 o# N- y# _: C8 K
9 C7 v( _9 ^8 s* w3 T% W // Get the value of the object at those coordinates; r( w" {' B! T3 z3 N$ k7 U
int I;' J. ?& G5 e5 Q; s6 m
if(moneySpace.getObjectAt(x,y)!= null){
" C" G- u. ]+ f i I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
, F" V3 F+ |; b+ Q5 X4 J }
. f! ?% d( Q3 q3 w+ O# @ else{1 `; q Z+ V. {# b' e; ~% t6 \/ z
I = 0;7 u& B+ t; o' ?; N' a# @
}
" s8 A8 Y) ?- B8 C2 Z; D [* r // Replace the Integer object with another one with the new value
3 \0 W* ~3 Z; B- d/ X% u% m. e8 C moneySpace.putObjectAt(x,y,new Integer(I + 1));
, u4 q+ k. q; K }8 N2 ~* w" @# j: [1 ~$ t6 A; F3 E
这里面if(moneySpace.getObjectAt(x,y)!= null){1 X1 @4 {: u+ U+ _$ G8 [+ e& U; z
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();- e- Y- `. _6 U
}
1 N# }2 B9 g. ^: t/ {+ @ else{
$ F. p* U+ n4 Q% ~3 _ I = 0;
6 g6 c( v8 D7 U/ W( B/ u是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?% T9 b& z x1 ~/ L: y; ~& `* J
初学者,还请见谅! |
|