0% found this document useful (0 votes)
16 views

Lab29a (1)

The assignment requires students to create a 'Random Maze' program using Java's Stack class to demonstrate knowledge of backtracking algorithms. The maze is a 12x12 matrix with walls and paths, and the program must find a way out using a stack to track visited locations. There are two versions of the assignment, with the 100-point version requiring completion of the maze-solving methods, while the 80-point version provides assistance for a specific method.

Uploaded by

emailao031
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Lab29a (1)

The assignment requires students to create a 'Random Maze' program using Java's Stack class to demonstrate knowledge of backtracking algorithms. The maze is a 12x12 matrix with walls and paths, and the program must find a way out using a stack to track visited locations. There are two versions of the assignment, with the 100-point version requiring completion of the maze-solving methods, while the 80-point version provides assistance for a specific method.

Uploaded by

emailao031
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 8

AP Computer Science II Java Unit Assignment # 29a

The "Random Maze" Program 80 & 100 Point Versions

Assignment Purpose:
Demonstrate knowledge of Java's Stack class. For this assignment you will write
a program with the standard Stack class. Without knowledge or even concern
for the implementation of this stack class, write the specified "Maze" program.

Write a program that randomly generates a maze, and a random starting position. The program then
needs to find a path out off the maze, if it is possible, to exit the maze, using a backtrack algorithm.
Backtracking is an excellent example of the importance of the stack ADT.

The maze is a 12 X 12 matrix that uses the following symbols:

X -SOLID WALL (no passage allowed)

O -PATH (passage allowed in 8 directions)

. -TRAVELED (path traveled to find the exit)

The maze is solved using the Stack class. The Stack object needs to stores coordinate values of
every visited location. The interface information of the Stack class is provided below.

MyStack Interface Information


public MyStack()
// Initializes an empty array object with references of private variable data objects.

public boolean isEmpty()


// Returns true if stack is empty, false otherwise

public void push (Object x)


// Adds variable x to the top of the stack

public Object pop()


// Returns and removes the top element from the stack

public Object peek()


// Returns the top element from the stack without removal

Exposure Java 2008 Chapter Lab 29a Page 1 07-26-08


The essence of this assignment is to demonstrate knowledge of the stack ADT and backtracking
techniques in solving the maze. A considerable part of the program is already provided. Those
methods that are specific to the solving of the maze are left incomplete. The file printed below will be
available for practice and at the time that this assignment is graded.

// Lab29ast.java
// This is the student version of the Lab29a assignment.
// Completing this file, as is, is the 100 point version.
// For 80 points you will be given the <getMove> code.

import java.util.*;

public class Lab29ast


{
public static void main(String args[])
{
System.out.println("\nLab 29a 80/100 Point Version\n");
Scanner input = new Scanner(System.in);
System.out.print("Enter random starting seed ===>> ");
int seed = input.nextInt();

Maze maze = new Maze(seed);


maze.displayMaze();
maze.solveMaze();
maze.displayMaze();
maze.mazeSolution();
}
}

class Maze
{

private char mat[][]; // 2d character array that stores the maze display
private Coord currentMove; // object that stores current maze position
private Stack visitStack; // stack that stores location that have been visited

class Coord
// Coord is a class that stores a single maze location.
{
private int rPos;
private int cPos;
public Coord (int r, int c) { rPos = r; cPos = c; }
public boolean isFree() { return (rPos == 0 && cPos == 0); }
public void setPos(int r, int c) { rPos+= r; cPos+= c; }
}

public Maze(int seed)


// constructor which generates the random maze, random starting location
// and initializes Maze class values. If the random value equals 0 the maze
// store an 'X' otherwise it store an 'O' in the maze.
{
Random random = new Random(seed);
int startRow, startCol;
mat = new char[12][12];
for (int r = 0; r < 12; r++)
for (int c = 0; c < 12; c++)
{
if (r == 0 || c == 0 || r == 11 || c == 11)
mat[r][c] = 'X';
else
{
int rndInt = random.nextInt(2);
if (rndInt == 0)
mat[r][c] = 'X';
else

Exposure Java 2008 Chapter Lab 29a Page 2 07-26-08


mat[r][c] = 'O';
}
}
mat[0][0] = 'O';
startRow = random.nextInt(12);
startCol = 11;
mat[startRow][startCol] = '.';
visitStack = new Stack();
currentMove = new Coord(startRow,startCol);
visitStack.push(currentMove);
}

void displayMaze()
// displays the current maze configuration
{
System.out.println("\nRANDOM MAZE DISPLAY\n");
for (int r = 0; r < 12; r++)
{
for (int c = 0; c < 12; c++)
System.out.print(mat[r][c] + " ");
System.out.println();
}
System.out.println();
pause();
}

public void solveMaze()


// This methods solves the maze with private helper method <getMove>.
// A loop is needed to repeat getting new moves until either a maze solution
// is found or it is determined that there is no way out off the maze.
{
System.out.println("\n>>>>> WORKING .... SOLVING MAZE <<<<<\n");

public void mazeSolution()


// Short method to display the result of the maze solution
{
if (currentMove.isFree())
System.out.println("\nTHE MAZE HAS A SOLUTION.\n");
else
System.out.println("\nTHE MAZE HAS NO SOLUTION.\n");
}

private boolean inBounds(int r, int c)


// This method determines if a coordinate position is inbounds or not
{

private boolean getMove()


// This method checks eight possible positions in a counter-clock wise manner
// starting with the (-1,0) position. If a position is found the method returns
// true and the currentMove coordinates are altered to the new position
{

private void pause()


{
Scanner input = new Scanner(System.in);
String dummy;
System.out.print("\nPress <Enter> to continue ===>> ");
dummy = input.nextLine();
}

Exposure Java 2008 Chapter Lab 29a Page 3 07-26-08


80-Point and 100-Point Versions
There is no difference in the appearance of the 80-point and 100-point version of this lab
assignment. If you complete this assignment with only the provided student file, you will earn 100
points. If you require additional help, which will be a sheet with the getMove method, you will earn
80 points. You will only be provided with this sheet if you request the help. Once you receive the
help sheet it is not possible to get higher than 80 points.

Lab29a 100 Point Version Two Separate Required Outputs


Lab 29a 100 Point Version

Enter random starting seed ===>> 100

RANDOM MAZE DISPLAY

O X X X X X X X X X X X
X O O X O O X O X O O X
X O X X X X X O O O X X
X X X O O O X X X X X X
X O O O X O O X O X X X
X X O X O O X O O X X X
X X O X O X O X X X O .
X X O O O X O O X O O X
X O O X O X O O O O X X
X X X O X X X X X O X X
X O O X X O O X X X X X
X X X X X X X X X X X X

Press <Enter> to continue ===>>

>>>>> WORKING .... SOLVING MAZE <<<<<

RANDOM MAZE DISPLAY

O X X X X X X X X X X X
X O O X O O X O X O O X
X O X X X X X O O O X X
X X X . . . X X X X X X
X . . . X . . X . X X X
X X . X . . X . . X X X
X X . X . X . X X X . .
X X . . . X . . X . . X
X . . X . X . . . . X X
X X X . X X X X X . X X
X . . X X O O X X X X X
X X X X X X X X X X X X

Press <Enter> to continue ===>>

THE MAZE HAS NO SOLUTION.

Exposure Java 2008 Chapter Lab 29a Page 4 07-26-08


Second Execution on the next page

Lab 29a 100 Point Version

Enter random starting seed ===>> 25

RANDOM MAZE DISPLAY

O X X X X X X X X X X X
X O O X O O X X X X X X
X O O X O X O X O O O X
X X X O O X X O O O X X
X X X X O O O O O X O X
X X X X O O O X X X X X
X O X O X X O X O X O X
X X X X X O O X X X O X
X X X O O X X O O X O X
X O X X O O O X O O O X
X X X X X O O X O X O .
X X X X X X X X X X X X

Press <Enter> to continue ===>>

>>>>> WORKING .... SOLVING MAZE <<<<<

RANDOM MAZE DISPLAY

. X X X X X X X X X X X
X . . X . . X X X X X X
X . . X . X . X . . . X
X X X . . X X . . . X X
X X X X . . . . . X . X
X X X X . . . X X X X X
X O X O X X . X O X . X
X X X X X . O X X X . X
X X X O . X X . . X . X
X O X X . . . X . . . X
X X X X X . . X . X . .
X X X X X X X X X X X X

Press <Enter> to continue ===>>

RANDOM MAZE DISPLAY

. X X X X X X X X X X X
X . . X . . X X X X X X
X . . X . X . X . . . X
X X X . . X X . . . X X
X X X X . . . . . X . X
X X X X . . . X X X X X
X O X O X X . X O X . X
X X X X X . O X X X . X
Exposure Java 2008 Chapter Lab 29a Page 5 07-26-08
X X X O . X X . . X . X
X O X X . . . X . . . X
X X X X X . . X . X . .
X X X X X X X X X X X X

Press <Enter> to continue ===>>

THE MAZE HAS A SOLUTION.

AP Computer Science I Java Keyword Lab Assignment # 26B

The "Stack Implementation" Program 100 Point Version only

Assignment Purpose:
Demonstrate knowledge of implementing required stack methods.

Write a MyStack class, which implements the standard stack methods. You will be provided with a
student test program that checks to see if your class works correctly. This is one of the few
assignments with a single, 100-point version.

You will need to implement the following methods listed below. Required parameters, if any, and
data types are intentional not shown.

MyStack
// Constructor

isEmpty
// Returns true if data is empty, false otherwise

push
// Adds variable to the top of the stack

pop
// Returns and removes the top element from the stack

peek
// Returns the top element from the stack without removal
Exposure Java 2008 Chapter Lab 29a Page 6 07-26-08
Lab29b 80 Point Version One Required Output
Lab29b 80 Point Version

Testing the <push> method

Testing the <getSize> method


There are 4 students stored on the stack

Testing the <peekTop> method


Jamie Singbush is at the top of the stack

Testing the <isEmpty> and <pop> methods


Popping Jamie Singbush from the stack
Popping Mike Lewis from the stack
Popping Brian Sims from the stack
Popping Luke Watts from the stack

Lab29b 100 Point Version One Required Output


Lab29b 100 Point Version

Testing the <push> method

Testing the <getSize> method


There are 4 students stored on the stack

Testing the <peekTop> method


Jessie Lin is at the top of the stack

Testing the <isEmpty> and <pop> methods


Popping Jessie Lin from the stack
Popping Mike Lewis from the stack
Popping Brian Sims from the stack
Popping Luke Watts from the stack
There are 0 students stored on the stack

Exposure Java 2008 Chapter Lab 29a Page 7 07-26-08


Testing the copy constructor
Popping Jessie Lin from the stack
Popping Mike Lewis from the stack
Popping Brian Sims from the stack
Popping Luke Watts from the stack

Exposure Java 2008 Chapter Lab 29a Page 8 07-26-08

You might also like