Skip to content

Commit 5a1d4d7

Browse files
committed
Chapter 08 restructured.
1 parent 29099ea commit 5a1d4d7

File tree

27 files changed

+42
-167
lines changed

27 files changed

+42
-167
lines changed

08-Move-the-Box-Solver/06-Match/src/Board.java

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,20 @@ public void run(){
8585
return;
8686
}
8787

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+
88102
private static int d[][] = {{0, 1}, {1, 0}};
89103
private boolean match(){
90104

@@ -118,17 +132,4 @@ private boolean match(){
118132
return isMatched;
119133
}
120134

121-
private void drop(){
122-
123-
for(int j = 0 ; j < M ; j ++){
124-
int cur = N-1;
125-
for(int i = N-1 ; i >= 0 ; i --)
126-
if(data[i][j] != EMPTY){
127-
swap(cur, j, i, j);
128-
cur--;
129-
}
130-
}
131-
132-
return;
133-
}
134135
}

08-Move-the-Box-Solver/07-Show-the-Results/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/07-Show-the-Results/src/AlgoVisualizer.java

Lines changed: 2 additions & 3 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(){
@@ -43,7 +41,8 @@ private void setData(){
4341

4442
public static void main(String[] args) {
4543

46-
String filename = "level/boston_09.txt";
44+
// String filename = "level/boston_09.txt";
45+
String filename = "level/boston_16.txt";
4746

4847
AlgoVisualizer vis = new AlgoVisualizer(filename);
4948
}

08-Move-the-Box-Solver/07-Show-the-Results/src/Board.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,9 @@ private void drop(){
136136
int cur = N-1;
137137
for(int i = N-1 ; i >= 0 ; i --)
138138
if(data[i][j] != EMPTY){
139-
data[cur][j] = data[i][j];
139+
swap(cur, j, i, j);
140140
cur--;
141141
}
142-
for(; cur >= 0 ; cur --)
143-
data[cur][j] = EMPTY;
144142
}
145143

146144
return;

08-Move-the-Box-Solver/07-Show-the-Results/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(new Board(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)

08-Move-the-Box-Solver/Chapter-08-Completed-Codes/Move-the-Box-Solver/src/AlgoFrame.java

Lines changed: 1 addition & 11 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

@@ -103,12 +99,6 @@ public void paintComponent(Graphics g) {
10399
String text = String.format("( %d , %d )", i, j);
104100
AlgoVisHelper.drawText(g2d, text, j*h + h/2, i*w + w/2);
105101
}
106-
107-
if( i == data.clickx && j == data.clicky) {
108-
AlgoVisHelper.setColor(g2d, AlgoVisHelper.LightBlue);
109-
AlgoVisHelper.setStrokeWidth(g2d, 4);
110-
AlgoVisHelper.strokeRectangle(g2d, j * h + 2, i * w + 2, w - 4, h - 4);
111-
}
112102
}
113103
}
114104

08-Move-the-Box-Solver/Chapter-08-Completed-Codes/Move-the-Box-Solver/src/AlgoVisualizer.java

Lines changed: 4 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import java.awt.*;
22
import java.awt.event.*;
3-
import javax.swing.*;
43

54
public class AlgoVisualizer {
65

@@ -18,7 +17,7 @@ public AlgoVisualizer(String filename){
1817

1918
EventQueue.invokeLater(() -> {
2019
frame = new AlgoFrame("Move the Box Solver", sceneWidth,sceneHeight);
21-
frame.addMouseListener(new AlgoMouseListener());
20+
2221
new Thread(() -> {
2322
run();
2423
}).start();
@@ -27,77 +26,22 @@ public AlgoVisualizer(String filename){
2726

2827
private void run(){
2928

30-
setData(-1, -1);
29+
setData();
3130

3231
if(data.solve())
3332
System.out.println("The game has a solution!");
3433
else
3534
System.out.println("The game does NOT have a solution.");
36-
37-
setData(-1, -1);
3835
}
3936

40-
private void setData(int clickx, int clicky){
41-
data.clickx = clickx;
42-
data.clicky = clicky;
43-
37+
private void setData(){
4438
frame.render(data);
4539
AlgoVisHelper.pause(DELAY);
4640
}
4741

48-
public void addAlgoMouseListener(){
49-
frame.addMouseListener(new AlgoMouseListener());
50-
}
51-
52-
private Position clickPos1 = null;
53-
private Position clickPos2 = null;
54-
private class AlgoMouseListener extends MouseAdapter{
55-
56-
@Override
57-
public void mouseReleased(MouseEvent event){
58-
event.translatePoint(
59-
-(int)(frame.getBounds().width - frame.getCanvasWidth()),
60-
-(int)(frame.getBounds().height - frame.getCanvasHeight())
61-
);
62-
63-
Point pos = event.getPoint();
64-
//System.out.println(pos.x + " , " + pos.y );
65-
66-
int w = frame.getCanvasWidth() / data.M();
67-
int h = frame.getCanvasHeight() / data.N();
68-
69-
int x = pos.y / h;
70-
int y = pos.x / w;
71-
//System.out.println(x + " , " + y);
72-
73-
if(SwingUtilities.isLeftMouseButton(event)){
74-
if(data.inArea(x, y)){
75-
setData(x, y);
76-
if(clickPos1 == null){
77-
clickPos1 = new Position(x, y);
78-
}
79-
else{
80-
clickPos2 = new Position(x, y);
81-
if(clickPos2.nextTo(clickPos1)){
82-
data.getShowBoard().swap(clickPos1.getX(), clickPos1.getY(), clickPos2.getX(), clickPos2.getY());
83-
data.getShowBoard().run();
84-
}
85-
clickPos1 = null;
86-
clickPos2 = null;
87-
setData(-1, -1);
88-
}
89-
}
90-
else{
91-
setData(-1, -1);
92-
clickPos1 = null;
93-
clickPos2 = null;
94-
}
95-
}
96-
}
97-
}
98-
9942
public static void main(String[] args) {
10043

44+
// String filename = "level/boston_09.txt";
10145
String filename = "level/boston_16.txt";
10246

10347
AlgoVisualizer vis = new AlgoVisualizer(filename);

08-Move-the-Box-Solver/Chapter-08-Completed-Codes/Move-the-Box-Solver/src/Board.java

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -151,30 +151,4 @@ public void printSwapInfo(){
151151
System.out.println(swapString);
152152
return;
153153
}
154-
155-
@Override
156-
public int hashCode(){
157-
158-
StringBuilder s = new StringBuilder();
159-
for(int i = 0 ; i < data.length ; i ++)
160-
s.append(data[i]);
161-
return s.toString().hashCode();
162-
}
163-
164-
@Override
165-
public boolean equals(Object another){
166-
167-
if(!(another instanceof Board))
168-
return false;
169-
170-
if( another == this)
171-
return true;
172-
173-
Board anotherBoard = (Board)another;
174-
for(int i = 0 ; i < N ; i ++)
175-
for(int j = 0 ; j < M ; j ++)
176-
if(this.data[i][j] != anotherBoard.data[i][j])
177-
return false;
178-
return true;
179-
}
180154
}

08-Move-the-Box-Solver/Chapter-08-Completed-Codes/Move-the-Box-Solver/src/GameData.java

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,14 @@
44
import java.io.IOException;
55
import java.util.ArrayList;
66
import java.util.Scanner;
7-
import java.util.HashSet;
7+
88

99
public class GameData {
1010

1111
private int maxTurn;
1212
private int N, M;
1313
private Board starterBoard;
1414
private Board showBoard;
15-
private HashSet<Board> searchedBoards;
16-
17-
public int clickx = -1, clicky = -1;
1815

1916
public GameData(String filename){
2017

@@ -44,11 +41,9 @@ public GameData(String filename){
4441
this.N = starterBoard.N();
4542
this.M = starterBoard.M();
4643

47-
//starterBoard.print();
44+
starterBoard.print();
4845

4946
showBoard = new Board(starterBoard);
50-
51-
searchedBoards = new HashSet<Board>();
5247
}
5348
catch(IOException e){
5449
e.printStackTrace();
@@ -72,21 +67,13 @@ public boolean solve(){
7267
if(maxTurn < 0)
7368
return false;
7469

75-
long startTime = System.currentTimeMillis();
76-
77-
searchedBoards.add(starterBoard);
78-
boolean ret = solve(starterBoard, maxTurn);
79-
80-
long endTime = System.currentTimeMillis();
81-
System.out.println( "Time : " + (endTime-startTime) + "ms" );
82-
83-
return ret;
70+
return solve(starterBoard, maxTurn);
8471
}
8572

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

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

9279
if(turn == 0)
@@ -106,18 +93,12 @@ private boolean solve(Board board, int turn){
10693
Board nextBoard = new Board(board, board, swapString);
10794
nextBoard.swap(x, y, newX, newY);
10895
nextBoard.run();
109-
110-
if(!searchedBoards.contains(nextBoard)){
111-
searchedBoards.add(nextBoard);
112-
if(solve(nextBoard, turn-1))
113-
return true;
114-
}
115-
96+
if(solve(nextBoard, turn-1))
97+
return true;
11698
}
11799
}
118100
}
119101

120102
return false;
121103
}
122-
123104
}

08-Move-the-Box-Solver/Chapter-08-Completed-Codes/Move-the-Box-Solver/src/Position.java

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

08-Move-the-Box-Solver/Chapter-08.key

74 KB
Binary file not shown.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2
2+
.........
3+
....A....
4+
...BCB...
5+
...ADA...
6+
..BDECB..
7+
..DFAFC..
8+
.BAAEFEB.

0 commit comments

Comments
 (0)