Problem
#1: Imagine a two-dimensional board
with N rows and M columns. Lets say this board represents the floor of a chamber
that contains a moving particle. In this program, you will model the movement
of this particle on the floor.
Pseudo code:
· Ask the user for input values for
the number of rows and columns of the board (integer input values).
· Create a two dimensional array of
size as specified in the previous step.
· Ask the user for an integer input
value for the total number of steps (n) of the simulation.
· Set all the values in the two
dimensional array to zero. Output the array so that the user can see the
initial state of the floor.
· Place the particle at the center
of the board by the assigning a value of 1 to the cell that is located at the
center of the array.
While your program has not completed n steps
{
Generate a random integer (n)
between 1 and 4.
If n =1, move the particle up.
else if n = 2, move the particle
down.
else if n= 3, move the particle
to the left.
else, move the particle to the
right.
}
· During each of the above
movements, your program has to check if the particles next move is beyond the
boundaries of the floor. Output a line stating the boundary (top, down, left,
right) that the particle crossed. Also, count the number of times the particle
bumped into the boundaries (see example below).
· Since you are only generating
random numbers between 1 and 4, there is a likelihood that the row and column
numbers generated for the next move will be equal to the row and column numbers
of the current position. In that case, simply increment the value in the
current cell of the two-dimensional array by 1 and count this move towards the
total number of steps of the simulation.
· Output the array to show the
number of times the particle visited each cell of the floor. Print the number
of steps that were taken by the particle within the boundary and the total
number of steps taken by the particle that were out of boundary.
· Solution
Program Output (Your answers will
vary):
Two
Dimensional Random Walk
Input length
10
Input width
10
How many
steps?
100
The initial
state of the board
0 0
0 0 0
0 0 0
0 0
0 0
0 0 0
0 0 0
0 0
0 0
0 0 0
0 0 0
0 0
0 0
0 0 0
0 0 0
0 0
0 0
0 0 0
0 0 0
0 0
0 0
0 0 0
0 0 0
0 0
0 0
0 0 0
0 0 0
0 0
0 0
0 0 0
0 0 0
0 0
0 0
0 0 0
0 0 0
0 0
0 0
0 0 0
0 0 0
0 0
The particle
is outside the boundary in the following steps:
Row# =
-1 Column# = 0 - Beyond the upper wall
Row# =
1 Column# = -1 - Beyond the left wall
Row# =
1 Column# = -1 - Beyond the left wall
Row# =
2 Column# = -1 - Beyond the left wall
Row# =
0 Column# = -1 - Beyond the left wall
Row# =
-1 Column# = 0 - Beyond the upper wall
Row# =
-1 Column# = 0 - Beyond the upper wall
Row# =
1 Column# = -1 - Beyond the left wall
The final
state of the board
2 3
1 1 1
1 1 2
0 0
5 7
7 3 5
2 1 1
0 0
4 5
7 4 5
3 1 0
0 0
1 4
5 3 0
0 0 0
0 0
0 0
2 2 1
0 0 0
0 0
0 0
0 0 1
1 0 0
0 0
0 0
0 0 0
0 0 0
0 0
0 0
0 0 0
0 0 0
0 0
0 0
0 0 0
0 0 0
0 0
0 0
0 0 0
0 0 0
0 0
Total within
boundary steps 92
Total out of
boundary steps 8
Problem #2:
a)
Instead of taking an input for the number of steps, your program should
continue to perform the two-dimensional random walk (as described in Problem
#1) until every cell in the two dimensional array is visited at least once.
b)
Also, your program should count the number of steps required to
return to the starting position.
Example
(Your answers will vary):
Two-Dimensional
Random Walk
Input length
5
Input width
7
The initial
state of the board
0 0
0 0 0
0 0
0 0
0 0 0
0 0
0 0
0 0 0
0 0
0 0
0 0 0
0 0
0 0
0 0 0
0 0
Starting
Step -Step #1 2 3
Step #2 3 3
Step #3 4 3
Step #4 4 4
Step #5 4 5
Step #6 4 4
Step #7 4 5
Step #8 3 5
Step #9 3 4
Step
#10 2
4
Step
#11 3
4
Step
#12 4
4
Step
#13 3
4
Step
#14 3
3
Step
#15 4
3
Step
#16 3
3
Step
#17 3
2
Step
#18 3
3
Step
#19 2
3
It took 19
steps to get back to the starting position
Step
#20 1
3
Step
#21 1
2
Step
#22 1
1
Step
#23 1
0
Step
#24 1
1
Step
#25 2
1
Step
#26 1
1
Step
#27 2
1
Step
#28 2
0
Row# =
2 Column# = -1 - Beyond the left wall
.
.
.
Step
#176 2
5
Step
#177 2
4
Step
#178 2
5
Step
#179 3
5
Step
#180 4
5
Step
#181 4
6
Step
#182 3
6
Number of
steps needed to visit every cell atleast once = 182
The final
state of the board
5 6
2 2 4
4 3
7 9
4 6 4
5 5
4 8
5 6 5
7 3
2 2
4 9 10
7 1
3 3
1 4 6
4 1
Total number
of steps that were within the boundary = 161
Total number
of steps that were outside the boundary = 21
Problem
#3:
The program in the link below plays the Tic-Tac-Toe game between
two human players:
Re-write the above program to play Tic Tac Toe between the human player
(yourself) and the computer. If the computer were to simply randomly generate
its row and column choices, the human player would win most of the games. You
should design the program so that the computer strategically selects the row#
and column# depending on the selection (row# and col#) of the human player.
That way, the human player would have to work very hard to win!