Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a Java program named RaceTo100.java. In the game Race To 100, a board has

ID: 642168 • Letter: W

Question

Write a Java program named RaceTo100.java.

In the game Race To 100, a board has 100 consecutive squares. Beginning in the first square, two players alternate rolling two dice and advancing forward the number of spaces shown on the dice. The first player to reach the 100th square wins the game. A player who rolls a 7 must move backwards either seven spaces or to the first square. A player who rolls a 2 or a 12 does not move on that turn. If a player lands on a square occupied by the other player, the other player must go back to the first square.

Simulate the playing of this game by displaying the results of each dice roll.

Sample beginning of game:

Player 1 position: 1

Player 2 position: 1

Player 1 rolls a 5, move ahead 5

Player 1 position: 6

Player 2 position: 1

Player rolls a 9, move ahead 9

Player 1 position: 6

Player 2 position: 10

Player 1 rolls a 7, move ahead 0

Player 1 position: 6

Player 2 position: 10

etc

Player 1 position: 99

Player 2 position: 91

Player 2 rolls 11, move ahead 11

Player 1 position: 99

Player 2 position: 100

Player 2 wins

Explanation / Answer

By using the following logic u can get your desirable output

import java.util.ArrayList;  

import java.util.Date;  

import java.util.List;  

  

public class Worker implements Runnable   

{  

  public boolean running = false;  

    

  public Worker ()  

  {  

    Thread thread = new Thread(this);  

    thread.start();  

  }  

    

  public static void main (String[] args) throws InterruptedException  

  {  

    List<Worker> workers = new ArrayList<Worker>();  

      

    System.out.println("This is currently running on the main thread, " +  

        "the id is: " + Thread.currentThread().getId());  

  

    Date start = new Date();  

  

    // start 5 workers  

    for (int i=0; i<5; i++)  

    {  

      workers.add(new Worker());   

    }  

      

    // We must force the main thread to wait for all the workers  

    //  to finish their work before we check to see how long it  

    //  took to complete  

    for (Worker worker : workers)  

    {  

      while (worker.running)  

      {  

        Thread.sleep(100);  

      }  

    }  

      

    Date end = new Date();  

      

    long difference = end.getTime() - start.getTime();  

      

    System.out.println ("This whole process took: " + difference/1000 + " seconds.");  

  }  

    

  @Override  

  public void run()   

  {  

    this.running = true;  

    System.out.println("This is currently running on a separate thread, " +  

        "the id is: " + Thread.currentThread().getId());  

      

    try   

    {  

      // this will pause this spawned thread for 5 seconds  

      //  (5000 is the number of milliseconds to pause)  

      // Also, the Thread.sleep() method throws an InterruptedException  

      //  so we must "handle" this possible exception, that's why I've  

      //  wrapped the sleep() method with a try/catch block  

      Thread.sleep(5000);  

    }   

    catch (InterruptedException e)   

    {  

      // As user Bernd points out in the comments section below, you should  

      //  never swallow an InterruptedException.  

      Thread.currentThread().interrupt();  

    }  

    this.running = false;  

  }  

}  

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote