-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Dragon defense game utilizing inheritance
- Loading branch information
1 parent
a08b67f
commit 1241bab
Showing
12 changed files
with
389 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<module type="JAVA_MODULE" version="4"> | ||
<component name="NewModuleRootManager" inherit-compiler-output="true"> | ||
<exclude-output /> | ||
<content url="file://$MODULE_DIR$"> | ||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" /> | ||
</content> | ||
<orderEntry type="inheritedJdk" /> | ||
<orderEntry type="sourceFolder" forTests="false" /> | ||
</component> | ||
</module> |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
import java.util.Scanner; | ||
|
||
/** | ||
* Static functions used to check console input for validity. | ||
* | ||
* Use: Place CheckInput class in the same project folder as your code. | ||
* Call CheckInput functions from your code using "CheckInput." | ||
* | ||
* Example: int num = CheckInput.getInt(); | ||
* | ||
*/ | ||
public class CheckInput { | ||
|
||
/** | ||
* Checks if the inputted value is an integer. | ||
* @return the valid input. | ||
*/ | ||
public static int getInt() { | ||
Scanner in = new Scanner( System.in ); | ||
int input = 0; | ||
boolean valid = false; | ||
while( !valid ) { | ||
if( in.hasNextInt() ) { | ||
input = in.nextInt(); | ||
valid = true; | ||
} else { | ||
in.next(); //clear invalid string | ||
System.out.println( "Invalid Input." ); | ||
} | ||
} | ||
return input; | ||
} | ||
|
||
/** | ||
* Checks if the inputted value is an integer and | ||
* within the specified range (ex: 1-10) | ||
* @param low lower bound of the range. | ||
* @param high upper bound of the range. | ||
* @return the valid input. | ||
*/ | ||
public static int getIntRange( int low, int high ) { | ||
Scanner in = new Scanner( System.in ); | ||
int input = 0; | ||
boolean valid = false; | ||
while( !valid ) { | ||
if( in.hasNextInt() ) { | ||
input = in.nextInt(); | ||
if( input <= high && input >= low ) { | ||
valid = true; | ||
} else { | ||
System.out.println( "Invalid Range." ); | ||
} | ||
} else { | ||
in.next(); //clear invalid string | ||
System.out.println( "Invalid Input." ); | ||
} | ||
} | ||
return input; | ||
} | ||
/** | ||
* Checks if the inputted value is a non-negative integer. | ||
* @return the valid input. | ||
*/ | ||
public static int getPositiveInt( ) { | ||
Scanner in = new Scanner( System.in ); | ||
int input = 0; | ||
boolean valid = false; | ||
while( !valid ) { | ||
if( in.hasNextInt() ) { | ||
input = in.nextInt(); | ||
if( input >= 0 ) { | ||
valid = true; | ||
} else { | ||
System.out.println( "Invalid Range." ); | ||
} | ||
} else { | ||
in.next(); //clear invalid string | ||
System.out.println( "Invalid Input." ); | ||
} | ||
} | ||
return input; | ||
} | ||
|
||
/** | ||
* Checks if the inputted value is a double. | ||
* @return the valid input. | ||
*/ | ||
public static double getDouble() { | ||
Scanner in = new Scanner( System.in ); | ||
double input = 0; | ||
boolean valid = false; | ||
while( !valid ) { | ||
if( in.hasNextDouble() ) { | ||
input = in.nextDouble(); | ||
valid = true; | ||
} else { | ||
in.next(); //clear invalid string | ||
System.out.println( "Invalid Input." ); | ||
} | ||
} | ||
return input; | ||
} | ||
|
||
/** | ||
* Takes in a string from the user. | ||
* @return the inputted String. | ||
*/ | ||
public static String getString() { | ||
Scanner in = new Scanner( System.in ); | ||
String input = in.nextLine(); | ||
return input; | ||
} | ||
|
||
/** | ||
* Takes in a yes/no from the user. | ||
* @return true if yes, false if no. | ||
*/ | ||
public static boolean getYesNo(){ | ||
boolean valid = false; | ||
while( !valid ) { | ||
String s = getString(); | ||
if( s.equalsIgnoreCase("yes") || s.equalsIgnoreCase("y") ) { | ||
return true; | ||
} else if( s.equalsIgnoreCase("no") || s.equalsIgnoreCase("n") ) { | ||
return false; | ||
} else { | ||
System.out.println( "Invalid Input." ); | ||
} | ||
} | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
public class Dragon | ||
{ | ||
private String name; | ||
private int hp; | ||
private int maxHp; | ||
|
||
public Dragon(String n, int mHp) | ||
{ | ||
name = n; | ||
maxHp = mHp; | ||
hp = mHp; | ||
} | ||
|
||
public String getName() | ||
{ | ||
return name; | ||
} | ||
public int getHp() | ||
{ | ||
return hp; | ||
} | ||
|
||
|
||
public int attack(){ | ||
int Min = 3; | ||
int Max = 7; | ||
int Damage = 0; | ||
Damage = Min + (int)(Math.random() * ((Max - Min) + 1)); | ||
return Damage; | ||
} | ||
|
||
public void takeDamage(int d) | ||
{ | ||
hp = hp - d; | ||
if (hp < 0){ | ||
hp = 0; | ||
} | ||
} | ||
|
||
public String toString() | ||
{ | ||
|
||
return name + " " + hp + "/" + maxHp; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
public class FireDragon extends Dragon{ | ||
private int fireShots = 3; | ||
|
||
public FireDragon(String n, int mHp) | ||
{ | ||
super(n, mHp); | ||
} | ||
|
||
public int fireShot(){ | ||
if (fireShots > 0){ | ||
int Min = 5; | ||
int Max = 9; | ||
int Damage = 0; | ||
Damage = Min + (int)(Math.random() * ((Max - Min) + 1)); | ||
fireShots -= 1; | ||
return Damage; | ||
}else{ | ||
return 0; | ||
} | ||
} | ||
|
||
@Override | ||
public String toString(){ | ||
return super.toString() + "\nFire Shots remaining: " + fireShots; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
public class FlyingDragon extends Dragon{ | ||
private int swoops = 5; | ||
|
||
public FlyingDragon(String n, int mHp){ | ||
super(n, mHp); | ||
} | ||
|
||
public int swoopAttack(){ | ||
if (swoops > 0){ | ||
int Min = 5; | ||
int Max = 10; | ||
int Damage = 0; | ||
Damage = Min + (int)(Math.random() * ((Max - Min) + 1)); | ||
swoops -= 1; | ||
return Damage; | ||
}else{ | ||
return 0; | ||
} | ||
} | ||
|
||
@Override | ||
public String toString(){ | ||
|
||
return super.toString() + "\nSwoops attacks remaining: " + swoops; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
import java.util.ArrayList; | ||
import java.util.Scanner; | ||
|
||
public class Main { | ||
public static void main(String[] args) { | ||
Scanner in = new Scanner(System.in); | ||
ArrayList<Dragon> dragons = new ArrayList<Dragon>(); | ||
dragons.add(new Dragon("Paarthurnax",10)); | ||
dragons.add(new FireDragon("Volvagia",15)); | ||
dragons.add(new FlyingDragon("Spyro",20)); | ||
int uHP = 50; | ||
System.out.print("Name? "); | ||
String name = in.nextLine(); | ||
System.out.println("Excellent, now fight for your life."); | ||
System.out.println("3 dragons stand before you."); | ||
boolean fight = true; | ||
while (fight) | ||
{ | ||
System.out.println(name+ " HP: "+uHP); | ||
for (Dragon d: dragons) | ||
{ | ||
if (d.getHp() != 0) | ||
{ | ||
int index = dragons.indexOf(d); | ||
System.out.print(index + 1 + ". " +" Attack "); | ||
System.out.println(d); | ||
|
||
} | ||
|
||
} | ||
int attack = CheckInput.getIntRange(1,3); | ||
boolean dead = true; | ||
while (dead) | ||
{ | ||
while (dragons.get(attack-1).getHp() == 0 ) | ||
{ | ||
System.out.println("Invalid input."); | ||
attack = CheckInput.getIntRange(1,3); | ||
} | ||
dead = false; | ||
} | ||
System.out.println("Choose your weapon."); | ||
System.out.print("1. Arrow (1 D12)\n"); | ||
System.out.print("2. Sword (2 D6)\n"); | ||
int weaponChoice = CheckInput.getIntRange(1,2); | ||
if (weaponChoice == 1) { | ||
int damage = 1 + (int) (Math.random() * ((12 - 1) + 1)); | ||
dragons.get(attack - 1).takeDamage(damage); | ||
System.out.println("You shoot the dragon with an arrow."); | ||
uHP = receiveDamage(dragons, uHP); | ||
|
||
} else if (weaponChoice == 2) | ||
{ | ||
int damage = 1 + (int) (Math.random() * ((6 - 1) + 1)); | ||
dragons.get(attack-1).takeDamage(damage); | ||
damage = 1 + (int) (Math.random() * ((6 - 1) + 1)); | ||
dragons.get(attack-1).takeDamage(damage); | ||
System.out.println("You slash at the dragon with your sword."); | ||
uHP = receiveDamage(dragons,uHP); | ||
|
||
|
||
} | ||
System.out.println(); | ||
if (isDead(dragons)) | ||
{ | ||
System.out.println("Congratulations! I didn't think you'd survive that."); | ||
System.out.println("You've defeated all three dragons."); | ||
fight = false; | ||
} | ||
if (uHP <= 0) | ||
{ | ||
uHP = 0; | ||
System.out.println("You collapse...and take your final breath."); | ||
System.out.println("*YOU'VE DIED*"); | ||
fight = false; | ||
} | ||
|
||
|
||
|
||
} | ||
} | ||
public static int receiveDamage(ArrayList<Dragon> dragons, int uHP) | ||
{ | ||
boolean unAttacked = true; | ||
while (unAttacked) | ||
{ | ||
int randomAttack = 1 + (int)(Math.random() * ((2 - 1) + 1)); | ||
int randomDrag = (int) (Math.random() * ((2) + 1)); | ||
if (dragons.get(randomDrag).getHp() != 0 && randomDrag == 0) { | ||
|
||
System.out.println("Paarthurnax slashes you with his sharp claws!"); | ||
uHP = uHP - dragons.get(0).attack(); | ||
unAttacked = false; | ||
return uHP; | ||
|
||
} else if (dragons.get(randomDrag).getHp()!= 0 && randomDrag ==1) | ||
{ | ||
if (randomAttack == 1) { | ||
System.out.println("Volvagia slashes you with it's sharp claws."); | ||
uHP = uHP - dragons.get(0).attack(); | ||
unAttacked = false; | ||
return uHP; | ||
} else if (randomAttack == 2) { | ||
System.out.println("You're burned by Volvagia's flames!"); | ||
Dragon d = dragons.get(1); | ||
if (d instanceof FireDragon) { | ||
int damage = ((FireDragon) d).fireShot(); | ||
uHP = uHP - damage; | ||
unAttacked = false; | ||
return uHP; | ||
} | ||
} | ||
|
||
} else if (dragons.get(randomDrag).getHp() != 0 && randomDrag == 2) | ||
{ | ||
if (randomAttack == 1) { | ||
System.out.println("Spyro slashes you with his sharp claws."); | ||
uHP = uHP - dragons.get(0).attack(); | ||
unAttacked = false; | ||
return uHP; | ||
} else if (randomAttack == 2) { | ||
System.out.println("Spyro swoops down and knocks you down!"); | ||
Dragon d = dragons.get(2); | ||
if (d instanceof FlyingDragon) { | ||
int damage = ((FlyingDragon) d).swoopAttack(); | ||
uHP = uHP - damage; | ||
unAttacked = false; | ||
return uHP; | ||
} | ||
} | ||
|
||
} else if (isDead(dragons)){ | ||
unAttacked = false; | ||
} | ||
|
||
} | ||
return uHP; | ||
} | ||
public static boolean isDead(ArrayList<Dragon> dragons){ | ||
if (dragons.get(0).getHp() == 0 && dragons.get(1).getHp() == 0 && dragons.get(2).getHp() == 0){ | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
} |