|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
! s$ M! |4 O0 L, i7 K4 C- I zpublic void spreadMoney(int money){
( y4 c) W; ^" E# \9 l // Randomly place money in moneySpace- u1 O" A3 m5 E" s1 E
for(int i = 0; i < money; i++){ \& F b; x; S+ a0 e
% l8 b9 P0 \' j9 y
// Choose coordinates1 c: ? E9 i1 D, Z; E, {
int x = (int)(Math.random()*(moneySpace.getSizeX()));, j9 d$ p0 R8 P4 @. k$ ~0 V
int y = (int)(Math.random()*(moneySpace.getSizeY()));2 ^1 z7 J% o- b2 j, o3 i B ?5 ?1 W* w
$ w; b9 W3 E1 Y' ]1 M* T* O
// Get the value of the object at those coordinates5 S, ?* g# T) r" V5 B( a7 `3 c
int I;
9 l: x! a: _, y$ i# W. u+ o. O if(moneySpace.getObjectAt(x,y)!= null){& z; f, W+ L2 F W6 J. o1 t( E
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
8 c S5 H z. L9 ?- u9 n3 d! w1 r$ A }
7 a0 e: ^) B' A8 o" G else{
5 s# R+ c' p" s! l8 I. d9 F/ U I = 0;( q2 ?9 j; E I& V$ F0 S
}2 B. C2 l8 M, D7 O: i7 Y
// Replace the Integer object with another one with the new value( {9 X; W, H' r7 X. e4 A7 J
moneySpace.putObjectAt(x,y,new Integer(I + 1));
8 e' \, Y% M6 h: m }$ F$ ?. n5 u9 g2 A* S) j
这里面if(moneySpace.getObjectAt(x,y)!= null){2 [# ~' n+ H9 \1 M
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();. A$ v$ x" u0 N' m
}
& R }: r' |+ j, z3 n( r" L5 u* Y* o else{3 c4 B$ C; N: g. _2 i! R/ p
I = 0;
* Q: W8 i5 N; Y9 g3 s/ w0 `! j是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?
, A0 Y: e5 e: w# Q( \- s4 `初学者,还请见谅! |
|