This isn't homework its just what I wanted to do to start out programming in games and what not.
I created this text based RPG type game and it seems to work fairly well, I was in the middle of testing other things so it might have some holes I didn't patch up yet and its taken me around 2 weeks to write between college, girlfriend, and everything. Still not done tho
but what I wanna know or be shown is what I can do to better my code and/or other ways of coding that might work better than what I've got here...
I'll post the Main code because its really all the complexity that I've written the other classes I'll just link because they just have constructors, getters,and setters and so on.
Basically what the game is, is a menu and you press 1 through whatever and it either fights or goes into other menus(while loops) for the shop, inventory, and the inn. all seem to work pretty well and also it doesn't have that great of a story line...i was just trying to get the basics down...
import java.util.*;
import java.io.*;
import javax.swing.JOptionPane;
public class Main {
/**
* @param args
*/
private static Scanner key = new Scanner(System.in);
private static Random rand = new Random();
private static int monster;
private static String Owned[] ={"Worm","Rat","Weasle with Sword!","Skeleton","Ghoul","Goblin","Ghost","Mummy","Dark Soldier","Floating Sword","Poison Flower","Giant Deadly Basilisk","Hunter","Magician","Wizard","Nightstalker","SabreTooth","Samurai","Ninja","Warlord",
"Jokester","Ultros","DoomLord","DoomGuard","Zombie","BrainEater","Naga","Demon posessed Dog","VoidWalker","Large Ogre","Welpling","Fire DragonKin","DragonKid","Scrunt","Green Imp","FelHunter","Blood Elf","Battle Orc","Black Troll","CutPurse","Bandit","Tauren","T-rex",
"Raptor","SeaMan","Pirate Floor Scrubber","Pirate","Pirate Captain","Mutated Pirate Monster!","Crocodile","Pikachu!","Charizard: The fire dragon","Ho-oh","Mewtwo","Link","Zelda","Darkling","Marlboro","Death Adder","Ramuh","Ifrit","Shiva","Fenrir","Alexander","Cecil",
"Terra","Sabin","Locke","Edgar","Shadow","Kefka","Cloud","Sephiroth","Squall","Seifer","Zidane","Vivi","Tidus","Phil of the future","Dragon King of the Underworld"};
private static int[] exp = new int[100];
private static int expnum = 102;
private static int weapnum;
private static int win, gold,shopnumber, num;
private static String pause;
//items (parallel arrays)
private static ArrayList<item> Items = new ArrayList<item>();
private static ArrayList<item> Inventory = new ArrayList<item>();
private static String shop[] ={"Potion","ether","Elixer","stick","bat","antidote","eyedrop","rusty sword",
"remedy","long sword", "Tent","glowing magic sword","fire sword","scimitar","prince's rapier",
"Knights goliath royal buster blade","Dark Sabre","Omega Weapon","Blacksmith's legendary ancient war sword",
"Ultima Sword","Sword of beasts","Holy Light Sword","??????????"};
private static String descrip[]={"Heal life by ","Regenerate mana by ", "Heal life and mana by ","Level 1 wep",
"Level 2 wep","Heal Poison status"," Heal blind status","Level 3 wep","Heal all imperfections","Level 4 wep",
"Stay the night outside and Heal all","Level 5 wep","Level 6 wep","Level 7 wep","Level 8 wep","Level 9 wep",
"Level 10 wep","Level 11 wep","Level 12 wep","Level 13 wep","Level 14 wep","Level 15 wep","??????????" };
private static int amount[] ={100, 50, 500, 10, 20, 0, 0,30,0,40, 100, 50, 60,70,80,90,100,110,120,130,140,1500,150000};
private static int prices[]= {100,50,500,10,200,50,90,300,500,900,1000,5000,6000,7000,8000,9000,10000,11000,1
2500,13000,14000,150000,1};
private static boolean use_able[] = {true,true,true,false,false,true,true,false,true,false,true,false,false,false,fa
lse,false,false,false,false,false,false,false,true};
private static int shopnum[]= {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23};
private static int j =90;
//character
private static Player dude = new Player(null, 1, 107, 59, 5, 0, 1000000);
public static void main(String[] args) {
//create items
for(int i=0; i<shop.length;i++){
item newitem = new item(shop[i],descrip[i],amount[i],prices[i],use_able[i]);
Items.add(newitem);
}
/**
* all items
*
*
System.out.println("All items in game for now:");
Iterator<item> it = Items.iterator ();
while (it.hasNext ()) {
Object o = it.next ();
System.out.println(o);
}
**/
//exp
for(int i=0;i<100;i++){
exp[i] = expnum*i;
}
//String name = JOptionPane.showInputDialog("What's your name?");
System.out.println("Whats your name?");
String name =key.next();
System.out.println("Crazy Game of Death (Title Menu)\n if you want to load a game enter ::yes:: if you want to \n create a new game enter ::no::, else closes.");
String answer = key.next();
if(answer == "yes"){
}
else if(answer == "no");
//start
System.out.println("Welcome young adventurer, your quest to become a great warrior begins now! WAKE UP!");
System.out.println("You wake up and find everyone gone, and a crashing noise outside,");
dude.setName(name);
dude.SetMaxStats();
System.out.println("Hey " + dude.getName() + " lets get ready for some fighting! Now you need a weapon.. oh wait i got one for ya, use this...");
System.out.println("Received new weapon: / " + shop[3]);
Inventory.add(Items.get(3));
equip(Items.get(3));
do{
if(j==90){
story(dude);
if(j==1){
fight();
}
if(j==2){
shop();
}
if(j==3){
Inventory();
}
if(j==4){
Inn();
}
}
}while(!(j==0));
//end of code
}//end main
public static void story(Player dude){
if(dude.getLevel() < 2){
System.out.println(dude.toString());
System.out.println("\n You are a level "+ dude.getLevel() + " warrior. What would you like to do?");
System.out.println("1) Fight! \n2) Shop \n3) Inventory \n4) Inn");
j= key.nextInt();
}
if(dude.getLevel()>1 && dude.getLevel() <=9 || dude.getLevel()>10){
System.out.println(dude.toString());
System.out.println("Dude you're makin it in the village...keep it up!");
System.out.println("1) Fight! \n2) Shop \n3) Inventory \n4) Inn");
j= key.nextInt();
}
if(dude.getLevel()==10){
System.out.println(dude.toString());
System.out.println("You're a fierce warrior..but many battles await.." +
"" +
"\nYou go to the Inn to rest, but right before you get all cozy the Innkeeper comes upstairs with grave news" +
"\nInnkeeper:OH its HORRIBLE! A Giant Deadly Basilisk has breached the village doors! Please help us!!");
int nii = key.nextInt();
fightboss();
System.out.println("1) Fight! \n2) Shop \n3) Inventory \n4) Inn");
j= key.nextInt();
}
else if(dude.getLevel() == 9){
System.out.println("You suck..");
j= key.nextInt();
}
}
/**
*
*
*
* FIGHT
*
*
*
*/
public static void fight(){
int a =0;
int monlvl = rand.nextInt(10);
monster = rand.nextInt((dude.getLevel() +1)+ monlvl);
System.out.println("You've encountered a monster! ");
int monsterlife=monster*monster*3+10;
int monsterdmg=monster+(monster*monster);
int monsterexp=(monster)*3+13;
int monstergold=(monster*100)+10;
Monster mon = new Monster(Owned[monster],monster,monsterlife,monsterdmg,monsterexp,monstergold);
System.out.println("It's a Level "+mon.getLevel()+" " + mon.getName()+"!");
int count = 0;
do{
mon.toString();
System.out.println(dude.toString());
System.out.println("1)Attack \n2)Heal \n3)Run");
a = key.nextInt();
//Attack
if(a ==1){
int hit = rand.nextInt(20);
if(hit > 2){
int dmgint = rand.nextInt(10);
int dmg = (dude.getstr()) + dmgint;
System.out.println("You swing and strike " + mon.getName() + " for " + dmg + " damage!");
mon.hit(dmg);
if(mon.getlife()<= 0)
break;
}
else if(hit > 17){
int dmgint = rand.nextInt(10);
int dmg = dude.getstr() + dmgint;
dmg = dmg *2;
System.out.println("You swing and strike " + mon.getName() + " for " + dmg + " damage! \nCritical hit!!!");
mon.hit(dmg);
if(mon.getlife()<= 0)
break;
}
else if(hit <=2)
System.out.println("You Miss! and faceplant into the ground...loser...");
int monstermiss = rand.nextInt(10);
int gethit = mon.getdamg();
if(monstermiss > 5){
System.out.println(mon.getName() + " attacks you for " + gethit + "damage!");
int mondmgint = rand.nextInt(mon.getdamg());
dude.loseLife(mon.getdamg()+ mondmgint);
if(dude.getLife()<=0)
break;
}
else if(monstermiss <= 5){
System.out.println(mon.getName() + " swings at you with hatred and misses!");
}
}
if(a==2){
System.out.println("Bag: ");
int i=0;
Iterator<item> it = Inventory.iterator ();
while (it.hasNext ()) {
Object o = it.next ();
System.out.println(i+")"+o);
i++;
}
System.out.println("Type the number of the item to use");
int itemuse=key.nextInt();
use(itemuse);
}
if(a==3){
System.out.println("You gain "+count+" xp for your "+count+" attacks");
dude.gainexp(count);
if(dude.getexp()>= (exp[dude.getLevel()])){
levelgain();
mon.hit(mon.getlife());
break;
}
}//end for loop for fight
}while(true);
System.out.println(mon.toString());
//won the fight
if(mon.getlife()<=0){
System.out.println("Dude you won...congrats n' stuff! You gained " + mon.getexp() +"xp!");
dude.gainexp(mon.getexp());
//gain lvl
if(dude.getexp()>= ((exp[dude.getLevel()])+9) ){
levelgain();
}
else{
;
}
dude.obtaingold(mon.getgold());
System.out.println("You received " + mon.getgold() + " gold!!!\n");
int drop = rand.nextInt(10);
if(drop <3){
int dropnumber = key.nextInt(shop.length);
System.out.println(mon.getName()+" dropped a "+
shop[dropnumber]+ ". "+shop[dropnumber]+ " was added to your [bag].");
Inventory.add(Items.get(dropnumber));
}
else
j=90;
}
else if(dude.getLife() <= 0){
dude.setLife(0);
System.out.println("Your Life has depleted.. you are.. \nAnnihilated...");dude.setLife(1);
System.out.println("The townspeople have found you and healed your wounds, you now have " + dude.getLife() + " Life left ");
int nni = key.nextInt();
}
else if(a==3){
System.out.println("Sucessfully escaped!");
}
j=90;
}
/**
*
*
*
* BOSS FIGHT
*
*
*
*/
public static void fightboss(){
int a =0;
monster = 11;
System.out.println("You've encountered a BOSS! ");
int monsterlife=monster*dude.getLevel()*3+10+75;
int mondmg = rand.nextInt(50);
int monsterdmg=(dude.getLevel()+(dude.getLevel()*dude.getLevel())+mondmg);
int monsterexp=(dude.getLevel())*30+13;
int monstergold=(monster*100)+10;
Monster mon = new Monster(Owned[monster],monster,monsterlife,monsterdmg,monsterexp,monstergold);
System.out.println("It's a Level "+mon.getLevel()+" " + mon.getName()+"!");
int count = 0;
do{
mon.toString();
System.out.println(dude.toString());
System.out.println("1)Attack \n2)Heal \n3)item");
a = key.nextInt();
//Attack
if(a ==1){
int hit = rand.nextInt(20);
if(hit > 2){
int dmgint = rand.nextInt(10);
int dmg = (dude.getstr()) + dmgint;
System.out.println("You swing and strike " + mon.getName() + " for " + dmg + " damage!");
mon.hit(dmg);
if(mon.getlife()<= 0)
break;
}
else if(hit > 17){
int dmgint = rand.nextInt(10);
int dmg = dude.getstr() + dmgint;
dmg = dmg *2;
System.out.println("You swing and strike " + mon.getName() + " for " + dmg + " damage! \nCritical hit!!!");
mon.hit(dmg);
if(mon.getlife()<= 0)
break;
}
else if(hit <=2)
System.out.println("You Miss! and faceplant into the ground...loser...");
int monstermiss = rand.nextInt(10);
int gethit = mon.getdamg();
if(monstermiss > 4){
System.out.println(mon.getName() + " attacks you for " + gethit + "damage!");
int mondmgint = rand.nextInt(mon.getdamg());
dude.loseLife(mon.getdamg()+ mondmgint);
if(dude.getLife()<=0)
break;
}
else if(monstermiss <= 4){
System.out.println(mon.getName() + " swings at you with hatred and misses!");
}
}
if(a==3){
//use item
}
}while(true);
System.out.println(mon.toString());
//won the fight
if(mon.getlife()<=0){
System.out.println("Dude you won...congrats n' stuff! You gained " + mon.getexp() +"xp!");
dude.gainexp(mon.getexp()*2);
//gain lvl
while(dude.getexp()>= ((exp[dude.getLevel()] ))){
levelgain();
}
dude.obtaingold(mon.getgold());
System.out.println("You received " + mon.getgold() + " gold!!!\n");
int drop = rand.nextInt(10);
if(drop <3){
System.out.println(mon.getName()+" dropped a "+
shop[mon.getLevel()]+ ". "+shop[mon.getLevel()]+ " was added to your [bag].");
Inventory.add(Items.get(mon.getLevel()));
}
}
else if(dude.getLife() <= 0){
dude.setLife(0);
System.out.println("Your Life has depleted.. you are.. \nAnnihilated...");dude.setLife(1);
System.out.println("The townspeople have found you and healed your wounds, you now have " + dude.getLife() + " Life left ");
int nni = key.nextInt();
}
j=90;
}
/**
*
*
*
* SHOP
*
*
*
*/
public static void shop(){
System.out.println("1)buy \n2)Sell");
int buy = key.nextInt();
if(buy==1){
System.out.println("Welcome to the shop! We have quite a selection,\nchoose one, making you better feeling!\n Gold:" + dude.getgold() + "\n" +
"0) " + shop[0] +" $"+prices[0]+" \n1) " + shop[1] + " $"+prices[1]+"\n2) " + shop[2] + " $"+prices[2]+"\n3) " + shop[3] + " $"+prices[3]);
shopnumber= key.nextInt();
if(dude.getgold() < prices[shopnumber]){
System.out.println("You don't seem to have enough, come on back later buddy!");
}
else{
System.out.println("How many ya need Partner?");
int num = key.nextInt();
for(int i=0;i<num;i++){
dude.buyitem(prices[shopnumber]);
Inventory.add(Items.get(shopnumber));
}
System.out.println("Added "+num+" " + shop[shopnumber] + " to your (Bag)!");
}
j=90;
}
//sell
if(buy ==2){
while(true){
System.out.println("Enter the number of the item to sell");
System.out.println("Bag: ");
Iterator<item> it = Inventory.iterator ();
int i =0;
while (it.hasNext ()) {
Object o = it.next ();
System.out.println(i +")" +o);
i++;
}
int sell = key.nextInt();
if(sell > Inventory.size()){
System.out.println("dude you dont have that many items you douche ");
}
else if(sell <= Inventory.size()){
int sellvalue = ((-1)*prices[sell])/2;
dude.buyitem(sellvalue);
System.out.println("Sold "+ Inventory.get(sell).getname() + " for $" + sellvalue);
Inventory.remove(sell);
break;
}
}
j=90;
}
}
/**
*
*
*
* INVENTORY
*
*
*
*/
public static void Inventory(){
System.out.println("Bag: ");
int i=0;
Iterator<item> it = Inventory.iterator ();
while (it.hasNext ()) {
Object o = it.next ();
System.out.println(i+")"+o);
i++;
}
System.out.println("1)use 2)equip \n 3)Back");
int useitem = key.nextInt();
if(useitem ==1){
System.out.println("Type the number of the item to use");
int itemuse=key.nextInt();
use(itemuse);
}
else if(useitem ==2){
System.out.println("Which item will you equip?");
int itemequip = key.nextInt();
equip(Inventory.get(itemequip));
}
else
;
System.out.println();
System.out.println("enter 1 to cont.");
String iin= key.next();
j=90;
}
/**
*
*
*
* INN
*
*
*
*/
public static void Inn(){
int inngold = (dude.getexp()*3)+ 7;
System.out.println("Welcome to the Inn.. it's " + inngold + " gold for one night.");
System.out.println("Stay?\n y or n ?");
char stay = key.next().charAt(0);
if(stay == 'y' || stay == 'Y'){
--------------------
JAVA == AWESOMENESS :)