Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created January 9, 2023 13:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codecademydev/45f0356fd0d63ef5419f1c231ee77158 to your computer and use it in GitHub Desktop.
Save codecademydev/45f0356fd0d63ef5419f1c231ee77158 to your computer and use it in GitHub Desktop.
Codecademy export
����5N
& ' ( ) *+/
01@Y3456serialVersionUIDJ ConstantValuenameLjava/lang/String;
durabilityI defenseRating<init>(Ljava/lang/String;II)VCodeLineNumberTablereduceDurability(I)V StackMapTable repairArmourgetDefenseRating()ItoString()Ljava/lang/String;
SourceFile Armour.java 7   8 9:BootstrapMethods;< =>? @AB =CArmourjava/lang/Objectjava/io/Serializable()Vjava/lang/SystemoutLjava/io/PrintStream;
DETYour  has been rendered useless. It cannot be repaired and must be sold for scraps.makeConcatWithConstants&(Ljava/lang/String;)Ljava/lang/String;java/io/PrintStreamprintln(Ljava/lang/String;)V, Defense Rating: 
'(Ljava/lang/String;I)Ljava/lang/String;F =J$java/lang/invoke/StringConcatFactoryLLookup InnerClasses�(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/invoke/CallSite;M%java/lang/invoke/MethodHandles$Lookupjava/lang/invoke/MethodHandles!  <*�*+�*�*�� W&*Y�d�*���*���*��
 %%G*Y�`�*�d� *d��
!# !)*��*��ok��'"#&*�*��
�,$%I
GKH,-.-2
import java.io.Serializable; //6.
public class Armour implements Serializable { //7.
/* Class Variables */
private static final long serialVersionUID = 1L; // 8.
/* Instance Variables */
private final String name;
private int durability;
private int defenseRating;
/* Constructors */
public Armour(String name, int durability, int defenseRating) {
this.name = name;
this.durability = durability;
this.defenseRating = defenseRating;
}
/* Instance Methods */
public void reduceDurability(int reduction) {
durability -= reduction;
if (durability <= 0) {
System.out.println("Your " + name + " has been rendered useless. It cannot be repaired and must be sold for scraps.");
defenseRating = 0;
}
}
public void repairArmour(int amount) {
durability += amount;
if (durability > 100) {
durability = 100;
}
}
/* Getters & Setters */
public int getDefenseRating() {
return (int) (defenseRating * (durability / 100.0)) ;
}
@Override
public String toString() {
return name + ", Defense Rating: " + defenseRating + "\n";
}
}
import java.io.Serializable; //9.
public class Helmet extends Armour implements Serializable { //9.
/* Class Variables */
private static final long serialVersionUID = 1L; // 9.
/* Constructors */
public Helmet(String name, int durability, int defenseRating) {
super(name, durability, defenseRating);
}
}
import java.util.Scanner;
import java.util.Objects;
import java.io.FileOutputStream; //12.
import java.io.ObjectOutputStream; //13.
import java.io.IOException; // 15.
public class MedievalGame {
/* Instance Variables */
private Player player; //10.
/* Main Method */
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
MedievalGame game = new MedievalGame();
game.player = game.start(console);
game.addDelay(500);
System.out.println("\nLet's take a quick look at you to make sure you're ready to head out the door.");
System.out.println(game.player);
game.addDelay(1000);
System.out.println("\nWell, you're off to a good start, let's get your game saved so we don't lose it.");
game.save();
game.addDelay(2000);
System.out.println("We just saved your game...");
System.out.println("Now we are going to try to load your character to make sure the save worked...");
game.addDelay(1000);
System.out.println("Deleting character...");
String charName = game.player.getName();
game.player = null;
game.addDelay(1500);
game.player = game.load(charName, console);
System.out.println("Loading character...");
game.addDelay(2000);
System.out.println("Now let's print out your character again to make sure everything loaded:");
game.addDelay(500);
System.out.println(game.player);
} // End of main
/* Instance Methods */
private Player start(Scanner console) {
// Add start functionality here
Player player;
Art.homeScreen(); //23.
System.out.println("Welcome to your latest adventure!");
System.out.println("Tell me traveler, have you been here before?");
System.out.print(" Enter 'y' to load a game, 'n' to create a new game: "); //24.
String answer = console.next().toLowerCase(); //25.
while (true) {
addDelay(500);
if (answer.equals("y")) {
System.out.print("\nAhh... I knew I remembered you, what was your name again? Let me see if I can find your backpack: ");
player = load(console.next(), console);
break;
} else if (answer.equals("n")) {
System.out.print("\nWell then, don't be shy, go ahead an tell me your name: ");
String possibleName = console.next();
while (true) {
System.out.println("Welcome " + possibleName + ", am I pronouncing that correctly? (Enter 'y' to confirm, 'n' to enter a new name");
String nameResponse = console.next().toLowerCase();
if (Objects.equals(nameResponse, "y")) break;
System.out.println("So sorry, can you spell it for me again?");
possibleName = console.next();
}
player = new Player(possibleName);
break;
} else {
System.out.print("Sorry adventurer, I only speak the common tongue, please enter 'y' to load a game or 'n' to start a new game: ");
answer = console.next().toLowerCase();
}
} // 26.
return new Player(player); //27.
} // End of start
private void save() {
String fileName = player.getName() + ".svr"; //11.
try { //12, 13, 14, 15
FileOutputStream userSaveFile = new FileOutputStream(fileName);
ObjectOutputStream playerSaver = new ObjectOutputStream(userSaveFile);
playerSaver.writeObject(player);
} catch (IOException e) {
System.out.println("There was an error saving your game, your file might not be available the next time you go to load a game.");
}
} // End of save
private Player load(String playerName, Scanner console) {
// Add load functionality here
Player loadedPlayer; // 17.
try {
FileInputStream userSaveFile = new FileInputStream(playerName + ".svr");
ObjectInputStream playerLoader = new ObjectInputStream(userSaveFile);
loadedPlayer = (Player) playerLoader.readObject();
} catch (IOException | ClassNotFoundException e) {
addDelay(1500);
System.out.println("\nThere was a problem loading your character, we've created a new player with the name you entered.");
System.out.println("If you're sure the spelling is correct, your character file may no longer exist, please reload the game if you'd like to try again.");
System.out.println("In the mean time, we'll create you a new character with the name: " + playerName);
addDelay(2000);
loadedPlayer = new Player(playerName);
}
} // End of load
// Adds a delay to the console so it seems like the computer is "thinking"
// or "responding" like a human, not instantly like a computer.
private void addDelay(int time) {
try {
Thread.sleep(time);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
import java.io.Serializable; //9.
public class Player implements Serializable { //9.
/* Class Variables */
private static final long serialVersionUID = 1L; // 9.
/* Instance Variables */
private final String name;
private double health;
private Weapon currentWeapon;
private Helmet helmet;
private Shirt shirt;
private Trouser trouser;
private Shoe shoe;
/* Constructors */
public Player(String name) {
this.name = name;
this.currentWeapon = new Weapon("Rusty Short Sword", 3);
this.health = 100;
}
/* Instance Methods */
public void takeDamage(double enemyAttack) {
double damage = enemyAttack - (getDefenseRating() / 5.0);
this.health -= damage;
if (this.health <= 0) {
System.out.println("You have succumbed to the enemy, better luck next time.");
System.exit(1);
} else {
System.out.println("Current Health: " + this.health);
}
}
public void heal(double healthToAdd) {
this.health += healthToAdd;
if (this.health > 100) {
this.health = 100;
}
}
/* Getters & Setters */
public String getName() {
return name;
}
public String getCurrentWeapon() {
return "Currently wielding: " +
currentWeapon.getName() +
". This weapon does " +
currentWeapon.getDamage() +
" damage.\n";
}
public void setCurrentWeapon(Weapon currentWeapon) {
this.currentWeapon = currentWeapon;
}
public double getHealth() {
return health;
}
public int getDefenseRating() {
return helmet.getDefenseRating() +
shirt.getDefenseRating() +
trouser.getDefenseRating() +
shoe.getDefenseRating();
}
public Helmet getHelmet() {
return helmet;
}
public void setHelmet(Helmet helmet) {
this.helmet = helmet;
}
public Shirt getShirt() {
return shirt;
}
public void setShirt(Shirt shirt) {
this.shirt = shirt;
}
public Trouser getTrouser() {
return trouser;
}
public void setTrouser(Trouser trouser) {
this.trouser = trouser;
}
public Shoe getShoe() {
return shoe;
}
public void setShoe(Shoe shoe) {
this.shoe = shoe;
}
@Override
public String toString() {
return "\nCurrent Player: \n" +
"Name: " + name + "\n" +
"Health: " + getHealth() + "\n" +
getCurrentWeapon() +
"Equipped Armour: " + "\n" +
" Helmet: " + helmet +
" Shirt: " + shirt +
" Trousers: " + trouser +
" Shoes: " + shoe;
}
}
import java.io.Serializable; //9.
public class Shirt extends Armour implements Serializable { //9.
/* Class Variables */
private static final long serialVersionUID = 1L; // 9.
/* Constructors */
public Shirt(String name, int durability, int defenseRating) {
super(name, durability, defenseRating);
}
}
import java.io.Serializable; //9.
public class Shoe extends Armour implements Serializable { //9.
/* Class Variables */
private static final long serialVersionUID = 1L; // 9.
/* Constructors */
public Shoe(String name, int durability, int defenseRating) {
super(name, durability, defenseRating);
}
}
import java.io.Serializable; //9.
public class Trouser extends Armour implements Serializable { //9.
/* Class Variables */
private static final long serialVersionUID = 1L; // 9.
/* Constructors */
public Trouser(String name, int durability, int defenseRating) {
super(name, durability, defenseRating);
}
}
import java.io.Serializable; //9.
public class Weapon implements Serializable { //9
/* Class Variables */
private static final long serialVersionUID = 1L; // 9.
/* Instance Variables */
private final String name;
private final int damage;
/* Constructors */
public Weapon(String name, int damage) {
this.name = name;
this.damage = damage;
}
/* Getters & Setters */
public String getName() {
return name;
}
public int getDamage() {
return damage;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment