Skip to content

Commit 92efd7a

Browse files
committed
Chapter 03 section 01-02 codes bug fixed.
1 parent bc7b70c commit 92efd7a

File tree

5 files changed

+20
-45
lines changed

5 files changed

+20
-45
lines changed

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ public void run(){
2727

2828
while(true){
2929

30-
int i = (int)(Math.random() * money.length);
31-
int j = (int)(Math.random() * money.length);
32-
33-
if(money[i] > 0){
34-
money[i] -= 1;
35-
money[j] += 1;
30+
for(int i = 0 ; i < money.length; i ++){
31+
if(money[i] > 0){
32+
int j = (int)(Math.random() * money.length);
33+
money[i] -= 1;
34+
money[j] += 1;
35+
}
3636
}
3737

3838
frame.render(money);

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,14 @@ public void run(){
3131
while(true){
3232

3333
// 改进1:每一帧执行的tick数
34-
for(int k = 0 ; k < DELAY*100 ; k ++){
35-
int i = (int)(Math.random() * money.length);
36-
int j = (int)(Math.random() * money.length);
37-
money[i] -= 1;
38-
money[j] += 1;
34+
for(int k = 0 ; k < 50 ; k ++){
35+
for(int i = 0 ; i < money.length; i ++){
36+
//if(money[i] > 0){
37+
int j = (int)(Math.random() * money.length);
38+
money[i] -= 1;
39+
money[j] += 1;
40+
//}
41+
}
3942
}
4043

4144
// 改进2:是否排序

03-Probability-Simulation/03-Get-PI-with-Monte-Carlo/src/AlgoFrame.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ public class AlgoFrame extends JFrame{
1414

1515
private int canvasWidth;
1616
private int canvasHeight;
17-
private JPanel canvas;
1817

1918
public AlgoFrame(String title, int canvasWidth, int canvasHeight){
2019

03-Probability-Simulation/03-Get-PI-with-Monte-Carlo/src/AlgoVisHelper.java

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -34,35 +34,35 @@ private AlgoVisHelper(){}
3434
public static final Color White = new Color(0xFFFFFF);
3535

3636

37-
static public void strokeCircle(Graphics2D g, int x, int y, int r){
37+
public static void strokeCircle(Graphics2D g, int x, int y, int r){
3838

3939
Ellipse2D circle = new Ellipse2D.Double(x-r, y-r, 2*r, 2*r);
4040
g.draw(circle);
4141
}
4242

43-
static public void fillCircle(Graphics2D g, int x, int y, int r){
43+
public static void fillCircle(Graphics2D g, int x, int y, int r){
4444

4545
Ellipse2D circle = new Ellipse2D.Double(x-r, y-r, 2*r, 2*r);
4646
g.fill(circle);
4747
}
4848

49-
static public void strokeRectangle(Graphics2D g, int x, int y, int w, int h){
49+
public static void strokeRectangle(Graphics2D g, int x, int y, int w, int h){
5050

5151
Rectangle2D rectangle = new Rectangle2D.Double(x, y, w, h);
5252
g.draw(rectangle);
5353
}
5454

55-
static public void fillRectangle(Graphics2D g, int x, int y, int w, int h){
55+
public static void fillRectangle(Graphics2D g, int x, int y, int w, int h){
5656

5757
Rectangle2D rectangle = new Rectangle2D.Double(x, y, w, h);
5858
g.fill(rectangle);
5959
}
6060

61-
static public void setColor(Graphics2D g, Color color){
61+
public static void setColor(Graphics2D g, Color color){
6262
g.setColor(color);
6363
}
6464

65-
static public void setStrokeWidth(Graphics2D g, int w){
65+
public static void setStrokeWidth(Graphics2D g, int w){
6666
int strokeWidth = w;
6767
g.setStroke(new BasicStroke(strokeWidth, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
6868
}
@@ -95,31 +95,4 @@ public static void drawText(Graphics2D g, String text, int centerx, int centery)
9595
g.drawString(text, centerx - w/2, centery + h);
9696
}
9797

98-
public static void strokeTriangle(Graphics2D g, int x1, int y1, int x2, int y2, int x3, int y3){
99-
100-
GeneralPath path = new GeneralPath();
101-
path.moveTo(x1, y1);
102-
path.lineTo(x2, y2);
103-
path.lineTo(x3, y3);
104-
path.closePath();
105-
106-
g.draw(path);
107-
}
108-
109-
public static void fillTriangle(Graphics2D g, int x1, int y1, int x2, int y2, int x3, int y3){
110-
111-
GeneralPath path = new GeneralPath();
112-
path.moveTo(x1, y1);
113-
path.lineTo(x2, y2);
114-
path.lineTo(x3, y3);
115-
path.closePath();
116-
117-
g.fill(path);
118-
}
119-
120-
public static void drawLine(Graphics2D g, double x1, double y1, double x2, double y2){
121-
122-
Line2D line = new Line2D.Double(x1, y1, x2, y2);
123-
g.draw(line);
124-
}
12598
}
962 KB
Binary file not shown.

0 commit comments

Comments
 (0)