Skip to content

Commit 48564fb

Browse files
committed
Chapter 03 section 01-02 codes updated.
1 parent 92efd7a commit 48564fb

File tree

4 files changed

+24
-59
lines changed

4 files changed

+24
-59
lines changed

03-Probability-Simulation/01-A-Money-Experiment-Basics/src/AlgoVisualizer.java

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@ public class AlgoVisualizer {
66
private int[] money;
77
private AlgoFrame frame;
88

9-
public AlgoVisualizer(int sceneWidth, int sceneHeight, int N){
9+
public AlgoVisualizer(int sceneWidth, int sceneHeight){
1010

1111
// 初始化数据
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;
1615

1716
// 初始化视图
1817
EventQueue.invokeLater(() -> {
@@ -27,25 +26,24 @@ public void run(){
2726

2827
while(true){
2928

29+
frame.render(money);
30+
AlgoVisHelper.pause(DELAY);
31+
3032
for(int i = 0 ; i < money.length; i ++){
3133
if(money[i] > 0){
3234
int j = (int)(Math.random() * money.length);
3335
money[i] -= 1;
3436
money[j] += 1;
3537
}
3638
}
37-
38-
frame.render(money);
39-
AlgoVisHelper.pause(DELAY);
4039
}
4140
}
4241

4342
public static void main(String[] args) {
4443

45-
int sceneWidth = 1200;
46-
int sceneHeight = 840;
47-
int N = 120;
44+
int sceneWidth = 1000;
45+
int sceneHeight = 800;
4846

49-
AlgoVisualizer vis = new AlgoVisualizer(sceneWidth, sceneHeight, N);
47+
AlgoVisualizer vis = new AlgoVisualizer(sceneWidth, sceneHeight);
5048
}
5149
}

03-Probability-Simulation/01-A-Money-Experiment-Basics/src/SocietyData.java

Lines changed: 0 additions & 16 deletions
This file was deleted.

03-Probability-Simulation/02-More-about-the-Money-Experiment/src/AlgoVisualizer.java

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,12 @@ public class AlgoVisualizer {
99
private int[] money;
1010
private AlgoFrame frame;
1111

12-
public AlgoVisualizer(int sceneWidth, int sceneHeight, int N){
12+
public AlgoVisualizer(int sceneWidth, int sceneHeight){
1313

1414
// 初始化数据
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;
1918

2019
// 初始化视图
2120
EventQueue.invokeLater(() -> {
@@ -30,30 +29,30 @@ public void run(){
3029

3130
while(true){
3231

33-
// 改进1:每一帧执行的tick数
32+
// 改进2:是否排序
33+
Arrays.sort(money);
34+
frame.render(money);
35+
AlgoVisHelper.pause(DELAY);
36+
37+
// 改进1:每一帧执行的轮数
3438
for(int k = 0 ; k < 50 ; k ++){
3539
for(int i = 0 ; i < money.length; i ++){
40+
// 改进3:允许money为负值
3641
//if(money[i] > 0){
3742
int j = (int)(Math.random() * money.length);
3843
money[i] -= 1;
3944
money[j] += 1;
4045
//}
4146
}
4247
}
43-
44-
// 改进2:是否排序
45-
Arrays.sort(money);
46-
frame.render(money);
47-
AlgoVisHelper.pause(DELAY);
4848
}
4949
}
5050

5151
public static void main(String[] args) {
5252

53-
int sceneWidth = 1200;
54-
int sceneHeight = 840;
55-
int N = 120;
53+
int sceneWidth = 1000;
54+
int sceneHeight = 800;
5655

57-
AlgoVisualizer vis = new AlgoVisualizer(sceneWidth, sceneHeight, N);
56+
AlgoVisualizer vis = new AlgoVisualizer(sceneWidth, sceneHeight);
5857
}
5958
}

03-Probability-Simulation/02-More-about-the-Money-Experiment/src/SocietyData.java

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)