Below are the screenshots of code and a few snapshots of race simulated:
https://media.cheggcdn.com/coop/640/6408d87d-69be-41fb-aca9-b31804cc41b1/1594544593066_Screenshot1950.png
https://media.cheggcdn.com/coop/35b/35bcc142-8735-4851-9a1f-d9ab112d4159/1594544658555_Screenshot1951.png
Race:
https://media.cheggcdn.com/coop/35d/35dfe747-4336-4d90-a718-1914c5091dd5/1594544714150_Screenshot1952.png
....................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................
https://media.cheggcdn.com/coop/35d/35dad45e-97bc-46d1-9203-af9e013f790b/1594544765606_Screenshot1953.png
Below is the java code for Snail.java. The 4 functions are implemented as mentioned in the question.
Snail.java
import java.util.LinkedList;
import java.util.Queue;
public class Snail {
public static char icon = '@';
private int position;
private int raceLength = 50;/**for storing raceLength**/
private Queue<Integer> movePattern;
/**implementing snail constructor**/
public Snail(int[] pattern) {
position = 0;
movePattern = (Queue<Integer>) new LinkedList<Integer>();
for (int pt: pattern)
movePattern.add(pt);
}
/**implementing move method**/
public void move(){
int move=movePattern.peek();
movePattern.remove();
position = Math.min(position+move, raceLength);
movePattern.add(move);
}
/**implementing getPosition method**/
public int getPosition(){
return position;
}
/**implementing display method**/
public void display(){
int dashesBefore = position;
int dashesAfter = raceLength - position;
for(int i = 0 ; i < dashesBefore ; i++)
System.out.print("-");
System.out.print(icon);
for(int i = 0 ; i < dashesAfter ; i++)
System.out.print("-");
System.out.println();
}
}
Below is the SnailRace.java. It is used unmodified to test & simulate the above Snails class.
SnailRace.java
public class SnailRace {
private Snail[] participants;
public static int raceLength = 50;
public SnailRace() {
participants = new Snail[3];
participants[0] = new Snail(new int[]{1,0,1,2,1,0});
participants[1] = new Snail(new int[]{2,1,1,0});
participants[2] = new Snail(new int[]{0,0,3});
try{
startRace();
} catch(Exception e){
System.out.println("An error occurred");
}
}
public void startRace() throws InterruptedException{
boolean racing = true;
while(racing){
for(int i = 0; i < participants.length; i++){
participants[i].move();
}
for(int i = 0; i < participants.length; i++){
if(participants[i].getPosition() == raceLength){
racing = false;
}
}
display();
Thread.sleep(500);
}
}
public void display(){
for(int i = 0; i < participants.length; i++){
participants[i].display();
}
System.out.println("\n\n\n\n");
}
public static void main(String[] args) {
new SnailRace();
}
}