| CS306 |
Processing Systems and Structures |
Lockwood, Spring 2002 |
Machine Problem 4: The Maze (Part II)
| Assigned |
Monday, March 18 |
| Due Date |
Friday, March 29 |
| Purpose: |
Algorithms, Recursion |
| Points |
50 |
Introduction
Consider again the
mouse of Machine Problem 3.
For this assignment. For this assignment, you will write
procedures that automatically guide the mouse through the maze to
find the solution.
You will first implement an advance (Adv) routine to move the
mouse in the maze by a single position.
The advance routine checks
that the move is valid before moving the mouse.
A valid move is one that
does not put the mouse into a wall.
This routine is used by both by manual mode and turbo mode.
Turbo mode allows the mouse to automatically
advance forward through straight hallways.
Next, you will implement the 'Auto-Advance' function
which automatically forwards the mouse through hallways and around turns.
Recall that at each location in the maze is marked with
a decision point where two or more hallways meet.
Your 'Auto-Advance' routine will advance the mouse until reaching
such a decision point.
Lastly, you will implment 'Auto-Solve' which
implements a Depth First Search (DFS) algorithm. This procedure
will automatically guide the mouse out of the maze.
Problem Description
There are four modes of operation to this program: Manual, Turbo,
AutoAdvance, and AutoSolve. Modes can be selected by pressing the 'M' key,
followed by a number (0 to 3). In manual mode (M0), pressing the arrows keys
will advance you by one position. In turbo mode (M1), the mouse will run down a
hallway until hitting a wall. In AutoAdvance mode (M2), the mouse will traverse
down a hallway until hitting a decision point. In AutoSolve mode (M3), the mouse
will find the solution to the maze automatically. The mode of the program is
displayed at the top-center of the screen.
In modes 1-3, the speed of the mouse can be adjusted. The (+) and (-) keys
determine the delay rate. A larger rate slows the mouse, while a smaller makes
the mouse move faster. The delay rate is displayed at the top-right of the
screen.
Recall that the _MAZE variable contains an array
of HALL, WALL, DECPOS, and ENDPOS constants.
A decision point is a location in the maze where the
mouse must decide which way to travel (i.e., the hallway branches).
For this machine problem, we will sub-divide the decision points into
three types of nodes: the original (unvisted)
decision point (DECPOS), a visited
decision point (VISPOS), and finished decision point (FINPOS).
You can toggle the type of decision point by pressing the 'v'
key.
The ASM procedures that you will need to implement are described below.
There are working library versions of each of these procedures in
libmp3.lib. Unless otherwise noted, it is expected that that
all subroutines will
preserve the value of any register that they modify.
The only exception is for registers that are used as outputs of
procedures.
Adv
- Purpose: Advance position by one character (if possible)
- Variables: _MAZE read, _POS modified.
- Inputs:
- AL indicates direction (NORTH, EASH, SOUTH, WEST)
- Outputs:
- AL: unchanged (still holds current direction)
- AH: AH=0 indicates a valid move, AH=1 indicates invalid move
- SI: Original position of mouse
- DI: New position of mouse (after advancing)
- Description:
This routine allows the mouse to move. This procedure is called with AL
holding the desired direction (NORTH, EAST, SOUTH, or WEST). A move is only
possible as long as the new position in the maze is
NOT inside a wall. It is valid if it is the endpoint.
The current position of the mouse in the maze is determined by the variable
_POS. This value should always be returned in SI.
If a move is possible, the new position should be computed then stored in
_POS and returned in DI. Further, AH should be set to zero to
indicate a valid move.
If a move is not possible, AH should be set to 1 and DI should return the
original position. (i.e., DI=SI=_POS).
AutoAdv
- Purpose: Advance the mouse through the maze until hitting a
decision point.
- Inputs: AL holds starting direction
- Outputs:
- DI: New position of mouse [like it was for Adv]
- SI: Original position of mouse [previous value of DI]
- AL: Final direction
- Variables: _MAZE read, _POS modified.
- Useful Constants: NORTH, EAST, SOUTH, WEST, DECPOS, VISPOS,
FINPOS, ENDPOS.
- Description:
This routine allows the mouse to move through hallways and turn at corners
of the maze. The mouse will continue to explore the maze until it reaches an
intersection (decision point) or hits the end of the maze.
This routine should use Adv, UpdateScreen and
Delay to move though straight hallways. When the mouse runs into a
wall, it will turn and walk down the hallway further. If the mouse reaches a
dead-end, it turns around and returns to the decision point where it started
from.
Decision points may be colored by the AutoSolve function. This
function should stop at ANY decision point (DECPOS, VISPOS, and
FINPOS), or at the end of the maze (ENDPOS). Register AL
should return with the direction of the mouse when it stopped.
AutoSolve
- Purpose: Automatically solve the maze
- Inputs: AL holds starting direction
- Outputs:
- DI: New position of mouse [like it was for Adv]
- SI: Original position of mouse [previous value of DI]
- AL: Final direction
- Variables: _MAZE read (decision points can be modified), _POS modified.
- Description
This is a recursive procedure that calls itself, AutoAdv,
Adv, UpdateScreen, and Delay to automatically
solve the maze.
The mouse uses a depth-first-search to find it's way to the endpoint
of the maze. From a given starting point and direction, the mouse will begin
by walking deeper and deeper into the maze. When the mouse hits a dead-end,
it backs up, and explores another direction. In the library function, the
stuck mouse next tries walking to the right. After fully exploring that
region it next tries walking to the left. If, exploring all possible
directions does not
lead to the solution, the the mouse backs up the the previous decision point.
The depth-first-search works by labeling decision points as unvisited,
visited, or finished. Initially all decisions points are unvisited.
When the algorithm reaches another unvisited decision point, it marks
it as visited and recursively walks deeper into the maze. If it is
found that no further progress can be made by walking in any direction (i.e.,
all hallways lead to a dead-end or to an already-explored region of the
maze), the decision point is labeled as finished and the algorithm
back-tracks to the previous decision point. To avoid infinite looping,
previously-visited decision points (either visited or finished)
are never re-explored. A node is only revisited if it was necessary to
back-track.
For our problem, decision points are colored blue, green, or red. Constants
are defined in maze.def to define the values in _MAZE and the
attribute:character value for the video display. They three types of decision
points are summarized below:
Decision-Point Type |
Color |
_MAZE value |
Attribute: Character |
| Unvisited
| + Blue
| DECPOS
| DECCH |
| Visited
| + Green
| VISPOS
| VISCH |
| Finished
| + Red
| FINPOS
| FINCH |
Points
You earn points by replacing each subroutine with your own code.
Your score will be proportional to the percentage of the code that your write
yourself. The breakdown in points is given below. Your routine MUST perform all
functions of the subroutine to receive credit.
- Adv: 15 pts
- AutoAdv: 15 pts
- AutoSolve: 20 pts
Starting Files
- This program uses the same code and library routines as MP3.
No additional downloads are required.
- To begin this program, just continue editing your MP3.ASM file,
- Change the comments at the top of MP3.ASM
to indicate that you are working on MP4, as shown below
Relevant portions of MP3 Code for MP4
; You Name Here : ____________________________________
; CS306: Machine Problem 4, Spring 2002
; Prof. John W. Lockwood
; Washington University, Department of Computer Science
; Ver. 2.1
; ================ Constants / Definitions / MACROs =====================
; MAP Elements
WALL EQU 0
ENDPOS EQU 1
HALL EQU 2
DECPOS EQU 3 ; Original Decision point
VISPOS EQU 4 ; Visited Decision point
FINPOS EQU 5 ; Finished Decision point
; .. More ..
; Modes
MODEMAN EQU 0
MODEADV EQU 1
MODEAUTOADV EQU 2
MODEAUTOSOLVE EQU 3
; .. More ..
; Public variables (used by libmp3)
GLOBAL Movement, mazemode, mazedelay, updatescreen
; =================== External Library Procedures =======================
; Your code can call these library routines (but not for free)
EXTERN LibAdv
EXTERN LibAutoAdv
EXTERN LibAutoSolve
; The library routines may need call these
; procedures in your code
GLOBAL AutoAdv
GLOBAL AutoSolve
GLOBAL Adv
; .. More ..
mazemode db 0 ; By default, Start in Manual Mode
mazedelay db 16 ; Default Delay Period
; ------------------------------------------------------------------------
Adv:
Call LibAdv; [ Subject of Machine Problem 4 ]
ret
; ------------------------------------------------------------------------
AutoAdv:
Call LibAutoAdv ; [ Subject of Machine Problem 4 ]
ret
; ------------------------------------------------------------------------
AutoSolve:
Call LibAutoSolve; [ Subject of Machine Problem 4 ]
ret
; ------------------------------------------------------------------------
_MazeManual: ; This code is given to you for free.
; It helps to understand this code in order
; to write the AutoAdv and AutoSolve routines.
; Purpose: Interactively allows user to traverse maze and run AutoSolve
; Variables: _MAZE, mazemode, mazedelay
; Input: From keyboard
; Output: None.
PUSH BP
PUSH DS
PUSH ES
PUSH SI
PUSH DI
MOV AX,VIDTEXTSEG ; Use ES=Video Segment
MOV ES,AX
MOV BL,NORTH ; By default, go north
MMLoop: Call kbdin
CMP AL,'Q'
JNE MMNotDone1
JMP MMDone
MMNotDone1:
CMP AL,'q'
JNE MMNotDone2
JMP MMDone
MMNotDone2:
CMP AL,FORWARDKEY
JE MMForward
CMP AL,BACKWARDKEY
JE MMBackward
CMP AL,LEFTKEY
JE MMLeft
CMP AL,RIGHTKEY
JE MMRight
CMP AL,'m'
JE MMode
CMP AL,'+'
JE MMSlower
CMP AL,'-'
JE MMFaster
CMP AL,'g'
JE MMGrMode
CMP AL,'t'
JE MMTMode
CMP AL,'v'
JE MMMark
JMP MMLoop
; Check for Keyboard Arrow Keys (up, down, left, right)
; Redefine keyboard - forward , back , left turn , right turn
MMForward: MOV AL,BL ; Go Forward
JMP MMArrow
MMBackward: MOV AL,BL
ADD AL,2 ; Reverse direction
AND AL,3
CALL Adv ; Advance 1
MOV AL,BL ; Set Direction to forward
CALL UpdateScreen ; Show screen
JMP MMLoop
MMLeft: ADD BL,3 ; Go Left
AND BL,3
MOV AL,BL
; MOV SI,[_POS] ; should not be needed
; MOV DI,[_POS]
Call UpdateScreen
JMP MMLoop
MMRight: ADD BL,1 ; Go Right
AND BL,3
MOV AL,BL
; MOV SI,[_POS]
; MOV DI,[_POS]
Call UpdateScreen
JMP MMLoop
; Set Game Mode ('m0','m1','m2','m3')
MMode: Call kbdin
CMP AL,'0'
JB MMLoop
CMP AL,'3'
JA MMLoop
SUB AL,'0'
MOV [mazemode],AL
Call ShowMode
JMP MMLoop
; Control Interactive Speed (Smaller Mazedelay==Faster)
MMFaster: DEC byte [mazedelay]
Call ShowMode
JMP MMLoop
MMSlower: INC byte [mazedelay]
Call ShowMode
JMP MMLoop
MMGrMode: ; MP5 will support Graphics mode
; For now, do nothing.
JMP MMLoop
MMTMode: TMODE ; (Macro in MAZE.DEF) - Switch back to 80x25 Text Mode
mov byte [VidMode],TEXTMODE
Call ShowMaze
Call ShowMode ; Need to redraw (entire) text screen
JMP MMLoop ; after switching modes
MMMark: mov DI,[_POS] ; New Feature - Hit 'v' to visit/unvisit node
cmp byte [_MAZE+DI],DECPOS
jne MMMark2
mov byte [_MAZE+DI],VISPOS
jmp MMMarkD
MMMark2: cmp byte [_MAZE+DI],VISPOS
jne MMMark3
mov byte [_MAZE+DI],FINPOS
jmp MMMarkD
MMMark3: cmp byte [_MAZE+DI],FINPOS
jne MMLoop
mov byte [_MAZE+DI],DECPOS
MMMarkD: MOV AL,BL
MOV SI,DI
Call UpdateScreen
JMP MMLoop
MMArrow: CMP byte [mazemode],3 ; AutoSolve Mode
JE MMASolve
CMP byte [mazemode],2 ; AutoAdvance Mode
JE MMAutoAdv
MMAdv: Call Adv ; Movement Loop
Call UpdateScreen
CMP byte [mazemode],0
JE MMLoop
CMP byte [_MAZE+DI],HALL
JNE MMLoop
CMP AH,1
JE MMLoop
Call delay
JMP MMAdv
MMAutoAdv: mov al,bl
Call AutoAdv
mov bl,al
JMP MMLoop
MMASolve: mov al,bl
Call AutoSolve
mov bl,al
JMP MMLoop
MMDone: MOV AX,0 ; Return value
POP DI
POP SI
POP ES
POP DS
POP BP
RET