Car Racing

Scenario

Two cars are racing. A function carRace needs to be designed.

Both drivers start with a distance of 0.

In a loop, each driver's distance is updated by a random value between 1 and 20m.

While both drivers have not reached 200m, the race continues!

The first driver to reach 200m is the winner.

A coder has been messing with some ideas to code the solution but the lines are in the incorrect sequence. Reorder the code so that the function behaves as expected.

  • driver2 = driver2 + (int)(Math.random()*20)+1;
  • while(driver1<200 && driver2<200){
  • public static void carRace(){
  • if(driver1>driver2){
  • }else if(driver2>driver1){
  • System.out.println("It's a draw!");
  • int driver1 = 0;
  • }//end of function
  • int driver2 = 0;
  • System.out.println("Driver2 wins!");
  • driver1 = driver1 + (int)(Math.random()*20)+1;
  • System.out.println("Current distance, Driver 1: "+driver1+"; driver2 "+driver2);
  • }//end of loop
  • System.out.println("Driver1 wins!");
  • }else{
  • }

Task 1 - development

Create a new project called TheCarRace and design and test your solution. Does it work as expected?

Type your code into your IDE and run it. Fix any syntax errors that appear.

Task 2 - adaptation

Think about this game. Can you find a way to adapt it to make it more interesting?

This may involve adding code to the main function, the carRace function, or designing a new function which performs a specific task when it is called!

Be creative! What could possibly go wrong?!

Adaptations

Be creative. Try to imagine what you want to happen and then try to make it happen.

You may have to do some research.

You can use ChatGPT to get ideas - but don't rely on it for solutions!