Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 29099ea

Browse files
Chapter 08 restructured.
1 parent 06e6b72 commit 29099ea

File tree

41 files changed

+477
-27
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+477
-27
lines changed

‎08-Move-the-Box-Solver/03-Move-the-Box-Solver-Rendering/src/AlgoFrame.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
import java.awt.Graphics2D;
2-
import java.awt.Graphics;
3-
import java.awt.Dimension;
4-
import java.awt.Color;
5-
import java.awt.RenderingHints;
1+
import java.awt.*;
62
import java.util.*;
73
import javax.swing.*;
84

‎08-Move-the-Box-Solver/03-Move-the-Box-Solver-Rendering/src/AlgoVisualizer.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import java.awt.*;
2-
import java.awt.event.*;
32

43
public class AlgoVisualizer {
54

‎08-Move-the-Box-Solver/04-Backtrack-to-Solve-Move-the-Box/src/AlgoVisualizer.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ public void run(){
3232
System.out.println("The game has a solution!");
3333
else
3434
System.out.println("The game does NOT have a solution.");
35-
36-
setData();
3735
}
3836

3937
private void setData(){

‎08-Move-the-Box-Solver/04-Backtrack-to-Solve-Move-the-Box/src/GameData.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,19 @@ public boolean inArea(int x, int y){
6464

6565
public boolean solve(){
6666

67-
if(maxTurn <= 0)
67+
if(maxTurn < 0)
6868
return false;
6969

7070
return solve(new Board(starterBoard), maxTurn);
7171
}
7272

73-
private static int d[][] = {{-1, 0}, {0, 1}, {0,-1}};
73+
private static int d[][] = {{1, 0}, {0, 1}, {0,-1}};
74+
// 通过盘面board,使用turn次move,解决move the box的问题
75+
// 若可以成功解决,则返回true,否则返回false
7476
private boolean solve(Board board, int turn){
7577

76-
if(board == null)
77-
throw new IllegalArgumentException("board can not be null in solve function!");
78+
if(board == null || turn < 0)
79+
throw new IllegalArgumentException("Illegal arguments in solve function!");
7880

7981
if(turn == 0)
8082
return board.isWin();
File renamed without changes.
File renamed without changes.
File renamed without changes.

‎08-Move-the-Box-Solver/06-Show-the-Results/src/AlgoVisualizer.java renamed to ‎08-Move-the-Box-Solver/05-Drop/src/AlgoVisualizer.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ private void run(){
3232
System.out.println("The game has a solution!");
3333
else
3434
System.out.println("The game does NOT have a solution.");
35-
36-
setData();
3735
}
3836

3937
private void setData(){
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
public class Board {
2+
3+
public static char EMPTY = '.';
4+
5+
private int N, M;
6+
private char[][] data;
7+
8+
public Board(String[] lines){
9+
if(lines == null)
10+
throw new IllegalArgumentException("lines cannot be null in Board constructor.");
11+
12+
N = lines.length;
13+
if(N == 0)
14+
throw new IllegalArgumentException("lines cannot be empty in Board constructor.");
15+
16+
M = lines[0].length();
17+
18+
data = new char[N][M];
19+
for(int i = 0 ; i < N ; i ++){
20+
if(lines[i].length() != M)
21+
throw new IllegalArgumentException("All lines' length must be same in Board constructor.");
22+
for(int j = 0 ; j < M ; j ++)
23+
data[i][j] = lines[i].charAt(j);
24+
}
25+
}
26+
27+
public Board(Board board){
28+
if(board == null)
29+
throw new IllegalArgumentException("board can not be null in Board constructor!");
30+
31+
this.N = board.N;
32+
this.M = board.M;
33+
this.data = new char[N][M];
34+
for(int i = 0 ; i < N ; i ++)
35+
for(int j = 0 ; j < M ; j ++)
36+
this.data[i][j] = board.data[i][j];
37+
}
38+
39+
public int N(){ return N; }
40+
public int M(){ return M; }
41+
public char getData(int x, int y){
42+
if(!inArea(x, y))
43+
throw new IllegalArgumentException("x, y are out of index in getData!");
44+
45+
return data[x][y];
46+
}
47+
48+
public boolean inArea(int x, int y){
49+
return x >= 0 && x < N && y >= 0 && y < M;
50+
}
51+
52+
public void print(){
53+
for(int i = 0 ; i < N ; i ++)
54+
System.out.println(String.valueOf(data[i]));
55+
}
56+
57+
public boolean isWin(){
58+
59+
for(int i = 0 ; i < N ; i ++)
60+
for(int j = 0 ; j < M ; j ++)
61+
if(data[i][j] != EMPTY)
62+
return false;
63+
64+
return true;
65+
}
66+
67+
public void swap(int x1, int y1, int x2, int y2){
68+
69+
if(!inArea(x1, y1) || !inArea(x2, y2))
70+
throw new IllegalArgumentException("x, y are out of index in swap!");
71+
72+
char t = data[x1][y1];
73+
data[x1][y1] = data[x2][y2];
74+
data[x2][y2] = t;
75+
76+
return;
77+
}
78+
79+
public void run(){
80+
81+
do{
82+
drop();
83+
}while(match());
84+
85+
return;
86+
}
87+
88+
private void drop(){
89+
90+
for(int j = 0 ; j < M ; j ++){
91+
int cur = N-1;
92+
for(int i = N-1 ; i >= 0 ; i --)
93+
if(data[i][j] != EMPTY){
94+
swap(cur, j, i, j);
95+
cur--;
96+
}
97+
}
98+
99+
return;
100+
}
101+
102+
private boolean match(){
103+
104+
return false;
105+
}
106+
107+
}

‎08-Move-the-Box-Solver/05-Drop-and-Match/src/GameData.java renamed to ‎08-Move-the-Box-Solver/05-Drop/src/GameData.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,16 @@ public boolean inArea(int x, int y){
6464

6565
public boolean solve(){
6666

67-
if(maxTurn <= 0)
67+
if(maxTurn < 0)
6868
return false;
6969

70-
return solve(newBoard(starterBoard), maxTurn);
70+
return solve(starterBoard, maxTurn);
7171
}
7272

73-
private static int d[][] = {{-1, 0}, {0, 1}, {0,-1}};
73+
private static int d[][] = {{1, 0}, {0, 1}, {0,-1}};
7474
private boolean solve(Board board, int turn){
7575

76-
if(board == null)
76+
if(board == null || turn < 0)
7777
throw new IllegalArgumentException("board can not be null in solve function!");
7878

7979
if(turn == 0)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /