-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent.java
More file actions
97 lines (83 loc) · 3.45 KB
/
Copy pathagent.java
File metadata and controls
97 lines (83 loc) · 3.45 KB
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
/* Agent for Puzzle 8:
A breadth-first-search (BFS) algorithm to solve the Puzzle 8 problem.
Note: BFS is used only to analyse the time and space complexity, optimally a heuristic based algorithm
should be used to solve the problem efficiently
Variables:
game: object which stores the current game state, computes the moves and checks if goal is reached
startTime, stopTime: used to calcute total time taken by the BFS function
Functions:
main: creates new game object, calls BFS function and prints time and space used
breadthFirstSearch: prints the winning moves given the game object
copyState: returns a deep copy of current game state
*/
import java.util.LinkedList;
import java.util.Queue;
public class agent {
static tileGame game;
private static long startTime, stopTime;
private static final long MEGABYTE = 1024L * 1024L;
public static long bytesToMegabytes(long bytes) {
return bytes / MEGABYTE;
}
public static void main(String args[]) {
Runtime runtime = Runtime.getRuntime();
runtime.gc(); // Run the garbage collector
int depth = 10;
game = new tileGame();
game.newDepthGame(depth);
System.out.println("Game instance depth : " + depth);
game.printCurrentState("Initial State :");
// char[][] startState = copyState();
breadthFirstSearch();
game.printCurrentState("Final State :");
// Calculate the used time and memory
long memory = runtime.totalMemory() - runtime.freeMemory();
System.out.println("Used memory: " + memory + " bytes | " + bytesToMegabytes(memory) + " megabytes.");
System.out.println("Used time: " + (stopTime - startTime) + " milliseconds.");
}
public static void breadthFirstSearch() {
// Marking start time of BFS function
startTime = System.currentTimeMillis();
Queue<String> queue = new LinkedList<>();
String initialMoves = game.getActions(), currMove, goalPath = "", nextMoves;
char ch;
for (int i = 0; i < initialMoves.length(); i++)
queue.add(Character.toString(initialMoves.charAt(i)));
while (!queue.isEmpty()) {
currMove = queue.poll();
// System.out.print(currMove + " "); // To print the queue
for (int i = 0; i < currMove.length(); i++)
game.move(currMove.charAt(i));
if (game.checkGoal()) {
goalPath = currMove;
break;
}
nextMoves = game.getActions();
for (int i = 0; i < nextMoves.length(); i++)
queue.add(currMove + Character.toString(nextMoves.charAt(i)));
for (int i = currMove.length() - 1; i >= 0; i--) {
ch = currMove.charAt(i);
if (ch == 'U')
game.move('D');
else if (ch == 'D')
game.move('U');
else if (ch == 'L')
game.move('R');
else if (ch == 'R')
game.move('L');
}
}
// Marking end time of BFS function
stopTime = System.currentTimeMillis();
System.out.println("Moves: " + goalPath);
}
static char[][] copyState() {
char[][] arr = new char[3][3];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
arr[i][j] = game.state[i][j];
}
}
return arr;
}
}