import java.util.*; // for ArrayList and Random /* This program is an adventure game like game. It is text based. The game uses a Fighter class which has the ability to fight other * Fighters. Each Fighter has a weapon, one or two pieces of armor and treasure. The user's Fighter fights each other Fighter one * at a time until either the user quits, defeats all of the Fighters, or dies. Upon successfully killing a Fighter, the user's * Fighter takes the dead Fighter's weapon (if better than the user's current weapon), pieces of armor (if better than the user's current * armor) and treasure. Between fights, the user can rest and drink any potions or use any scrolls that the user has, or quit. */ public class AdventureGame { public static void main(String[] args) { Random g=new Random(); // shared among other Fighters Scanner in=new Scanner(System.in); // Scanner used for menu input boolean rested; // has user rested this turn already? ArrayList temp=new ArrayList<>(); // used to temporarily create the initial Treasure for each Fighter temp.add("potion"); temp.add("potion"); temp.add("scroll"); // user's initial Treasure // create user Fighter Fighter user=new Fighter("User", new Weapon("Sword", 2, 1, 8), new Armor("Leather armor", 10), new Treasure(temp), 50, g); // user will fight up to 8 Fighters Fighter[] them=new Fighter[8]; // create them[0]-them[5] and them[7], them[6] created later temp=new ArrayList<>(); temp.add("gold"); temp.add("potion"); them[0]=new Fighter("Orc", new Weapon("Dagger", 3, 1, 4), new Armor("Chain mail", 12), new Treasure(temp), 22, g); temp=new ArrayList<>(); temp.add("potion"); them[1]=new Fighter("Nest of snakes", new Weapon("Venom", 1, 4, 20), new Armor("No armor", 0), new Treasure(temp), 12, g); temp=new ArrayList<>(); temp.add("gold");temp.add("silver");temp.add("potion");temp.add("potion"); them[2]=new Fighter("Troll", new Weapon("Hands", 4, 1, 8), new Armor("Hide", 14), new Treasure(temp), 25, g); temp=new ArrayList<>(); temp.add("silver");temp.add("potion"); them[3]=new Fighter("Beserker", new Weapon("Sword", 4, 1, 4), new Armor("Skin", 3), new Armor("Shield", 2), new Treasure(temp), 28, g); temp=new ArrayList<>(); temp.add("potion");temp.add("potion");temp.add("scroll"); them[4]=new Fighter("Ninja", new Weapon("Super sword", 4, 2, 8), new Armor("Chain mail", 14), new Treasure(temp), 40, g); temp=new ArrayList<>(); temp.add("gold");temp.add("silver");temp.add("silver");temp.add("silver");temp.add("scroll"); them[5]=new Fighter("Dragon", new Weapon("Claws", 2, 11, 20), new Armor("Skin", 14), new Treasure(temp), 25, g); // the doppleganger will be filled in later temp=new ArrayList<>(); temp.add("gold");temp.add("gold");temp.add("gold"); them[7]=new Fighter("Wizard", new Weapon("Lightning bolts", 1, 21, 40), new Armor("Magic aura", 15), new Treasure(temp), 150, g); // ready for first fight against them[0], set choice to be 5 to start the fight int n=0, choice=5; // n is which Fighter you are about to face, choice is the user's choice in a menu listed between fights while(user.isAlive()&&n<8&&choice!=4) // continue while user is still alive, has not won the game and has not quit { if(n==6) // create them[6], the doppleganger them[6]=new Fighter("Doppleganger", user.getWeapon(), user.getArmor1(), user.getArmor2(), user.getTreasure(), user.getHitPoints(), g); user.fight(them[n]); // user fights nth Fighter if(user.isAlive()) // if user wins { user.takeWeapon(them[n].getWeapon()); // test to see if opponent's weapon, armor1 or armor2 are takeable and better user.takeArmor(them[n].getArmor1()); // than user's if(them[n].getArmor2()!=null) user.takeArmor(them[n].getArmor2()); user.getTreasure().addTreasure(them[n].getTreasure().getTreasure()); // obtain opponent's treasure System.out.println("Your status: " + user); // output user as it now is rested=false; // user is allowed to rest once between fights do{ // allow user to rest, use scrolls, potions or quit as desired choice=menu(in); if(choice==1&&!rested) // can only rest once { if(!user.rest()) // is rest interrupted? { System.out.println("User's rest is interrupted, get ready to fight!"); choice=5; // force user to fight when interrupted } rested=true; // rested once, cannot rest again this turn } else if(choice==1) System.out.println("You cannot rest again during this turn"); else if(choice==2) user.usePotion(); else if(choice==3) user.useScroll(); }while(choice!=4&&choice!=5&&user.isAlive()); if(choice!=4) n++; } } // output result of game if(n<8&&user.isAlive()) System.out.println("Running away, eh? You successfully defeated " + n + " fighters with " + user.getTreasure()); else if(n==8) System.out.println("Congratulations, you win!!! You end the game with " + user.getTreasure()); else System.out.println("I'm sorry, you died after defeating only " + n + " fighters, with " + user.getTreasure()); } // method to obtain user input public static int menu(Scanner s) { int temp; do { System.out.println("What do you want to do next? \n1. Rest (one time only)\n2. Drink a potion\n3. Use a scroll\n4. Quit\n5. Fight the next fighter"); temp=s.nextInt(); }while(temp<1||temp>5); return temp; } }