Question

In: Computer Science

Write a do-while loop IN JAVA to build a Pokémon game Prompt the user for two...

Write a do-while loop IN JAVA to build a Pokémon game

Prompt the user for two names one for their Pokémon, and one for the computers Pokémon

Make two integer variables to hold the HP of each Pokémon, HP should be 20 for both Pokémon’s.

Use Math.random() to determine the amount of damage caused, and which move the computer will select. The range of damage that can be done is up to you.

Output to the user an attack menu with different moves

In a do-while loop, prompt the user to pick an attack for their Pokémon to perform.

Store the damage in a variable and deduct points off the computer’s Pokémon HP.

Be sure to print out the name of the attack and the amount of damage that was imposed. For example: “Pokemon 1 used kick, and did 3 damage”

Use a random number to randomly pick an attack for the computer.

Use a random number to deduct points off the user’s pokemon HP.

Make sure to print out the computer's moves and the damage inflicted when it does an attack

Be sure you are checking the health points of each Pokémon at the end of each loop.

When the loop exits, indicate whether the user or computer won the game.

Solutions

Expert Solution

Dear student,

Here is the game coded using JAVA programming language:

CODE:


import java.io.*;
import java.util.*;

public class Main
{
        public static void main(String[] args) {
            Scanner sc=new Scanner(System.in);//used for input
            
            //For printing the attack menu
            System.out.println("Hello User. Here is the names of your attacks, their damage and code");
            System.out.println("KICK, DAMAGE:3, CODE: 0");
            System.out.println("PUNCH, DAMAGE:4, CODE: 1");
            System.out.println("BUMP, DAMAGE:2, CODE: 2");
            System.out.println("SPECIAL ATTACK, DAMAGE:8, CODE: 3");
            
            String poke1="";//for user's Pokemon
            String poke2="";//for computer's pokemon
            
            int hp1=20;
            int hp2=20;
            
            
            System.out.println("Enter the name of your Pokemon");
            poke1=sc.nextLine();
            System.out.println("Enter the name of Computer's Pokemon");
            poke2=sc.nextLine();
                
                
            do
            {
                //User's turn to attack
                System.out.println("Pick an attack to perform and enter its code");
                int attack=sc.nextInt();
                
                if(attack==0)
                {
                    System.out.println(poke1+" used KICK, and did 3 damage");
                    hp2=hp2-3;  //HP of computer decreases
                    
                }
                else if(attack==1)
                {
                    System.out.println(poke1+" used PUNCH, and did 4 damage");
                    hp2=hp2-4;  //HP of computer decreases
                }
                else if(attack==2)
                {
                    System.out.println(poke1+" used BUMP, and did 2 damage");
                    hp2=hp2-2;  //HP of computer decreases
                }
                else if(attack==3)
                {
                    System.out.println(poke1+" used SPECIAL ATTACK, and did 8 damage");
                    hp2=hp2-8;  //HP of computer decreases
                }
                else
                {
                    System.out.println("Wrong code. Try again");
                    continue;
                }
                
                if(hp2<=0)
                break;//if user defeats computer first
                
                //Attack of user done. Now it is the turn of the computer Pokemon
                
                int attackC=(int)(Math.random() * ((3 - 0) + 1)) + 0; //random attack for computer between 0(inclusive) and 3(inclusive)
                int damC=(int)(Math.random() * ((8 - 3) + 1)) + 3; //random damage for computer between 3(inclusive) and 8(inclusive)
                
                if(attackC==0)
                {
                    System.out.println(poke2+" used KICK, and did "+damC+" damage");
                    hp1=hp1-damC;  //HP of user decreases
                    
                }
                else if(attackC==1)
                {
                    System.out.println(poke2+" used PUNCH, and did "+damC+" damage");
                    hp1=hp1-damC;  //HP of user decreases
                }
                else if(attackC==2)
                {
                    System.out.println(poke2+" used BUMP, and did "+damC+" damage");
                    hp1=hp1-damC;  //HP of user decreases
                }
                else if(attackC==3)
                {
                    System.out.println(poke2+" used SPECIAL ATTACK, and did "+damC+" damage");
                    hp1=hp1-damC;  //HP of user decreases
                }
                else
                {
                    System.out.println("No damage done by"+poke2);
                }
                
                if(hp1<=0)//if computer defeats user first
                {
                    break;
                }
                
            }while(true);
            
            if(hp1<=0)
            {
                System.out.println(poke2+" is the winner");
            }
            
            if(hp2<=0)
            {
                System.out.println(poke1+" is the winner");
            }
            
            
            
            
        }
}

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

SAMPLE OUTPUT:

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

I hope the given solution helps clear your doubt.

Don't forget to give it a thumbs up.

Regards


Related Solutions

Java program Use Do-while Write a do-wile loop that asks the user to enter two numbers....
Java program Use Do-while Write a do-wile loop that asks the user to enter two numbers. The numbers should be added and the sum displayed. The loop should ask the user whether he or she wishes to perform the operation again. If so, the loop should repeat, otherwise, it should terminate. Use Continue branching statement Write a program that reads an integer and display all the positive ODD numbers from 0 to n (integer entered by the user). Use CONTINUE...
​​​​​​​For java program. Write a while loop that will let the user enter a series of...
​​​​​​​For java program. Write a while loop that will let the user enter a series of integer values and compute the total values and number of values entered. An odd number will stop the loop. Display the number of iterations and the total of the values after the loop terminates. for Loop Write a for loop to display all numbers from 13 - 93 inclusive, ending in 3. Write a for loop to display a string entered by the user...
IN JAVA PROGRAMMING Write a complete Java program to do the following: a) Prompt the user...
IN JAVA PROGRAMMING Write a complete Java program to do the following: a) Prompt the user to enter the name of the month he/she was born in (example: September). b) Prompt the user to enter his/her weight in pounds (example: 145.75). c) Prompt the user to enter his/her height in feet (example: 6.5). d) Display (print) a line of message on the screen that reads as follows: You were born in the month of September and weigh 145.75 lbs. and...
Using a while loop. Write a JAVA program that asks a user for an integer between...
Using a while loop. Write a JAVA program that asks a user for an integer between 1 and 9. Using the user input, print out the Fibonaccci series that contains that number of terms. Sample output: How many terms would like printed out for the Fibonacci Sequence? 7 Fibonacci Series of 7 numbers: 0 1 1 2 3 5 8
in java Write a while loop to ask the user to type number of hours(double) they...
in java Write a while loop to ask the user to type number of hours(double) they work per day. Calculate and print the salary for each valid input. If number of hours is greater than or equal to 0 and less than 5, then:  salary = numberofhours * 5, loop continues, the user can type another number If number of hours is greater or equal to 5, and less than 10, then: salary = numberofours * 8, loop continues, the user...
Re-write following while loop into Java statements that use a Do-while loop. Your final code should...
Re-write following while loop into Java statements that use a Do-while loop. Your final code should result in the same output as the original code below. int total = 0; while(total<100) { System.out.println("you can still buy for"+(100-total)+"Dollars"); total=total+5; }
Java Program. Sentinel While Loop Lab Do the following: Prompts the user to enter a grade...
Java Program. Sentinel While Loop Lab Do the following: Prompts the user to enter a grade or a -1 to quit. IF the user entered a -1 THEN Display a message that the User is done entering grades ELSE Count each grade as it is entered. Compute a running total of the grades entered. END IF After the user enters the sentinel of -1, calculate the average of the grades entered. When computing the average, make sure that there is...
Write a Java program that uses a while loop to do the following: Repeatedly asks the...
Write a Java program that uses a while loop to do the following: Repeatedly asks the user to enter a number or -1 to exit the program. Keeps track of the smallest and largest numbers entered so far: You will need a variable for the smallest number and another variable for the largest number. Read the first number the user enters, and if it is not -1 set the smallest and largest variables to that number. Use a loop to...
Print greeting Prompt the user for starting odometer reading Use a while loop to validate that...
Print greeting Prompt the user for starting odometer reading Use a while loop to validate that starting odometer reading is a positive number. Initialize variables: last odometer reading, current odometer reading, leg number, total fuel, moreInput While moreInput == ‘y’ Prompt the user for new odometer reading and fuel consumed If fuel is positive and new odometer reading > last odometer reading: Calculate MPG for this leg using mpg = (new odometer – last odometer) / fuel Print MPG for...
*JAVA PROGRAM* Write a do loop that prompts a user to enter an integer between 1...
*JAVA PROGRAM* Write a do loop that prompts a user to enter an integer between 1 and 100 inclusive. The user will be repeatedly prompted until they input a valid integer.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT