|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中2 H' a1 H3 K8 T
public void spreadMoney(int money){
H1 f' z7 D! j- X: [4 z // Randomly place money in moneySpace
6 c) D+ s4 U# S% e2 e9 k for(int i = 0; i < money; i++){ R- M! z: K+ Q; {
" i" c. ]/ y0 a9 b( l" b
// Choose coordinates
8 P. P$ a) U) t int x = (int)(Math.random()*(moneySpace.getSizeX()));
4 f7 D5 p* @0 m% X( E; u/ M. V& r int y = (int)(Math.random()*(moneySpace.getSizeY()));
9 [! `3 p5 ~; T; W3 M9 ?8 k$ S2 ^7 X3 [$ Y- U: _5 H
// Get the value of the object at those coordinates
( X4 R7 p1 g" ~" N. f' I int I;
7 D+ J2 _/ Q- o ] if(moneySpace.getObjectAt(x,y)!= null){
% b& D6 ]. b% ? I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
0 C8 u7 d1 E% @ ]: y }9 R8 a9 o& D/ \' {
else{
( h+ |- R, L' `" O3 ^& T0 x% L" } I = 0;( ]" r$ d+ i; T) N
}
3 p! Y) y2 f; x7 S. }4 X // Replace the Integer object with another one with the new value
5 G: F- @% c' {% m moneySpace.putObjectAt(x,y,new Integer(I + 1));3 }" |4 q# l4 Z, G% h5 b2 T( N6 X- s5 j
}
k) r) t, `1 o: t3 l: p这里面if(moneySpace.getObjectAt(x,y)!= null){
4 o2 {4 x. y- c6 J' e I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
. g( {1 S; @: C, [ J }
% H+ _- Z: t' { else{
( H; `8 T% d) \ I = 0;
3 i: w1 F g9 \是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?
/ u G1 G% ?" A0 G' H1 a初学者,还请见谅! |
|