Drunken Man Walking

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 endIteration() to test whether the drunken man is at either position 0 or 10. Then in simOneIteration(), it will determine the movement of the drunken man by invoking a random number generator rand(). When the value is odd, the drunken man moves a step to the righ; when the value is even, then drunken man moves a step to the left.

In this exercise, you don't need to invoke the curses library. A simple output by cout is good enough.

To allow the robot to judge your program, please do NOT invoke srand() to set your random seed.


The program may run as follows.

Please input the initial position (p) -- 7

|      *  |
|       * |
|        *|
|       * |
|        *|
|       * |
|        *|
He falls into a river in a cold winter!


Please input the initial position (p) -- 3

|  *      |
|   *     |
|    *    |
|   *     |
|    *    |
|   *     |
|    *    |
|     *   |
|    *    |
|   *     |
|  *      |
|   *     |
|  *      |
| *       |
|  *      |
|   *     |
|  *      |
| *       |
|  *      |
|   *     |
|    *    |
|     *   |
|    *    |
|   *     |
|  *      |
| *       |
|*        |
| *       |
|*        |
| *       |
|*        |
He arrives home safely.