import java.util.*;
public class Main
{
public static void main(String[] args) {
Arena arena = new Arena();
arena.startGame();
}
}
import java.util.*;
public class Hero{
private String name;
private int health;
private int money;
private Weapon weapon;
public Hero(String name, int health, int money, Weapon wepaon){
this.name = name;
this.health = health;
this.money = money;
this.weapon = weapon;
}
public void setHealth(int health){
this.health = Math.max(0, health);
}
public void setMoney(int money){
this.money = Math.max(0, money);
}
public String getName(){
return name;
}
public int getHealth(){
return health;
}
public int getMoney(){
return money;
}
public Weapon getWeapon(){
return weapon;
}
public boolean isAlive(){
return health > 0;
}
public int attackVillain(Villain v){
Random rng = new Random();
int variance = rng.nextInt(5);
int dmg = Math.max(1, weapon.getDamage() + variance);
v.setHealth(v.getHealth() - dmg);
return dmg;
}
public void takeDamage(int dmg){
setHealth(health - Math.max(0, dmg));
}
public void upgradeWeapon(Shop shop){
shop.buyWeapon(this);
}
public void equipWeapon(Weapon newWeapon){
this.weapon = newWeapon;
}
@Override
public String toString(){
return name + " Health: " + health + " Money: " + money + " Weapon: " + weapon.getName() + " Damage: " + weapon.getDamage();
}
}
import java.util.*;
public class Villain{
private String name;
private int health;
private Weapon weapon;
private Random rng = new Random();
public Villain(String name, int health, Weapon weapon){
this.name = name;
this.health = health;
this.weapon = weapon;
}
public void setHealth(int health){
this.health = Math.max(0, health);
}
public boolean isAlive(){
return health > 0;
}
public int attackHero(Hero h){
int variance = rng.nextInt(5);
int dmg = Math.max(1, weapon.getDamage() + variance);
h.takeDamage(dmg);
return dmg;
}
public String getName(){
return name;
}
public int getHealth(){
return health;
}
public Weapon getWeapon(){
return weapon;
}
@Override
public String toString(){
return name + " Health: " + health + " Weapon: " + weapon.getName() + " Damage: " + weapon.getDamage();
}
}
import java.util.*;
public class Weapon{
private String name;
private int damage;
private int price;
public Weapon(){
this.name = "unknown";
this.damage = 0;
this.price = 0;
}
public Weapon(String name, int damage, int price){
this.name = name;
this.damage = damage;
this.price = price;
}
public int getDamage(){
return damage;
}
public int getPrice(){
return price;
}
public String getName(){
return name;
}
@Override
public String toString(){
return name + " Damage: " + damage + " Price: " + price;
}
}
import java.util.*;
public class Shop{
private Weapon addWeapon;
private ArrayList<Weapon> weapons;
public Shop(){
Weapon addWeapon = new Weapon();
addWeapon = new Weapon("Dagger", 5, 0);
weapons.add(addWeapon);
addWeapon = new Weapon("Shortsword", 10, 40);
weapons.add(addWeapon);
addWeapon = new Weapon("Longsword", 15, 80);
weapons.add(addWeapon);
addWeapon = new Weapon("Warhammer", 20, 120);
weapons.add(addWeapon);
addWeapon = new Weapon("Runeblade", 25, 160);
weapons.add(addWeapon);
}
public void showMenu(){
System.out.println("----Weapon Shop----");
for(int i=0; i<weapons.size(); i++){
Weapon w = weapons.get(i);
System.out.println((i+1) + ". " +w);
}
System.out.println("0. Exit Shop");
}
public void buyWeapon(Hero hero){
while(true){
showMenu();
System.out.println("Your money: " + hero.getMoney());
System.out.print("Choose a weapon number to purchase (or press 0 to exit): ");
Scanner sc = new Scanner(System.in);
int choice = sc.nextInt();
if(choice == 0){
return;
}
if(choice < 0 || choice > weapons.size()){
System.out.println("Invalid entry.");
continue;
}
Weapon picked = weapons.get(choice - 1);
if(picked.getPrice() > hero.getMoney()){
System.out.println("You do not have enough money.");
continue;
}
hero.setMoney(hero.getMoney() - picked.getPrice());
hero.equipWeapon(picked);
System.out.println("You have bought and equipped " + picked.getName());
return;
}
}
public Weapon findByName(String name){
for(Weapon w : weapons){
if(w.getName().equalsIgnoreCase(name)){
return w;
}
}
return null;
}
}
import java.util.*;
public class Arena{
private Shop shop = new Shop();
private Hero hero;
private ArrayList<Villain> villains;
public void startGame(){
while(true){
System.out.println("Welcome to the Fantasy Arena");
System.out.println("1. New Game");
System.out.println("2. Load Game");
System.out.println("0. Exit");
System.out.print("Choose: ");
Scanner sc = new Scanner(System.in);
int choice = sc.nextInt();
if(choice == 1){
newGame();
}
if(choice == 2){
loadGame();
}
if(choice == 0){
System.out.println("Goodbye");
return;
}
else{
System.out.println("Invalid choice.");
}
}
}
private void newGame(){
Scanner sc = new Scanner(System.in);
System.out.print("Enter your hero's name: ");
String name = sc.nextLine();
Weapon starter = shop.findByName("Dagger");
hero = new Hero(name, 100, 50, starter);
initiateVillains();
GameSave.save(hero);
gameLoop();
}
private void loadGame(){
Hero loaded = GameSave.load(shop);
if(loaded == null){
System.out.println("No saved game was found. Starting new game...");
newGame();
return;
}
hero = loaded;
initiateVillains();
gameLoop();
}
private void initiateVillains(){
villains = new ArrayList<>();
villains.add(new Villain("Troll Grunt", 50, new Weapon("Rock", 6,0)));
villains.add(new Villain("Troll Archer", 70, new Weapon("Bow", 9, 0)));
villains.add(new Villain("Troll Warrior", 90, new Weapon("Club", 12, 0)));
}
private void gameLoop(){
System.out.println("Welcome " + hero.getName() + " . Your journey begins!");
for(int i=0; i<villains.size(); i++){
Villain v = villains.get(i);
if(!hero.isAlive()){
break;
}
System.out.println("Get read to fight!");
System.out.println("Your hero" + hero);
System.out.println("You opponent" + v);
preBattleMenu();
System.out.println("----FIGHT----");
boolean won = fight(v);
if(!won){
System.out.println("You were defeated by " + v.getName() +" . Game Over.");
return;
}
int prize = 30 + new Random().nextInt(21);
hero.setMoney(hero.getMoney() + prize);
System.out.println("Congratulations! You defeated " + v.getName() + " and earned " + prize + " coins");
GameSave.save(hero);
}
if(hero.isAlive()){
System.out.println("Congratulations! You have defeated all the villains!");
}
else{
System.out.println("You were defeated. Game Over.");
}
}
private void preBattleMenu(){
while(true){
System.out.println("1. Visit Shop");
System.out.println("2. Continue to fight");
Scanner sc = new Scanner(System.in);
int choice = sc.nextInt();
if(choice == 1){
hero.uprgadeWeapon(shop);
}
if(choice == 2){
return;
}
else{
System.out.println("Invalid selection.");
}
}
}
private boolean fight(Villain v){
while(hero.isAlive() && v.isAlive()){
System.out.println(hero.getName() + "(Health: " + hero.getHealth() + ") vs " + v.getName() + "(Health: " + v.getHealth() + ")");
System.out.println("1. Attack");
System.out.println("2. Save and Quit");
System.out.print("Choose: ");
Scanner sc = new Scanner(System.in);
int choice = sc.nextInt();
if(choice == 2){
GameSave.save(hero);
System.out.println("Game Saved. Exiting...");
System.exit(0);
}
//Hero turn
int dealt = hero.attackVillain(v);
System.out.println("You attack with your " + hero.getWeapon().getName() + " for " + dealt + " damage!");
if(!v.isAlive()){
System.out.println(v.getName() + "has 0 health. You win!");
break;
}
//Villain turn
int taken = v.attackHero(hero);
System.out.println(v.getName() + " attacks you with their " + v.getWeapon().getName() + " for " + taken + " damage!");
if(!hero.isAlive()){
System.out.println("You have 0 health. You lose.");
break;
}
}
return hero.isAlive();
}
}
import java.util.*;
import java.io.*;
public class GameSave{
private static String SAVE_FILE = "HeroStats.txt";
public static void save(Hero hero){
try(PrintWriter out = new PrintWriter(new FileWriter(SAVE_FILE))){
System.out.println(hero.getName());
System.out.println(hero.getHealth());
System.out.println(hero.getMoney());
System.out.println(hero.getWeapon().getName());
System.out.println("Game saved to " + SAVE_FILE);
}
catch(IOException e){
e.printStackTrace();
}
}
public static Hero load(Shop shop){
File f = new File(SAVE_FILE);
if(!f.exists()){
System.out.println("No save file found.");
return null;
}
try(Scanner sc = new Scanner(f)){
String name = sc.nextLine();
int health = Integer.parseInt(sc.nextLine());
int money = Integer.parseInt(sc.nextLine());
String weaponName = sc.nextLine();
Weapon w = shop.findByName(weaponName);
if(w == null){
System.out.println("Saved weapon not found in shop. You are given a Dagger.");
w = shop.findByName("Dagger");
}
System.out.println("Loaded hero " + name + " with Health: " + health + " Money: " + money + "and Weapon: " + w.getName());
return new Hero(name, health, money, w);
}
catch(IOException e){
e.printStackTrace();
return null;
}
}
}