Question
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
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(" Computer Angle : %.2f",theta);
double speed = rand.nextDouble()*100;
System.out.printf(" 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(" The two artillery pieces are %.2f distance apart ",X);
numGames++;
while(true)
{
System.out.print(" 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(" Distance travelled by user's shell : %.2f",userDistance);
compDistance = getCompDistance();
System.out.printf(" 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(" 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();
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started