programming solve a maze java

Get quality term paper help at Unemployedprofessor.net. Use our paper writing services to score better and meet your deadlines. It is simple and straightforward. Whatever paper you need—we will help you write it!


Order a Similar Paper Order a Different Paper

I’m working on an assignment using java language, it is about finding an algorithm to solve a maze. using stack + single linked list. I have little experience with programming however the deadline is near and still I can’t get it right.

here are the java classes I have written so far for this assignment.

linked List:

public class LinkedList<T extends Comparable<T>> {
   
  private class Node<T extends Comparable<T>> {  // Inner class to keep Nodes private
  T data;
  Node<T> next;
   
 
  public  Node(T s) {
  data = s;
  next = null;

  }
  }
 
  private Node<T> head;
  private int count;
 
 
  public int length() {  // return number of items in list
  return count;
  }
 
  public boolean isEmptyList() {
  return count == 0;
  }
 
  public void initializeList() {
  head = null;
  count = 0;
  }
 
  public void print() {
  Node<T> temp = head;
  for(int i = 0; i < count; i++) {
  System.out.print(temp.data + " ");
  if(i < count - 1)
  temp = temp.next;
  }
  System.out.println();
  }
 
  public int search(T s) {
  Node<T> temp = head;
  for(int i = 0; i < count; i++) {
  if(temp.data.compareTo(s) == 0) {
  return i;
  }
  if(i < count - 1)
  temp = temp.next;
  }
  return -1;
  }
 
  public Object clone() {
  LinkedList<T> newList = new LinkedList<T>();
  Node<T> n = head;
  for(int i = 0; i < count; i++) {
  newList.insert(n.data);
  if(n.next != null)
  n = n.next;
  }
  return newList;
  }
 
  public void insert(T s) {  
  //Node n = new Node(s);
  if(count == 0) {
  head = new Node<T>(s);
  count++;
  return;
  }
  Node<T> n = new Node<T>(s);
  Node<T> temp = head;

  while (temp.next != null ) {
  temp = temp.next;
  }
   

  temp.next = n;

   
  count++;
  }
 
 
  public T deleteFront() {
  if (count > 0) {
  T s = (T)head.data;
  head = head.next;

  count--;
   
  if(count == 0)
  head = null;
  return s;
  }
  return null;
  }
   
  public int delete(T s) {
  int index = search(s);
  if(index < 0)  // if object not found in list return -1
  return -1;
  else {
  if(index == 0)  // if target at head or tail, use other delete methods
  deleteFront();
  else {  // walk to delete point and delete node
  Node<T> n = head;
  for(int i = 0; i < index - 1; i++){
  n = n.next;
  }
  n.next = n.next.next;
  count--;
  }
  return index + 1;
  }
  }
 

}

Position:

public class Position implements Comparable<Position> {
  private int x;
  private int y;

 
  public Position() {
  this.x = 0;
  this.y = 0;
  }
 
  public Position(int xIn, int yIn) {
  x = xIn;
  y = yIn;
  }

 
 
  public Position(Position p) {
  x = p.x;
  y = p.y;
  }
 

  public void up() {
  x--;
  }
  public void down() {
  x++;
  }
 
  public void right() {
  y++;
  }
  public void left() {
  y--;
  }
  public int getX() {
  return x;
  }
 
  public int getY() {
  return y;
  }
  //@Override
  public int compareTo(Position p) {

  return 0;
  }
 
  //@Override
  public String toString() {
  return "[" + x + ", " + y + "]n";
  }
  public static void main(String[] args){
  Position p = new Position(5,5);
  System.out.print(p.toString());
  p.up();
  System.out.print(p.toString());
  p.down();
  System.out.print(p.toString());
  p.right();
  System.out.print(p.toString());
  }
   
}

Maze:

import java.io.*;
import java.util.*;
public class Maze {  
 

  public static void main(String[] args)throws FileNotFoundException{
  char[][] maze=new char[20][20];
  maze=fillArray("maze.txt"); //function to store maze from text file to an Array
  int x = 0, y = 0;
   
  Scanner input = new Scanner(System.in);
  do{
  System.out.println("Enter x and y values(to quit enter -1)");
  System.out.println("x = ");
  x = input.nextInt();
  if(x == -1){
  break;
  }
  System.out.println("y = ");
  y = input.nextInt();
  if((x>=0)&&(y>=0)){
  startMaze(x,y, maze);
  }
  }while((x> -1)||(y >-1));   
  }
  public static char[][]fillArray(String file)throws FileNotFoundException
  {
  Scanner sc = new Scanner(new File(file));
  char[][]maze = new char[20][20];// creats array to store the maze
  String line = ""; //creat string to hold the lines from the text file,
  //I have noticed .next function takes whole string if there was no whitespaces
   

  for(int r = 0; r < 20; r++)
  {
  line = sc.next();//move to next String/Line
  for(int c = 0; c < 20; c++)
  {
  maze[r][c] = line.charAt(c);//store charactures to the maze array
  }
   
  }//end for
  //printArray(maze);
   
  return maze;
  }//end fillArray
  public static void printArray(char[][] maze)
  {
  System.out.println();
  for(int r=0;r<20;r++)
  {
  for(int c=0;c<20;c++)
  {
  System.out.print(maze[r][c]+" ");
  }
  System.out.println();
  }//end for
  }//end printArray
 
  public static void startMaze(int x, int y, char[][] maze){
  if(maze[x][y] == 'E'){
  System.out.print("It is The Exitn " );
  printArray(maze);
  }else if(maze[x][y] == 1){
  System.out.println("Trapped!! :( n ");
  printArray(maze);
  }else{
  System.out.println("Solving the maze n ");
  solveMaze(x,y, maze);
  }
  }
 
 
  public static void solveMaze(int x, int y, char[][] mArray){
  Position p = new Position(x,y);
  char[][] maze = Arrays.copyOf(mArray, mArray.length);
  LinkedList list = new LinkedList();

  Stack stc = new Stack<LinkedList>();
  System.out.println("stack created");
  list.insert(p);
  list.print();
  boolean exit = true;
  //try{
  while(exit){
  System.out.println("Start "+ p.toString()+ ", with value " +maze[x][y]);
  System.out.println("1");

  if(x>0){  //moving up through the maze
  System.out.println("1.2 ");
  if(maze[x-1][y] == '0'){
  p.up();
  maze[x][y] = 'S';
  System.out.println("UP, " + maze[x][y]);
  x = p.getX();
  list.insert(p);
   
  }
   
  }
  System.out.println("2");
  if(y>0){
  System.out.println("2 ");
  if(maze[x][y-1] == '0'){
  p.left();
  maze[x][y] = 'S';
  System.out.println("LEFT, " + maze[x][y]);
  y = p.getY();
  list.insert(p);
  }else if(maze[x][y-1] == 'E'){
  p.left();
  exit = false;
  maze[x][y] = 'S';
  System.out.println("LEFT, " + maze[x][y]);
  y = p.getY();
  list.insert(p);
  }
  }
  if((x<20)&&(y<20)){
  if(maze[x+1][y] == '0'){
  p.down();
  maze[x][y] = 'S';
  System.out.println("DOWN, " + maze[x][y]);
  x= p.getX();
  list.insert(p);
  }else if(maze[x][y+1] == '0'){
  p.right();
  maze[x][y] = 'S';
  System.out.println("RIGHT, " + maze[x][y]);
  y = p.getY();
  list.insert(p);
  }
  }
  }//end Loop
  printArray(maze);
  list.print();
  /*}
  catch(ArrayIndexOutOfBoundsException a){
  System.out.println(p.getX() + " , " + p.getY());
  printArray(maze);
  }*/
  }
   
  } 

Writerbay.net

Our affordable academic writing services save you time, which is your most valuable asset. Share your time with your loved ones as our Unemployedprofessor.net experts deliver unique, and custom-written paper for you.

Get a 15% discount on your order using the following coupon code SAVE15


Order a Similar Paper Order a Different Paper