Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

convert below c code program to java code #include <stdio.h> #include <string.h>

ID: 3759604 • Letter: C

Question

convert below c code program to java code

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define FALSE 0
#define TRUE 1

typedef int boolval;
typedef struct Node{
int x;
int y;
struct Node* next;
} NODE;

typedef struct mazeStruct
{
char *arr;
int rows;
int columns;
int x_start;
int y_start;
int end_x;
int end_y;
int size_of_x;
int size_of_y;
} MAZE;

typedef NODE * NodePointer;
void printMaze(MAZE);
void freeMaze(MAZE);
int debug= FALSE;
NodePointer innitialize_stack(MAZE);
MAZE make_a_Maze(char *);

void push(NodePointer *top, int x, int y)
{
NodePointer temp = (NodePointer) malloc( sizeof(NODE) );
temp->x = x;
temp->y = y;
temp->next = *top;
*top = temp;
}

void pop(NodePointer *top) {
if(*top!=NULL )
{
NodePointer temp = *top;
*top = (*top)->next;
free(temp);
}
}

void top(NodePointer t, int *x, int *y)
{
if(t!=NULL )
{
*x = t->x;
*y = t->y;
}
else
{
printf("Stack is Empty ");
exit(-1);
}
}

void clearStack(NodePointer *top)
{
while( *top!= NULL)
{
pop(top);
}
}

bool isEmpty(NodePointer top)
{
return (top==NULL);
}

void printStack(NodePointer top)
{
int n;
if( top!=NULL)
{
printStack( top->next );
printf("(%d, %d) ", top->x, top->y );
}
}

NodePointer innitialize_stack(MAZE m) {
NodePointer stack = NULL;
bool end = FALSE;
char *temp= (char *) malloc(sizeof(char) * m.rows * m.columns );
memcpy(temp, m.arr, (m.rows*m.columns));
push( &stack, m.x_start, m.y_start );
while( !isEmpty( stack ) && !end ) {
if( debug== TRUE ){
printf("List stack:");
printStack( stack );
printf(" ");
}
int x = -1, y = -1;
top( stack, &x, &y );
if( m.end_x == x && m.end_y == y )
{
end = TRUE;
break;
}
if( m.arr[(x*m.columns + y)+1]=='.'
|| m.arr[(x*m.columns + y)+1]=='E')
{
push( &stack , (((x*m.columns + y)+1)/m.columns),
(((x*m.columns + y)+1) % m.columns));
m.arr[(x*m.columns + y)+1] = 'x';
}
else if(m.arr[(x*m.columns + y)+m.columns]=='.'
||m.arr[(x*m.columns + y)+m.columns]=='E')
{
push( &stack, (((x*m.columns + y)+m.columns)/m.columns),
(((x*m.columns + y)+m.columns) % m.columns));
m.arr[(x*m.columns + y)+m.columns] = 'x';
}
else if(m.arr[(x*m.columns + y) - 1]=='.'
||m.arr[(x*m.columns + y) - 1]=='E')
{
push( &stack, (((x*m.columns + y) - 1) / m.columns),
(((x*m.columns + y) - 1 )% m.columns));
m.arr[ (x*m.columns + y)- 1] = 'x';
}
else if(m.arr[(x*m.columns + y) - m.columns]=='.'
||m.arr[(x*m.columns + y)- m.columns]=='E' )
{
push( &stack,(((x*m.columns + y) - m.columns)/ m.columns),
(((x*m.columns + y) - m.columns)% m.columns));
m.arr[(x*m.columns + y) - m.columns] = 'x';
}
else
{
pop( &stack );
}
}
memcpy(m.arr, temp,(m.rows * m.columns) );
return stack;
}

MAZE make_a_Maze(char *fileName) {
MAZE m1;
int x_position;
int y_position;
int i,j;
FILE *src;
if ( ( src = fopen( fileName, "r" )) == NULL )
{
printf("Cannot open input file: %s ", fileName);
exit(-1);
}
/* read in the size, starting and ending positions in the maze */
fscanf (src, "%d %d", &m1.size_of_x, &m1.size_of_y);
fscanf (src, "%d %d", &m1.x_start,  & m1.y_start);
fscanf (src, "%d %d", &m1.end_x,    & m1.end_y);
m1.rows = m1.size_of_x+2;
m1.columns = m1.size_of_y+2;
/* Allocate memory for the maze array */
int size = m1.rows * m1.columns;
m1.arr = (char *) malloc( sizeof(char) * size );
/* initialize the maze to empty */
for (i = 0; i < m1.size_of_x+2; i++)
for (j = 0; j < m1.size_of_y+2; j++)
m1.arr[i*m1.columns + j] = '.';
/* mark the borders of the maze with *'s */
for (i=0; i < m1.size_of_x+2; i++)
{
m1.arr[i*m1.columns + 0] = '*';
m1.arr[i*m1.columns + m1.size_of_y+1] = '*';
}
for (i=0; i < m1.size_of_y+2; i++)
{
m1.arr[i] = '*';
m1.arr[(m1.size_of_x+1)*m1.columns + i] = '*';
}
/* mark the starting and ending positions in the maze */
m1.arr[m1.x_start*m1.columns + m1.y_start] = 'S';
m1.arr[m1.end_x*m1.columns + m1.end_y] = 'E';
/* mark the blocked positions in the maze with *'s */
while (fscanf (src, "%d %d", &x_position, &y_position) != EOF)
{
m1.arr[x_position*m1.columns + y_position] = '*';
}
return m1;
}

void printMaze(MAZE m1) {
int i,j;
for (i = 0; i < m1.size_of_x+2; i++)
{
for (j = 0; j < m1.size_of_y+2; j++)
printf ("%c", m1.arr[i*m1.columns + j]);
printf(" ");
}
}

void freeMaze(MAZE m) {
free(m.arr);
}

int main (int argc, char **argv)
{
int i,j;
debug= FALSE;
char *fileName = NULL;
if( argc > 3 ) {
printf("Usage: %s [-d] <maze data file> ", argv[0]);
exit(-1);
}
if( 2 == argc ) {
fileName = argv[1];
}
if( 3 == argc ) {
fileName = argv[2];
if( argv[1][0] == '-' && argv[1][1] == 'd' ) {
debug = TRUE;
}
}
MAZE m1 = make_a_Maze( fileName );
printf("Input Maze ");
printf ("size : %d, %d ", m1.size_of_x, m1.size_of_y);
printf ("start: %d , %d ", m1.x_start, m1.y_start);
printf ("end : %d, %d ", m1.end_x, m1.end_y);
printMaze( m1 );
NodePointer stack = innitialize_stack( m1 );
if( isEmpty( stack ) ) {
printf(" Sorry there is no way to reach the end ");
} else {
printf(" Here is a path through the Maze ");
int old_x = -1;
int old_y = -1;
NodePointer temp = stack;
while( NULL != temp ) {
int x = temp->x;
int y = temp->y;
if(m1.arr[x*m1.columns + y]!='E'
&& m1.arr[x*m1.columns + y]!= 'S' ) {
if((x+1)==old_x) {
m1.arr[x*m1.columns + y] = '#';
} else if((x-1)==old_x ){
m1.arr[x*m1.columns + y] = '#';
} else if((y+1)==old_y ){
m1.arr[x*m1.columns + y] = '#';
} else if((y-1)==old_y){
m1.arr[x*m1.columns + y] = '#';
} else {
printf(" Stack corrdinates: (%d, %d) ", x, y);
exit(-1);
}
}
old_x = x;
old_y = y;
temp = temp->next;
}
printMaze( m1 );
printf(" Coordinates in the path are: ");
printStack( stack );
printf(" ");
}
clearStack( &stack );
freeMaze( m1 );
return 0;
}

Explanation / Answer

import java.io.File;
import java.io.FileNotFoundException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Scanner;


public class Maze


private int out;
private int enter;
private ArrayList<ArrayList<Character>> maze;


  
public Maze (File file) throws FileNotFoundException
{

  
maze = new ArrayList<ArrayList<Character>>();

  
  
Scanner readFile = new Scanner (file);
readFile.nextLine();
exit = readFile.nextInt();
System.out.println(out);
readFile.nextLine();
entrance = readFile.nextInt();
readFile.nextLine();
System.out.println(enter);

while (readFile.hasNextLine())
{
String line = readFile.nextLine();
ArrayList<Character> tempRows = new ArrayList<Character>();
for (int j = 0; j < line.length(); j++)
{
tempRows.add(line.charAt(j));
}
maze.add(tempRows);
  
}
}
public void solvePrintMaze()
{
ArrayList<ArrayList<Character>> solvedMaze = solveMaze();
for (int j = 0; j < solvedMaze.size(); j++)
{
for (int j = 0; j < solvedMaze.get(j).size(); j++)
System.out.print(solvedMaze.get(j).get(j));
System.out.print(" ");
}

}
  


public ArrayList<ArrayList<Character>> solveMaze()
{
ArrayList<ArrayList<Character>> workedMaze = maze;
ArrayList<ArrayList<Character>> solvedMaze = maze;
int rows = maze.size() - 1;

int colus = entrance;
int moves = 0;

while (!(rows == 0 && colus == exit))
{
System.out.println(rows);
System.out.println(colus);
solvedMaze.get(rows).set(colus, 'R');
ArrayList<Character> tempRowSolved = solvedMaze.get(rows);
tempRowsSolved.set(colus, 'R');
ArrayList<Character> tempRowWorked = workedMaze.get(rows);

if (isDeadEnd(rows, colus, workedMaze))
{
tempRowsWorked.set(colus, 'x');
}
workedMaze.set(rows, tempRowsWorked);
System.out.println(workedMaze);
solvedMaze.set(rows, tempRowsSolved);
  
colus = nextColus(rows, colus, workedMaze);
rows = nextRow(rows, colus, workedMaze);

  
}
return solvedMaze;
}


public static boolean canMove (int rows, int colus,ArrayList<ArrayList<Character>> arrayMaze)
{
if (colus< arrayMaze.size() && colus >=0 && rows < arrayMaze.get(rows).size() && rows >= 0 && arrayMaze.get(rows).get(colus).equals( ' '))
{
return true;
}
return false;
}
  
public static boolean canMoveBot (int rows, int colus, ArrayList<ArrayList<Character>> arrayMaze)
{
return canMove(rows+1, colus, arrayMaze);
}
public static boolean canMoveTop (int rows, int colus,ArrayList<ArrayList<Character>> arrayMaze)
{
return canMove(rows-1, colus, arrayMaze);
}
public static boolean canMoveLeft (int rows, int colus, ArrayList<ArrayList<Character>> arrayMaze)
{
return canMove(rows, colus -1, arrayMaze);
}
public static boolean canMoveRight (int rows, int colus, ArrayList<ArrayList<Character>> arrayMaze)
{
return canMove(rows, colus + 1, arrayMaze);
}

public static boolean isDeadEnd (int rows, int colus, ArrayList<ArrayList<Character>> arrayMaze)
{
int n = 0;
if (!(canMoveBot(rows, colus, arrayMaze)))
n++;
if (!(canMoveBot(rows, colus, arrayMaze)))
n++;
if (!(canMoveBot(rows, colus, arrayMaze)))
n++;
if (!(canMoveBot(rows, colus, arrayMaze)))
n++;
if (n >= 3)
return true;
return false;
}

public int nextRows(int rows, int colus, ArrayList<ArrayList<Character>> arrayMaze)
{
if (canMoveTop(rows, colus, arrayMaze))
return rows - 1;
if (canMoveBot(rows, colus, arrayMaze))
return rows + 1;
return rows;
}

public int nextColus(int rows, int colus, ArrayList<ArrayList<Character>> arrayMaze)
{
if (canMoveRight(rows colus, arrayMaze))
return colus + 1;
if (canMoveLeft(rows, colus, arrayMaze))
return colus - 1;
return colus;
}


  
public static void main(String[] args)
{
File mazeFile = new File ("maze1.txt");
Maze testMaze = null;
try
{
testMaze = new Maze(mazeFile);
} catch (FileNotFoundException e) {

e.printStackTrace();
}
if (testMaze!= null)
{
testMaze.solvePrintMaze();
}
else
System.out.println("Maze file cannot be found");
}

}