Skip to content

Instantly share code, notes, and snippets.

Created November 25, 2012 01:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/4142095 to your computer and use it in GitHub Desktop.
Save anonymous/4142095 to your computer and use it in GitHub Desktop.
BACKGROUND
In his book, Full House, Stephen Jay Gould uses illustrations to help make a point about the progression (or lack or progression) of a species toward greater complexity. Such examples include the disappearance of the .400 batting average in baseball and a drunk randomly staggering along a wide side walk bounded on one side by a building wall and on the other by a curb. Of course, once against the wall, the drunk can stagger no closer; however, he is free to stagger in the opposite direction until he falls off the curb into the street. In the middle (or interior) of the sidewalk, he can stagger in either direction. We will go no further in explaining how these relate to the progression of complexity in a species.
The example of the drunk staggering along the sidewalk can be extended to a two-dimensional situation to obtain some interesting paths of staggering in the following way. Imagine a |`|-shaped building enclosing a rectangular court yard, bounded on three sides by building walls and open on the remaining side. The plot can be simulated by an M X N two-dimensional array. We will let our drunkard start in the upper left hand corner, [0][0] position, and then randomly move in horizontal, vertical or diagonal directions one step at a time. Diagonal progressions are allowed. Of course, if he is next to the wall he can’t move any farther in the direction toward the wall. For example, in the upper left corner, if his direction is determined to be to the left and up, he can’t move, and that would count him as visiting that same position again. Now we let him walk randomly, counting the number of times he hits each square and counting each step, even if he stays in the same place. He walks until he exits downward from the bottom row.
In this assignment you are to write a class to represent the courtyard bounded on three sides by three buildings forming a rectangular courtyard. With this class you will need to instantiate an object representing :
 the courtyard,
 the location of the drunk inside the yard both row and column,
 the number of steps he has taken,
 whether or not he has stepped out of the courtyard through the non-bounded edge,
 how many times he has visited each square in the array,
 two one-dimensional arrays of doubles each of length 3 holding the probabilities that his horizontal move is -1(left), 0, 1(right) and the probabilities that his vertical move is -1(up), 0, 1(down).
When making a one-step move here are the rules. Based on the probabilities given, first decide which direction to move to a new row, by computing -1, 0, or 1. If this move is 1 and it would take him out of the courtyard, set the “outofbounds” flag to true, and don’t do anything else (other than increment the number of steps). If it is -1 and he is already against the top boundary, don’t change rows. After the row is determined, compute a column move, keeping in mind that if there is an attempt to go beyond either wall (left or right) the column does not change. When the row and column locations have been updated, then increment the corresponding courtyard location and also increment the number of steps. These are incremented even if he does not move.
Non-static Methods Needed
 A constructor to initialize the courtyard to the dimensions specified as arguments. It should also appropriately initialize the array and all instance variables in the class. The two probability vectors should each be set to .33333, .33333, .33333 (representing equal likelihood to move in any of the directions.
 A method to advance the drunk one time unit based on the rules for moving described above.
 A method to set new probabilities for a vertical move.
 A method to set new probabilities for a horizontal move.
 A method to return true if the drunk has stepped out of the courtyard and false otherwise.
 A method to print the drunk’s history after he has exited the courtyard. This method will print M rows of characters with N characters on each line, where M and N represent the dimensions of the courtyard. The character printed will be based on the integer value of each entry in the array, based on the following mapping:
o 0: print a blank
o 1 to 4: print ‘-‘
o 5 to 9: print ‘0’
o 10 to 14: print ‘*’
o > 14: print ‘@’
After printing these characters, the last thing it should do is print vertically E X I T and then print a line indicating that he stepped out in column ? after ? steps. See sample output below.
Static Variables and Methods
 There should be one static variable of class type Random to be used in computing the direction the drunk moves.
 There should be one static method with a double array parameter, that will return -1, 0, or 1 based on the three probabilities in the array.
The main program should do the following:
 Ask the user to input the number of rows and columns;
 Instantiate an object with those rows and columns;
 Ask the user if he or she desires to change the probabilities;
o if no, then proceed with running the program with equal probabilities.
o if yes, then get new probabilities for vertical and horizontal moves. Then reset the probabilities using appropriate methods. NOTE: Probabilities should be between 0 and 1 inclusive, and the sum of the three should be 1.0. However, you don’t want to compare doubles for equality, so test that Math.abs(sum-1.0) <= 1.0E-5.
 Let the program run until the drunk exits the courtyard.
 Print the configuration of the courtyard based on the character mappings given above.
WARNING: If you set a vertical probability that is low for +1, this thing could run for a long long time. A high +1 probability will cause it to terminate quickly. What will low and high horizontal probabilities for -1 or 1 do?
Once your program is working, play with it and see how changing the probabilities affects the outcome.
Enter the number of courtyard rows: 40
Enter the number of courtyard columns: 40
Do you want to change the probabilities? (Y or N): Yes
Answer must be Y or N. Re-enter: Yep
Answer must be Y or N. Re-enter: y
Enter three Vertical probabilities separated by white space: -.1 .8 .3
Invalid probabilities!
Each must be in [0,1] and the sum must be within 1.0E-5 of 1.0
Enter three Vertical probabilities separated by white space: .33 .33 .33
Invalid probabilities!
Each must be in [0,1] and the sum must be within 1.0E-5 of 1.0
Enter three Vertical probabilities separated by white space: .33 .34 .33
Enter three Horizontal probabilities separated by white space: .3333333 .3333333 .3333333
**0- ----0-- ---000*@***@0@@@@0----00-
@*----00---- -0-000-00*0**0**@@@*0---0*0
**00-0--0-------000000@@**@***@0*0 --00*
0-00--0--00-----00-0*@*0@****0000--00-00
---- -0---0---- --**@@*@**--**-0*0-0--00
----0--0-----0---00*@@@@*0-****000000--0
--- ------ ---0000000@*000**00000- ---0
000 ---- --0000-0-00**0**0000--0- 0*0*
--- ------00-- 00-0*@@@00-------000
--- ----- ---0*------0-0**@*00--00----0
---- ----0 - --*0*@@@0---0*--0--
-0-- ---- ----@0@000----0--0--
-- - ---00*0000-0-*0----
- -- -0---0-----0*0-0--
- -- --0------0-0-00-0-0
-- ---- ---000- -- -0-
- --- ------0----- -
-----0-----0-------- -
- --00--0--00-- - --
----0------00---0-- -----
--------- 0000--0--0-- ---0-
- ------ ---00-0--0 - -00-
---------- -00000- ----
-0----0- -0--0 -----
----0-- --00-- -
---0- --0----*0-- ---
---0- ------*00 - -- ------
------ - -- -0*0------0- -- --
---- ---0 - --0----00-0 --0-
----0-- ----00**-00- -0
- --0-- - --*0@*--- --
------- - -*0000-0 -----
- ---- -0000- ----0-0
--------0- -----
-- --------
--------
----0---
--0-00-
--0---
----
E
X
I
T
He exited in column 36 after 4243 steps.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment