File tree Expand file tree Collapse file tree 4 files changed +24
-59
lines changed
03-Probability-Simulation
01-A-Money-Experiment-Basics/src
02-More-about-the-Money-Experiment/src Expand file tree Collapse file tree 4 files changed +24
-59
lines changed Original file line number Diff line number Diff line change @@ -6,13 +6,12 @@ public class AlgoVisualizer {
6
6
private int [] money ;
7
7
private AlgoFrame frame ;
8
8
9
- public AlgoVisualizer (int sceneWidth , int sceneHeight , int N ){
9
+ public AlgoVisualizer (int sceneWidth , int sceneHeight ){
10
10
11
11
// 初始化数据
12
- money = new int [N ];
13
- for (int i = 0 ; i < N ; i ++) {
14
- money [i ] = sceneHeight /3 ;
15
- }
12
+ money = new int [100 ];
13
+ for (int i = 0 ; i < money .length ; i ++)
14
+ money [i ] = 100 ;
16
15
17
16
// 初始化视图
18
17
EventQueue .invokeLater (() -> {
@@ -27,25 +26,24 @@ public void run(){
27
26
28
27
while (true ){
29
28
29
+ frame .render (money );
30
+ AlgoVisHelper .pause (DELAY );
31
+
30
32
for (int i = 0 ; i < money .length ; i ++){
31
33
if (money [i ] > 0 ){
32
34
int j = (int )(Math .random () * money .length );
33
35
money [i ] -= 1 ;
34
36
money [j ] += 1 ;
35
37
}
36
38
}
37
-
38
- frame .render (money );
39
- AlgoVisHelper .pause (DELAY );
40
39
}
41
40
}
42
41
43
42
public static void main (String [] args ) {
44
43
45
- int sceneWidth = 1200 ;
46
- int sceneHeight = 840 ;
47
- int N = 120 ;
44
+ int sceneWidth = 1000 ;
45
+ int sceneHeight = 800 ;
48
46
49
- AlgoVisualizer vis = new AlgoVisualizer (sceneWidth , sceneHeight , N );
47
+ AlgoVisualizer vis = new AlgoVisualizer (sceneWidth , sceneHeight );
50
48
}
51
49
}
Load Diff This file was deleted.
Original file line number Diff line number Diff line change @@ -9,13 +9,12 @@ public class AlgoVisualizer {
9
9
private int [] money ;
10
10
private AlgoFrame frame ;
11
11
12
- public AlgoVisualizer (int sceneWidth , int sceneHeight , int N ){
12
+ public AlgoVisualizer (int sceneWidth , int sceneHeight ){
13
13
14
14
// 初始化数据
15
- money = new int [N ];
16
- for (int i = 0 ; i < N ; i ++) {
17
- money [i ] = sceneHeight /4 ;
18
- }
15
+ money = new int [100 ];
16
+ for (int i = 0 ; i < money .length ; i ++)
17
+ money [i ] = 100 ;
19
18
20
19
// 初始化视图
21
20
EventQueue .invokeLater (() -> {
@@ -30,30 +29,30 @@ public void run(){
30
29
31
30
while (true ){
32
31
33
- // 改进1:每一帧执行的tick数
32
+ // 改进2:是否排序
33
+ Arrays .sort (money );
34
+ frame .render (money );
35
+ AlgoVisHelper .pause (DELAY );
36
+
37
+ // 改进1:每一帧执行的轮数
34
38
for (int k = 0 ; k < 50 ; k ++){
35
39
for (int i = 0 ; i < money .length ; i ++){
40
+ // 改进3:允许money为负值
36
41
//if(money[i] > 0){
37
42
int j = (int )(Math .random () * money .length );
38
43
money [i ] -= 1 ;
39
44
money [j ] += 1 ;
40
45
//}
41
46
}
42
47
}
43
-
44
- // 改进2:是否排序
45
- Arrays .sort (money );
46
- frame .render (money );
47
- AlgoVisHelper .pause (DELAY );
48
48
}
49
49
}
50
50
51
51
public static void main (String [] args ) {
52
52
53
- int sceneWidth = 1200 ;
54
- int sceneHeight = 840 ;
55
- int N = 120 ;
53
+ int sceneWidth = 1000 ;
54
+ int sceneHeight = 800 ;
56
55
57
- AlgoVisualizer vis = new AlgoVisualizer (sceneWidth , sceneHeight , N );
56
+ AlgoVisualizer vis = new AlgoVisualizer (sceneWidth , sceneHeight );
58
57
}
59
58
}
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments