need help with programming assignment

0 Aaron Hebert · June 20, 2014
After my protagonist misses the antagonist, i want to make it to where the antagonist automatically gets a turn to either hit or miss the protagonist and vice verse... 
import javax.swing.JOptionPane;


public class LordOfRingsProgOne {

public static void main(String[] args) {

JOptionPane.showMessageDialog(null, "One day in a far out land a small hobbit by the name of Frodo set" + "
"
+ "off on a wild adventure with the wize and powerful wizard Gandolf!" + "
"
+ "Gandolf sent Frodo off into the wild to fight off the evil wicked" + "
"
+ "Sauron! Sauron was an evil sorcerer who had a 6 pack, but Frodo" + "
"
+ "wasn't scared because suns out guns out!" + "

"
+ "Frodo knew the only way to beat the evil sorcerer was to throw the" + "
"
+ "power of the python ring into the deep pits of fire, from which Sauron" + "
"
+ "came from. In order to do this they would have to fight it out to" + "
"
+ "the death. Since Frodo was into martial arts a bit, he had a few" + "
"
+ "powerful attacks he could use on Sauron. They were slap, dragon punch" + "
"
+ "and high kick. Going from weakest to strongest attack. Not sure of" + "
"
+ "Sauron's attacks yet..." + "

"
+ "Click OK to begin the Battle!!!");

String levelOfDifficulty, attackChoice;
int easy = 1;
int medium = 2;
int hard = 3;
int prot = 0;
int anta = 0;



levelOfDifficulty = JOptionPane.showInputDialog(null, "Now that you are ready to begin battle, " + "

"
+ "Please enter the difficulty level, then press OK " + "

"
+ " 1 is the Easiest Level" + "

"
+ " 2 is the Medium Level" + "

"
+ " 3 is the Most Difficult Level" + "

"
+ " Click CANCEL, if you are too afraid to continue...");

int difficultyLevel = Integer.parseInt(levelOfDifficulty);


if (difficultyLevel == 1)
{
prot = 75;
anta = 40;
JOptionPane.showMessageDialog(null, "You have chosen the easiest level number 1... Whimp!" + "

"
+ "You have a life force of 75, and Sauron has a life force of 40." + "

"
+ "Click OK to choose your first strike. ");

}

else if (difficultyLevel == 2)

{
prot = 50;
anta = 50;
JOptionPane.showMessageDialog(null, "You have chosen the medium level number 2... Safe and Smart" + "

"
+ "You have a life force of 50, and Sauron has a life force of 50." + "

"
+ "Click OK to choose your first strike.");

}

else if (difficultyLevel == 3)
{
prot = 50;
anta = 75;
JOptionPane.showMessageDialog(null, "You have chosen the hardest level number 3... Tough Guy!" + "

"
+ "You have a life force of 50, and Sauron has a life force of 75." + "

"
+ "Click OK to choose your first strike.");
}
else
{
System.exit(0);
}


while ((anta > 0) && ( prot > 0))
{

int sting = 1;
int charge = 2;
int punch = 3;
int hit = 0;
int miss = 0;

attackChoice = JOptionPane.showInputDialog(null, "If you are ready to begin battle, " + "

"
+ "Please enter the type of attack you choose, then press OK " + "

"
+ " 1 is a Sting. " + "

"
+ " 2 is a Charge." + "

"
+ " 3 is a Punch. " + "

"
+ " Click CANCEL, if you are too afraid to continue...");

int typeOfAttack = Integer.parseInt(attackChoice);



if (typeOfAttack ==1)
{
typeOfAttack = (int)(Math.random()*3 + 1);
if (typeOfAttack <=1)
{
hit = 9;
prot = prot - hit;
}
}
else if (typeOfAttack == 2)

{
typeOfAttack = (int)(Math.random()*3 + 1);
if (typeOfAttack <=2)
{
hit = 7;
prot = prot - hit;
}
}
else if (typeOfAttack == 3)

{
typeOfAttack = (int)(Math.random()*3 + 1);
if (typeOfAttack <=3)
{
hit = 5;
prot = prot - hit;
}
}

if (typeOfAttack == 1)
{
int stingOne = (int) (Math.random() * 100);
if (stingOne < 70)
{
hit = 10;
anta = anta - hit;

JOptionPane.showMessageDialog(null, "You Stung Sauron! Awesome!!!" + "

"
+ "Click OK to choose your next strike.");
}


else
{
miss = 0;
anta = anta - miss;
prot = prot - 3;


JOptionPane.showMessageDialog(null, "You totally missed! Protect Yourself!" + "

"
+ "Click OK to choose your next strike.");
}
}

if (typeOfAttack == 2)
{
int chargeTwo = (int) (Math.random()* 100);
if (chargeTwo < 60)
{
hit = 7;
anta = anta - hit;


JOptionPane.showMessageDialog(null, "You charged Sauron and knocked Sauron down! Right on!!!" + "

"
+ "Click OK to choose your next strike.");
}


else
{
miss = 0;
anta = anta - miss;
prot = prot - 5;

JOptionPane.showMessageDialog(null, "You totally missed! What are you doing?" + "

"
+ "Protect Yourself!" + "

"
+ "Click OK to choose your next strike.");
}
}

if (typeOfAttack == 3)
{
int punchThree = (int) (Math.random()* 100);
if (punchThree < 50)
{
hit = 5;
anta = anta - hit;


JOptionPane.showMessageDialog(null, "You punched Sauron HARD and knocked Sauron down!!!" + "

"
+ "Click OK to choose your next strike.");

}


else
{
miss = 0;
anta = anta - miss;
prot = prot - 3;

JOptionPane.showMessageDialog(null, "You totally swung blindly!" + "

"
+ "LOOK OUT!" + "

"
+ "Click OK to choose your next strike.");
}
}

//attackChoice = (int)(Math.random()*3 + 1);
if ((anta > 0) && (prot > 0))
{


JOptionPane.showMessageDialog(null, "Sauron has " + anta + " Life Force Left!" + "

"
+ "You have " + prot + " Life Force Left!");
}

else if (anta <= 0)
{
JOptionPane.showMessageDialog(null, "Sauron has " + anta + " Life Force and he has died..." + "

"
+ "You have " + prot + " Life Force Left, and have defeated Sauron!!!" + "

"
+ "Thanks for Playing" + "

"
+ "Have a nice day! : - ) ");

}

else if (prot <=0)
{
JOptionPane.showMessageDialog(null, "Sauron has " + anta + " Life Force Left, and he has defeated you..." + "

"
+ "You have " + prot + " NO Life Force Left!" + "

"
+ "Thanks for Playing" + "

"
+ "Have a nice day! : - ) ");
}

else
{
System.exit(0);
}

}
}
}


  

Post a Reply

Replies

Oldest  Newest  Rating
-2 Adam Beck · June 21, 2014
First of all, you should have this code into separate classes or at least into different methods.

For example:


public static void main(String args[])
{
chooseALevel();
attack();

}

public static void chooseALevel()
{
/// code here
}

public static void attack()
{
// code here
}


Anyways, I got your code to work. So here is the part of your code that you need to work on:


		else
{
miss = 0;
anta = anta - miss;
prot = prot - 3;

JOptionPane.showMessageDialog(null, "You totally swung blindly!" +


"LOOK OUT!" +


"Click OK to choose your next strike.");
}


So this part of your code is when you miss. You  can do a Math.random() in this else-statement and have your antagonist have a 50% chance of hitting you or something. For example:


else{
// Do something like this:

JOptionPane.showMessageDialog(null, "You swung blindly!!" +
}

Random rand = new Random();

if(rand.nextInt(2) == 0)
{

JOptionPane.showMessageDialog(null, "The antagonist missed!" +
}

else
{
// Deduct a random amount of health, for example:

prot = prot - rand.nextInt(10)+5;

}





}



I hope this helps.
  • 1

Java / Android Development

107,018 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator