370 Arrays Chapter 7 from the wall, eventually you will arrive at the exit of the maze. There may be a shorter path than the one you have taken, but you are guaranteed to get out of the maze if you follow the algorithm. Write recursive method mazeTraverse to walk through the maze. The method should receive as arguments a 12-by-12 character array representing the maze and the starting location of the maze. As mazeTraverse attempts to locate the exit from the maze, it should place the character X in each square in the path. The method should display the maze after each move so the user can watch as the maze is solved. 7.39 (Generating Mazes Randomly) Write a method mazeGenerator that takes as an argument a double-subscripted 12-by-12 character array and randomly produces a maze. The method should also provide the starting and ending locations of the maze. Try your method mazeTraverse from Exercise 7.38, using several randomly generated mazes. 7.40 (Mazes of Any Size) Generalize methods mazeTraverse and mazeGenerator of Exercise 7.38 and Exercise 7.39 to process mazes of any width and height. 7.41 (Simulation: The Tortoise and the Hare) In this problem, you will recreate one of the truly great moments in history, namely the classic race of the tortoise and the hare. You will use random number generation to develop a simulation of this memorable event. Our contenders begin the race at square 1 of 70 squares. Each square represents a possible position along the race course. The finish line is at square 70. The first contender to reach or pass square 70 is rewarded with a pail of fresh carrots and lettuce. The course weaves its way up the side of a slippery mountain, so occasionally the contenders lose ground. There is a clock that ticks once per second. With each tick of the clock, your applet should adjust the position of the animals according to the rules in Fig. 7.32 Use variables to keep track of the positions of the animals (i.e., position numbers are 1 70). Start each animal at position 1 (i.e., the starting gate ). If an animal slips left before square 1, move the animal back to square 1. Generate the percentages in the preceding table by producing a random integer, i, in the range 1 = i = 10. For the tortoise, perform a fast plod when 1 = i = 5, a slip when 6 = i = 7 or a slow plod when 8 = i = 10. Use a similar technique to move the hare. Begin the race by printing BANG !!!!! AND THEY’RE OFF !!!!! Then, for each tick of the clock (i.e., each repetition of a loop), print a 70-position line showing the letter T in the position of the tortoise and the letter H in the position of the hare. Occasionally, the contenders will land on the same square. In this case, the tortoise bites the hare, and your program should print OUCH!!! beginning at that position. All print positions other than the T, the H or the OUCH!!!(in case of a tie) should be blank. After each line is printed, test for whether either animal has reached or passed square 70. If so, print the winner and terminate the simulation. If the tortoise wins, print TORTOISE WINS!!! YAY!!! If the hare wins, print Hare wins. Yuch. If both animals win on the same tick of the clock, you may want to favor the turtle (the underdog ) or you may want to print It’satie. If neither animal wins, perform the loop again to simulate the next tick of the clock. When you are ready to run your program, assemble a group of fans to watch the race. You ll be amazed at how involved your audience gets! Later in the book, we introduce a number of Java capabilities, such as graphics, images, animation, sound and multithreading. As you study those features, you might enjoy enhancing your tortoise and hare contest simulation. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01
Note: In case you are looking for affordable and reliable webhost to host and run your j2ee application check Vision best web hosting services