Question: Java program using threads. Each walker has a thread and each of the four threads is to continue running until Phase 1 is complete. The

Java program using threads. Each "walker" has a thread and each of the four threads is to continue running until Phase 1 is complete. The winner from Phase 1 continues onto Phase 2. Can use Math.random() method to generate random numbers.
Each state of the grid is maintained in a data structure shared among the four threads. Can use the keywords to synchronize the program: synchronized, wait, notify or notifyAll.
Initally display the grid, all cells should be painted one uniform color. Associate a color with each of the walkers.
If the cell has not been visited by any walker before, paint the cell with the color of the current visitor.
If the cell has been visited by one or two of the other walkers paint the cell a unique color.
If the cell has been visited by all three walkers then paint the cell a different color.
Cells retain their color until changed by the next visitor. As soon as the winner is determined from phase 1, display the winner.
Consider a 10 times 10 grid. Each square will be referred to as a cell. There are four entities ("walkers") that are to visit each of the cells of the grid in a random manner. When a walker visits a cell, it marks the cell; this indicates that it has visited the cell. There are two phases to this game: Each walker starts in a different corner of the grid. All walkers are to start at the same time, or as close to the same time as possible. After a walker has visited a cell, it should pause for a random interval of time (for example: 0 - 500 milliseconds) before visiting another cell. Which cell a walker visits next is determined based on where it is located and the selection of one of four random directions (up, down, left, right). The walker moves to the indicated adjacent cell if possible. The first walker to visit all cells is the winner. However, this phase is not through until all the other walkers have visited all of the cells. Note that this phase must not start until all walkers have visited all of the cells during phase 1. Then the grid must be reinitialized, and the winner from the first phase is to take another random walk on the grid. Each cell is painted with the color (see below) used by the winner
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
