Rabbit Racing With a Turtle [JAVA]
October 9, 2017 • ☕️ 4 min read • 🏷 computer, software
Translated by author into: English
I want to share the solution I have developed in the most of the departments related to computer science, in the process of learning programming lessons, at the homeworks or at the examinations. All explanations about the program are in the loop.
Program Guidelines
In this problem, you will re-create 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. A clock ticks once per second. With each tick of the clock, your application should adjust the position of the animals according to the rules in Fig. 7.34. Use variables to keep track of the positions of the animals (i.e., position numbers are 1–70). Start each animal at position 1 (the “starting gate”). If an animal slips left before square 1, move it back to square 1.
Generate the percentages in Fig. 7.34 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.Then, for each tick of the clock (i.e., each repetition of a loop), display 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 application should display OUCH!!! beginning at that position. All output positions other than the T, the H or the OUCH!!! (in case of a tie) should be blank. After each line is displayed, test for whether either animal has reached or passed square 70. If so, display the winner and terminate the simulation. If the tortoise wins, display TORTOISE WINS!!! YAY!!! If the hare wins, display Hare wins. Yuch. If both animals win on the same tick of the clock, you may want to favor the tortoise (the “underdog”), or you may want to display It’s a tie. If neither animal wins, perform the loop again to simulate the next tick of the clock. When you are ready to run your application, assemble a group of fans to watch the race. You’ll be amazed at how involved your audience gets!
- Probability of movement of the turtle:
o Quick Motion --- 50% --- 3 positions to the right
o Backward Slip ---% 20 --- 6 positions to the left
o Slow Motion --- 30% --- 1 position to the right - Possibilities of movement of Tavşa:
o In Sleep --- 20% --- No movement
o Large Motion --- 20% --- 9 positions to the right
o Large Slip --- 10% --- 12 positions to the left
o Small Motion --- 30% --- 1 position to the right
o Small Shift --- 20% --- 2 positions to the left
[JAVA] Program Codes
TortoiseHare.java
/*
* coded by hkucuk
* */
public class TortoiseHare {
public static void main(String [] args){
int clock = 0; // Geçen zaman
Move move = new Move();
// Let's say something for the start
System.out.print("\nLets Goooo\n----------------------------------------------------------------------\n\n");
// Continue the race until reaching the 70th
while(move.getTortoisePosition() != 70 && move.getHarePosition() != 70){
move.actionTortoise(); // Move the turtle =)
move.actionHare(); // Move the rabbit =)
move.echoPositions(); // Let's print the turtle and rabbit's position
clock++; // Increase the time spent in each step
}
System.out.print("\n----------------------------------------------------------------------\n");
if(move.getTortoisePosition() >= move.getHarePosition()){ // Turtle wins: D
System.out.print("\nTORTOISE WINS!!! YAY!!!\n");
}else{ // Rabbit wins: D
System.out.print("\nHare wins. Yuch\n");
}
// Let's say the last time
System.out.printf("Elapsed Time : %d\n\n", clock);
}
}
Move.java
import java.util.Random;
/*
* coded by hkucuk
* */
public class Move {
public int tortoisePosition = 1; // The position of the turtle
public int harePosition = 1; // Position of the rabbit
Random rand = new Random();
public void actionTortoise(){
int randomNumber = rand.nextInt(10) + 1;
if(randomNumber >= 1 && randomNumber <= 5) setTortoisePosition(3);
else if(randomNumber == 6 || randomNumber == 7) setTortoisePosition(0, 6);
else setTortoisePosition(1);
if(getTortoisePosition() < 1) restoreTortoisePosition(1); if(getTortoisePosition() > 70) restoreTortoisePosition(70);
}
public void setTortoisePosition(int positionRight){ this.tortoisePosition += positionRight; }
public void setTortoisePosition(int positionRight, int positionLeft){ this.tortoisePosition -= positionLeft; }
public void restoreTortoisePosition(int position){ this.tortoisePosition = position; }
public int getTortoisePosition(){ return this.tortoisePosition; }
public void actionHare(){
int randomNumber = rand.nextInt(10) + 1;
if(randomNumber == 3 || randomNumber == 4) setHarePosition(9);
else if(randomNumber == 5) setHarePosition(0, 12);
else if(randomNumber >= 6 && randomNumber <= 8) setHarePosition(1);
else if(randomNumber == 9 || randomNumber == 10) setHarePosition(0, 2);
if(getHarePosition() < 1) restoreHarePosition(1); if(getHarePosition() > 70) restoreHarePosition(70);
}
public void setHarePosition(int positionRight){ this.harePosition += positionRight; }
public void setHarePosition(int positionRight, int positionLeft){ this.harePosition -= positionLeft; }
public void restoreHarePosition(int position){ this.harePosition = position; }
public int getHarePosition(){ return this.harePosition; }
public void echoPositions(){
int counter;
// We look at each position from 1 to 70 as the positions will be between 1 and 70
for(counter = 1; counter <= 70; counter++){
// If the turtle and rabbit are in the same position we warn you
if(counter == getTortoisePosition() && counter == getHarePosition())
System.out.printf("OUCH!!!");
// If there is a tortoise in the current position, we print T on the screen
else if(counter == getTortoisePosition())
System.out.printf("T");
// If there is a rabbit in the current position, we print on the screen
else if(counter == getHarePosition())
System.out.printf("H");
else // If there is no one in the current position, we leave a space
System.out.printf(" ");
}
System.out.printf("\n");
}
}