In: Computer Science
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.
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