package test;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import java.awt.Graphics;
import java.util.Random;
public class Testsync extends JFrame implements Runnable {
private int size;
private int squarePixels;
private boolean[][] pointsOccupied;
private int[] currentPosition = new int[2], nextPosition = new int[2];
private Thread thread;
private Random random;
public Testsync(int gridSize, int gridSquarePixels) {
super("Self Avoiding Random Walk");
size = gridSize;
squarePixels = gridSquarePixels;
setSize(size*squarePixels, size*squarePixels);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pointsOccupied = new boolean[size][];
for (int i = 0; i < size; i++)
pointsOccupied[i] = new boolean[size];
thread = new Thread(this);
random = new Random();
reset();
}
public void reset() {
currentPosition[0] = nextPosition[0] = currentPosition[1] = nextPosition[1] = size/2;
for (int i = 0; i < size; i++)
java.util.Arrays.fill(pointsOccupied[i], false);
pointsOccupied[size/2][size/2] = true;
}
public void draw() {
thread.start();
}
public void run() {
while(true) {
if (!randomChooseNext())
break;
repaint();
try {
Thread.sleep(1*100);
} catch(Exception e) { }
}
if (JOptionPane.showConfirmDialog(this, "Path ends. Still want to see another one?") == JOptionPane.YES_OPTION) {
getGraphics().clearRect(0, 0, size*squarePixels, size*squarePixels);
reset();
run();
}
System.exit(0);
}
public void paint(Graphics g) {
g.drawLine(currentPosition[0]*squarePixels, currentPosition[1]*squarePixels, nextPosition[0]*squarePixels, nextPosition[1]*squarePixels);
currentPosition[0] = nextPosition[0];
currentPosition[1] = nextPosition[1];
}
private boolean randomChooseNext() {
int dirsAvailable = 4;
int newX, newY;
int dir;
while (dirsAvailable>0) {
newX = currentPosition[0];
newY = currentPosition[1];
dir = random.nextInt(4);
switch(dir) {
case 0: // left
newX--;break;
case 1: // top
newY--; break;
case 2: // right
newX++; break;
case 3: // bottom
newY++; break;
}
if (newX <0 || newY <0 || newX>size-1 || newY>size-1 || pointsOccupied[newX][newY])
dirsAvailable--;
else {
nextPosition[0] = newX;
nextPosition[1] = newY;
pointsOccupied[newX][newY] = true;
return true;
}
}
return false;
}
public static void main(String[] args) {
String input;
int size, squarePixels;
while (true) {
input = JOptionPane.showInputDialog("Please tell me the size of the grid you want to create (between 10 to 100):");
try {
size = Integer.parseInt(input);
if (size < 10 || size > 100)
throw new Exception();
break;
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Sorry, input unacceptable. Please try again.");
}
}
while (true) {
input = JOptionPane.showInputDialog("Now please tell me the number of pixels of each grid square (between 5 to 10):");
try {
squarePixels = Integer.parseInt(input);
if (squarePixels < 5 || squarePixels > 10)
throw new Exception();
break;
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Sorry, input unacceptable. Please try again.");
}
}
Testsync frame = new Testsync(size, squarePixels);
frame.draw();
}
}