1. Engineering
  2. Computer Science
  3. java we will play a game of improved artillery do...

Question: java we will play a game of improved artillery do...

Question details

JAVA

We will play a game of Improved Artillery. (DO the code based on the following dotted line) In this game, you will have two tanks. Each tank will start a random distance apart.(Should be random) Each tank will take turns firing at the other until one has been eliminated. You will need to create classes for the tank, the game and the main. The tank class should have a method to fire and to take damage as well as a method to report the hit points left. It does not have to move. The game class should contain two instances of the tank class as well as any other variables needed to play the game. The game class will call the fire method from one tank and pass it on to the other tank as damage. You will also need a method in the game class to decide if one tank has been eliminated. You will need additional methods in the game class to play a round and to play the game.

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

package project;

public class main{

public static double getX(){

             Random rand = new Random();

             double x = (rand.nextDouble()*100)+100;

             return x;

       }

public static double getDistance(double angle, double speed)

       {

             double angleRadians = (3.14*angle)/180;

             double d = (speed*speed*Math.sin(2*angleRadians))/9.8;

             return d;

       }

       public static double getCompDistance()

       {

             Random rand = new Random();

             double theta = rand.nextDouble()*90;

            System.out.printf("\n Computer Angle : %.2f",theta);

             double speed = rand.nextDouble()*100;

            System.out.printf("\n Computer Speed : %.2f",speed);

             double compDist = getDistance(theta,speed);

             return compDist;

       }

       public static void main(String[] args) {

             int userWins=0, cWins=0, numGames=0;

             String choice;

             double angle, speed, X;

             double userDistance, compDistance;

             Scanner scan = new Scanner(System.in);

            while(true)

{ X = getX();

                   System.out.printf("\n The two artillery pieces are %.2f distance apart ",X);

                   numGames++;                    

                   while(true)

                    {

                          System.out.print("\n Enter the angle for the shell relative to the horizontal(in degrees) : ");

                          angle =scan.nextDouble();

                          System.out.print(" Enter the speed for the shell (in meters per second) : ");

                          speed = scan.nextDouble();

                          scan.nextLine();

                          userDistance = getDistance(angle, speed);

                          System.out.printf("\n Distance travelled by user's shell : %.2f",userDistance);

                          compDistance = getCompDistance();

                          System.out.printf("\n Distance travelled by computers's shell : %.2f",compDistance);

                          if(Math.abs(X-userDistance) <=50) // the shell should be within 50 meters of the artillery piece

                          {

userWins++;

                                 break;

                          }else if(Math.abs(X-compDistance) <=50)

                           {

                                 cWins++;

                                 break;

                          }

                    }

                   

                   System.out.print("\n Do you want to play another game (y/n) ? ");

                   choice = scan.nextLine();

                   if(choice.equalsIgnoreCase("n"))

                          break;

             }

            

            if(((userWins*100)/numGames) >=90)

             {

                    System.out.println(" Excellent!!! You won the game. Great Performance");

             }else if(((userWins*100)/numGames) >=75)

             {

                   System.out.println("Good Performance");

             }else

             {

                   System.out.println(" Very bad performance. ");

             }

            

            scan.close();

       }

}

Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution