Drunken Man Walking (Ex9-12, P.295)

A drunken man gets out of a bar and is so drunk that he will take one step at a time, left or right at random. At some distance on the left is his house, and somewhere on the right, an ice-cold river.

Write a program to ask the user input the initial position p of the man. Assume his house is at position 0, and the river is at position 10. If the initial position is at 3, then after 3 successive left steps, he arrives home. Or, after 7 successive right steps, he falls into the river.

You may find it helpful to have a function simOneIteration() which will end at either position 0 or 10.

In this exercise, you don't need to invoke the curses module. A simple output by print() is good enough.


The program may run as follows.

Input the initial position (p) -- 3
|  *      |
|   *     |
|  *      |
|   *     |
|    *    |
|     *   |
|      *  |
|       * |
|      *  |
|     *   |
|    *    |
|     *   |
|      *  |
|     *   |
|    *    |
|     *   |
|      *  |
|     *   |
|      *  |
|     *   |
|    *    |
|     *   |
|      *  |
|     *   |
|      *  |
|       * |
|        *|
|       * |
|        *|
|       * |
|        *|
|       * |
|      *  |
|     *   |
|      *  |
|       * |
|      *  |
|     *   |
|    *    |
|     *   |
|      *  |
|       * |
|      *  |
|       * |
|      *  |
|       * |
|        *|
|         *
He falls into a river in a cold winter!


Input the initial position (p) -- 3
|  *      |
| *       |
|  *      |
| *       |
|  *      |
|   *     |
|  *      |
| *       |
|*        |
*         |
He arrives home safely.