Ok I have this group project where we have to modify the Drunkard's Walk Program so there is a 50% probability the professor will continue to walk in the same direction as he or she did when taking the previous step. There should be a 10% probability that he or she will reverse direction and a 20% probability that he or she will turn either right or left. Assign a default starting direction before the loop begins.
This is the Original Program:
TITLE Drunkard's Walk (Walk.asm)
; Drunkard's walk program. The professors starts at
; coordinates 25,25 and wanders around the immediate area.
INCLUDE Irvine32.inc
WalkMax = 50
StartX = 25
StartY = 25
DrunkardWalk STRUCT
path COORD WalkMax DUP(<0,0>)
pathsUsed WORD 0
DrunkardWalk ENDS
DisplayPosition PROTO currX:WORD, currY:WORD
.data
aWalk DrunkardWalk <>
.code
main PROC
mov esi,OFFSET aWalk
call TakeDrunkenWalk
exit
main ENDP
;-------------------------------------------------------
TakeDrunkenWalk PROC
LOCAL currX:WORD, currY:WORD
;
; Take a walk in random directions (north, south, east,
; west).
; Receives: ESI points to a DrunkardWalk structure
; Returns: the structure is initialized with random values
;-------------------------------------------------------
pushad
; Use the OFFSET operator to obtain the address of
; path, the array of COORD objects, and copy it to EDI.
mov edi,esi
add edi,OFFSET DrunkardWalk.path
mov ecx,WalkMax ; loop counter
mov currX,StartX ; current X-location
mov currY,StartY ; current Y-location
Again:
; Insert current location in array.
mov ax,currX
mov (COORD PTR [edi]).X,ax
mov ax,currY
mov (COORD PTR [edi]).Y,ax
INVOKE DisplayPosition, currX, currY
mov eax,4 ; choose a direction (0-3)
call RandomRange
.IF eax == 0 ; North
dec currY
.ELSEIF eax == 1 ; South
inc currY
.ELSEIF eax == 2 ; West
dec currX
.ELSE ; East (EAX = 3)
inc currX
.ENDIF
add edi,TYPE COORD ; point to next COORD
loop Again
Finish:
mov (DrunkardWalk PTR [esi]).pathsUsed, WalkMax
popad
ret
TakeDrunkenWalk ENDP
;-------------------------------------------------------
DisplayPosition PROC currX:WORD, currY:WORD
; Display the current X and Y positions.
;-------------------------------------------------------
.data
commaStr BYTE ",",0
.code
pushad
movzx eax,currX ; current X position
call WriteDec
mov edx,OFFSET commaStr ; "," string
call WriteString
movzx eax,currY ; current Y position
call WriteDec
call Crlf
popad
ret
DisplayPosition ENDP
END main
This is what we've tried so far
TITLE Drunkard's Walk (Walk.asm)
; Drunkard's walk program. The professors starts at
; coordinates 25,25 and wanders around the immediate area.
INCLUDE Irvine32.inc
WalkMax = 50
StartX = 25
StartY = 25
DrunkardWalk STRUCT
path COORD WalkMax DUP(<0,0>)
pathsUsed WORD 0
DrunkardWalk ENDS
DisplayPosition PROTO currX:WORD, currY:WORD
.data
aWalk DrunkardWalk <>
.code
main PROC
mov esi,OFFSET aWalk
call TakeDrunkenWalk
exit
main ENDP
;-------------------------------------------------------
TakeDrunkenWalk PROC
LOCAL currX:WORD, currY:WORD
;
; Take a walk in random directions (north, south, east,
; west).
; Receives: ESI points to a DrunkardWalk structure
; Returns: the structure is initialized with random values
;-------------------------------------------------------
pushad
; Use the OFFSET operator to obtain the address of
; path, the array of COORD objects, and copy it to EDI.
mov edi,esi
add edi,OFFSET DrunkardWalk.path
mov ecx,WalkMax ; loop counter
mov currX,StartX ; current X-location
mov currY,StartY ; current Y-location
Again:
; Insert current location in array.
mov ax,currX
mov (COORD PTR [edi]).X,ax
mov ax,currY
mov (COORD PTR [edi]).Y,ax
INVOKE DisplayPosition, currX, currY
mov eax,10 ; choose a direction (0-3)
call RandomRange
.IF (eax == 0 || eax <= 4) ; North
dec currY
.ELSEIF eax == 5 ; South
inc currY
.ELSEIF (eax == 6 || eax <= 7) ; West
dec currX
.ELSE ; East (EAX = 3)
inc currX
.ENDIF
add edi,TYPE COORD ; point to next COORD
loop Again
Finish:
mov (DrunkardWalk PTR [esi]).pathsUsed, WalkMax
popad
ret
TakeDrunkenWalk ENDP
;-------------------------------------------------------
DisplayPosition PROC currX:WORD, currY:WORD
; Display the current X and Y positions.
;-------------------------------------------------------
.data
commaStr BYTE ",",0
.code
pushad
movzx eax,currX ; current X position
call WriteDec
mov edx,OFFSET commaStr ; "," string
call WriteString
movzx eax,currY ; current Y position
call WriteDec
call Crlf
popad
ret
DisplayPosition ENDP
END main
We tried to modify the if statements to match the percentages. As you can see we aren't getting far and I was wondering if you all had some suggestions....Thanks :bg
The first thing I would ask is what is the time (number of steps) over which the percentages must be met.
Then divide that number of steps by 10 to form blocks of steps, then fill an array with 5 F's, 1 B, 2 L's and 2 R's for each block, then randomize the array. Actually this must be tempered with the edge conditions, i.e. don't let the drunk walk over the edge. As the walk progresses, if an indicated move is blocked, then swap the move letter with the next letter (keep swapping deeper in the array until a suitable move can be found) then proceed. Repeat the array building when all moves are made.
Dave.
to start with, the new program has to keep track of the last step
because the "decisions" are based on that
this is a feature that the original program did not have
subsequently, you also need some way to give him a first step - a little tricky, there
then, you need a larger range of random values
we can use 10 :P
mov eax,10 ; choose a direction (0-9)
call RandomRange
now, write the IF THEN ELSE stuff so that
1) if EAX = 0, reverse direction
2) if EAX = 2 or 3, turn left
3) if EAX = 4 or 5, turn right
4) otherwise (6-9), go the same direction as last time
this will tend to make the professor appear a little more sober - lol
Dave,
Maybe not the first step, but at least an initial direction in which he is facing.
I see possible problems with just a random number. the percentage of occurrence of any number may not be random enough to keep the percentage within range. You also have the edge problem.
Dave.
well - that is true of the original program, as well
you can keep an array of 10 values and force them to be equally chosen if you want to ensure he stays on screen
however, i doubt that is within the scope of the assignment
the whole idea of time would way over-complicate it
let me correct that by saying.....
if there is a 50% chance of him going the same direction, he will leave the screen sooner than the original program :bg
this may well be the criteria used to determine success
Probably right.