Skip to content

Commit ff2ae2a

Browse files
committed
Chapter 07 section 03 codes updated.
1 parent 409c2f0 commit ff2ae2a

File tree

34 files changed

+568
-2
lines changed

34 files changed

+568
-2
lines changed

07-Mine-Sweeper/03-Mine-Sweeper-Board-Generation/01-Mine-Generation-Algorithm-1/src/MineSweeperData.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ public class MineSweeperData {
44
public static final String flagImageURL = "resources/flag.png";
55
public static final String mineImageURL = "resources/mine.png";
66
public static String numberImageURL(int num){
7-
if(num < 0 || num >= 8)
7+
if(num < 0 || num > 8)
88
throw new IllegalArgumentException("No such a number image!");
99
return "resources/" + num + ".png";
1010
}

07-Mine-Sweeper/03-Mine-Sweeper-Board-Generation/02-Mine-Generation-Algorithm-2/src/MineSweeperData.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ public class MineSweeperData {
44
public static final String flagImageURL = "resources/flag.png";
55
public static final String mineImageURL = "resources/mine.png";
66
public static String numberImageURL(int num){
7-
if(num < 0 || num >= 8)
7+
if(num < 0 || num > 8)
88
throw new IllegalArgumentException("No such a number image!");
99
return "resources/" + num + ".png";
1010
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import java.awt.*;
2+
import javax.swing.*;
3+
4+
public class AlgoFrame extends JFrame{
5+
6+
private int canvasWidth;
7+
private int canvasHeight;
8+
9+
public AlgoFrame(String title, int canvasWidth, int canvasHeight){
10+
11+
super(title);
12+
13+
this.canvasWidth = canvasWidth;
14+
this.canvasHeight = canvasHeight;
15+
16+
AlgoCanvas canvas = new AlgoCanvas();
17+
setContentPane(canvas);
18+
pack();
19+
20+
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
21+
setResizable(false);
22+
23+
setVisible(true);
24+
}
25+
26+
public AlgoFrame(String title){
27+
28+
this(title, 1024, 768);
29+
}
30+
31+
public int getCanvasWidth(){return canvasWidth;}
32+
public int getCanvasHeight(){return canvasHeight;}
33+
34+
// data
35+
private MineSweeperData data;
36+
public void render(MineSweeperData data){
37+
this.data = data;
38+
repaint();
39+
}
40+
41+
private class AlgoCanvas extends JPanel{
42+
43+
public AlgoCanvas(){
44+
// 双缓存
45+
super(true);
46+
}
47+
48+
@Override
49+
public void paintComponent(Graphics g) {
50+
super.paintComponent(g);
51+
52+
Graphics2D g2d = (Graphics2D)g;
53+
54+
// 抗锯齿
55+
RenderingHints hints = new RenderingHints(
56+
RenderingHints.KEY_ANTIALIASING,
57+
RenderingHints.VALUE_ANTIALIAS_ON);
58+
hints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
59+
g2d.addRenderingHints(hints);
60+
61+
// 具体绘制
62+
int w = canvasWidth/data.M();
63+
int h = canvasHeight/data.N();
64+
65+
for(int i = 0 ; i < data.N() ; i ++)
66+
for(int j = 0 ; j < data.M() ; j ++){
67+
68+
if(data.isMine(i, j))
69+
AlgoVisHelper.putImage(g2d, j*w, i*h, MineSweeperData.mineImageURL);
70+
else
71+
AlgoVisHelper.putImage(g2d, j*w, i*h, MineSweeperData.blockImageURL);
72+
73+
}
74+
}
75+
76+
@Override
77+
public Dimension getPreferredSize(){
78+
return new Dimension(canvasWidth, canvasHeight);
79+
}
80+
}
81+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import javax.swing.*;
2+
import java.awt.*;
3+
import java.awt.geom.Ellipse2D;
4+
import java.awt.geom.Rectangle2D;
5+
import java.lang.InterruptedException;
6+
7+
public class AlgoVisHelper {
8+
9+
private AlgoVisHelper(){}
10+
11+
public static final Color Red = new Color(0xF44336);
12+
public static final Color Pink = new Color(0xE91E63);
13+
public static final Color Purple = new Color(0x9C27B0);
14+
public static final Color DeepPurple = new Color(0x673AB7);
15+
public static final Color Indigo = new Color(0x3F51B5);
16+
public static final Color Blue = new Color(0x2196F3);
17+
public static final Color LightBlue = new Color(0x03A9F4);
18+
public static final Color Cyan = new Color(0x00BCD4);
19+
public static final Color Teal = new Color(0x009688);
20+
public static final Color Green = new Color(0x4CAF50);
21+
public static final Color LightGreen = new Color(0x8BC34A);
22+
public static final Color Lime = new Color(0xCDDC39);
23+
public static final Color Yellow = new Color(0xFFEB3B);
24+
public static final Color Amber = new Color(0xFFC107);
25+
public static final Color Orange = new Color(0xFF9800);
26+
public static final Color DeepOrange = new Color(0xFF5722);
27+
public static final Color Brown = new Color(0x795548);
28+
public static final Color Grey = new Color(0x9E9E9E);
29+
public static final Color BlueGrey = new Color(0x607D8B);
30+
public static final Color Black = new Color(0x000000);
31+
public static final Color White = new Color(0xFFFFFF);
32+
33+
34+
public static void strokeCircle(Graphics2D g, int x, int y, int r){
35+
36+
Ellipse2D circle = new Ellipse2D.Double(x-r, y-r, 2*r, 2*r);
37+
g.draw(circle);
38+
}
39+
40+
public static void fillCircle(Graphics2D g, int x, int y, int r){
41+
42+
Ellipse2D circle = new Ellipse2D.Double(x-r, y-r, 2*r, 2*r);
43+
g.fill(circle);
44+
}
45+
46+
public static void strokeRectangle(Graphics2D g, int x, int y, int w, int h){
47+
48+
Rectangle2D rectangle = new Rectangle2D.Double(x, y, w, h);
49+
g.draw(rectangle);
50+
}
51+
52+
public static void fillRectangle(Graphics2D g, int x, int y, int w, int h){
53+
54+
Rectangle2D rectangle = new Rectangle2D.Double(x, y, w, h);
55+
g.fill(rectangle);
56+
}
57+
58+
public static void setColor(Graphics2D g, Color color){
59+
g.setColor(color);
60+
}
61+
62+
public static void setStrokeWidth(Graphics2D g, int w){
63+
int strokeWidth = w;
64+
g.setStroke(new BasicStroke(strokeWidth, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
65+
}
66+
67+
public static void pause(int t) {
68+
try {
69+
Thread.sleep(t);
70+
}
71+
catch (InterruptedException e) {
72+
System.out.println("Error sleeping");
73+
}
74+
}
75+
76+
public static void putImage(Graphics2D g, int x, int y, String imageURL){
77+
78+
ImageIcon icon = new ImageIcon(imageURL);
79+
Image image = icon.getImage();
80+
81+
g.drawImage(image, x, y, null);
82+
}
83+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import java.awt.*;
2+
3+
public class AlgoVisualizer {
4+
5+
private static int DELAY = 5;
6+
private static int blockSide = 32;
7+
8+
private MineSweeperData data;
9+
private AlgoFrame frame;
10+
private static final int d[][] = {{-1,0},{0,1},{1,0},{0,-1}};
11+
12+
public AlgoVisualizer(int N, int M, int mineNumber){
13+
14+
data = new MineSweeperData(N, M, mineNumber);
15+
int sceneWidth = M * blockSide;
16+
int sceneHeight = N * blockSide;
17+
18+
EventQueue.invokeLater(() -> {
19+
frame = new AlgoFrame("Mine Sweeper", sceneWidth,sceneHeight);
20+
21+
new Thread(() -> {
22+
run();
23+
}).start();
24+
});
25+
}
26+
27+
private void run(){
28+
29+
setData();
30+
}
31+
32+
private void setData(){
33+
frame.render(data);
34+
AlgoVisHelper.pause(DELAY);
35+
}
36+
37+
public static void main(String[] args) {
38+
39+
int N = 20;
40+
int M = 20;
41+
int mineNumber = 20;
42+
43+
AlgoVisualizer vis = new AlgoVisualizer(N, M, mineNumber);
44+
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
public class MineSweeperData {
2+
3+
public static final String blockImageURL = "resources/block.png";
4+
public static final String flagImageURL = "resources/flag.png";
5+
public static final String mineImageURL = "resources/mine.png";
6+
public static String numberImageURL(int num){
7+
if(num < 0 || num > 8)
8+
throw new IllegalArgumentException("No such a number image!");
9+
return "resources/" + num + ".png";
10+
}
11+
12+
private int N, M;
13+
private boolean[][] mines;
14+
15+
public MineSweeperData(int N, int M, int mineNumber){
16+
17+
if(N <= 0 || M <= 0)
18+
throw new IllegalArgumentException("Mine sweeper size is invalid!");
19+
20+
if(mineNumber < 0 || mineNumber > N*M)
21+
throw new IllegalArgumentException("Mine number is larger than the size of mine sweeper board!");
22+
23+
this.N = N;
24+
this.M = M;
25+
26+
mines = new boolean[N][M];
27+
for(int i = 0 ; i < N ; i ++)
28+
for(int j = 0 ; j < M ; j ++){
29+
mines[i][j] = false;
30+
}
31+
32+
generateMines(mineNumber);
33+
}
34+
35+
public int N(){ return N; }
36+
public int M(){ return M; }
37+
38+
public boolean isMine(int x, int y){
39+
if(!inArea(x, y))
40+
throw new IllegalArgumentException("Out of index in isMine function!");
41+
return mines[x][y];
42+
}
43+
44+
public boolean inArea(int x, int y){
45+
return x >= 0 && x < N && y >= 0 && y < M;
46+
}
47+
48+
private void generateMines(int mineNumber){
49+
50+
for(int i = 0 ; i < mineNumber ; i ++){
51+
int x = i/M;
52+
int y = i%M;
53+
mines[x][y] = true;
54+
}
55+
56+
for(int i = 0 ; i < N*M ; i ++){
57+
58+
int x1 = i/M;
59+
int y1 = i%M;
60+
61+
int x2 = (int)(Math.random() * N);
62+
int y2 = (int)(Math.random() * M);
63+
64+
swap(x1, y1, x2, y2);
65+
}
66+
}
67+
68+
private void swap(int x1, int y1, int x2, int y2){
69+
boolean t = mines[x1][y1];
70+
mines[x1][y1] = mines[x2][y2];
71+
mines[x2][y2] = t;
72+
}
73+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import java.awt.*;
2+
import javax.swing.*;
3+
4+
public class AlgoFrame extends JFrame{
5+
6+
private int canvasWidth;
7+
private int canvasHeight;
8+
9+
public AlgoFrame(String title, int canvasWidth, int canvasHeight){
10+
11+
super(title);
12+
13+
this.canvasWidth = canvasWidth;
14+
this.canvasHeight = canvasHeight;
15+
16+
AlgoCanvas canvas = new AlgoCanvas();
17+
setContentPane(canvas);
18+
pack();
19+
20+
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
21+
setResizable(false);
22+
23+
setVisible(true);
24+
}
25+
26+
public AlgoFrame(String title){
27+
28+
this(title, 1024, 768);
29+
}
30+
31+
public int getCanvasWidth(){return canvasWidth;}
32+
public int getCanvasHeight(){return canvasHeight;}
33+
34+
// data
35+
private MineSweeperData data;
36+
public void render(MineSweeperData data){
37+
this.data = data;
38+
repaint();
39+
}
40+
41+
private class AlgoCanvas extends JPanel{
42+
43+
public AlgoCanvas(){
44+
// 双缓存
45+
super(true);
46+
}
47+
48+
@Override
49+
public void paintComponent(Graphics g) {
50+
super.paintComponent(g);
51+
52+
Graphics2D g2d = (Graphics2D)g;
53+
54+
// 抗锯齿
55+
RenderingHints hints = new RenderingHints(
56+
RenderingHints.KEY_ANTIALIASING,
57+
RenderingHints.VALUE_ANTIALIAS_ON);
58+
hints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
59+
g2d.addRenderingHints(hints);
60+
61+
// 具体绘制
62+
int w = canvasWidth/data.M();
63+
int h = canvasHeight/data.N();
64+
65+
for(int i = 0 ; i < data.N() ; i ++)
66+
for(int j = 0 ; j < data.M() ; j ++){
67+
68+
if(data.isMine(i, j))
69+
AlgoVisHelper.putImage(g2d, j*w, i*h, MineSweeperData.mineImageURL);
70+
else
71+
AlgoVisHelper.putImage(g2d, j*w, i*h, MineSweeperData.blockImageURL);
72+
73+
}
74+
}
75+
76+
@Override
77+
public Dimension getPreferredSize(){
78+
return new Dimension(canvasWidth, canvasHeight);
79+
}
80+
}
81+
}

0 commit comments

Comments
 (0)