-
Notifications
You must be signed in to change notification settings - Fork 173
/
GameData.java
123 lines (92 loc) · 3.56 KB
/
GameData.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.HashSet;
public class GameData {
private int maxTurn;
private int N, M;
private Board starterBoard;
private Board showBoard;
private HashSet<Board> searchedBoards;
public int clickx = -1, clicky = -1;
public GameData(String filename){
if(filename == null)
throw new IllegalArgumentException("Filename cannot be null!");
Scanner scanner = null;
try{
File file = new File(filename);
if(!file.exists())
throw new IllegalArgumentException("File " + filename + " doesn't exist!");
FileInputStream fis = new FileInputStream(file);
scanner = new Scanner(new BufferedInputStream(fis), "UTF-8");
String turnline = scanner.nextLine();
this.maxTurn = Integer.parseInt(turnline);
ArrayList<String> lines = new ArrayList<String>();
while(scanner.hasNextLine()){
String line = scanner.nextLine();
lines.add(line);
}
starterBoard = new Board(lines.toArray(new String[lines.size()]));
this.N = starterBoard.N();
this.M = starterBoard.M();
//starterBoard.print();
showBoard = new Board(starterBoard);
searchedBoards = new HashSet<Board>();
}
catch(IOException e){
e.printStackTrace();
}
finally {
if(scanner != null)
scanner.close();
}
}
public int N(){ return N; }
public int M(){ return M; }
public Board getShowBoard(){ return showBoard;}
public boolean inArea(int x, int y){
return x >= 0 && x < N && y >= 0 && y < M;
}
public boolean solve(){
if(maxTurn < 0)
return false;
long startTime = System.currentTimeMillis();
searchedBoards.add(starterBoard);
boolean ret = solve(starterBoard, maxTurn);
long endTime = System.currentTimeMillis();
System.out.println( "Time : " + (endTime-startTime) + "ms" );
return ret;
}
private static int d[][] = {{1, 0}, {0, -1}, {0, 1}};
private boolean solve(Board board, int turn){
if(board == null || turn < 0)
throw new IllegalArgumentException("board can not be null in solve function!");
if(turn == 0)
return board.isWin();
if(board.isWin())
return true;
for(int x = 0 ; x < N ; x ++)
for(int y = 0 ; y < M ; y ++)
if(board.getData(x, y) != Board.EMPTY){
for(int i = 0 ; i < 3 ; i ++){
int newX = x + d[i][0];
int newY = y + d[i][1];
if(inArea(newX, newY)){
String swapString = String.format("swap (%d, %d) and (%d, %d)", x, y, newX, newY);
Board nextBoard = new Board(board, board, swapString);
nextBoard.swap(x, y, newX, newY);
nextBoard.run();
if(!searchedBoards.contains(nextBoard)){
searchedBoards.add(nextBoard);
if(solve(nextBoard, turn-1))
return true;
}
}
}
}
return false;
}
}