neozf
01-09-2008, 02:22 PM
Up to the 3rd version now (at least I'm no longer just putting numbers at the end :p).
I'm currently rewriting it though, using the old BattleJ as a reference of what I would need or how some things would work, although there are two weird bugs that stops me from compiling and I can't seem to work them out.
I'll post the current source.
Player
public class Player
{
private int hp;
private int maxhp;
private int maxcartridge;
private int cartridge;
Player(int maxhp)
{
this.maxhp = maxhp;
this.hp = maxhp;
}
public void changeHp(int amount) {hp += amount;}
public int getHp() {return hp;}
public void setMaxCartridge(int maxcartridge) { this.maxcartridge = cartridge;}
public void showCartridge()
{
System.out.print("Cartridges remaining: ");
for (int cart = cartridge; cart > 0; cart--)
{
System.out.print("0 ");
}
for (int cart = maxcartridge-cartridge;cart > 0; cart--)
{
System.out.print("X ");
}
System.out.println();
}
public void showHealth()
{
System.out.println("Hp: "+hp+"/"+maxhp);
}
}Enemypublic class Enemy
{
private int hp;
private int maxhp;
private int power;
Enemy(int maxhp, int power)
{
this.maxhp = maxhp;
this.hp = maxhp;
this.power = power;
}
public void changeHp(int amount) {hp += amount;}
public int getHp() {return hp;}
public int getPower() {return power;}
}Itempublic class Item
{
private String name;
private char type;
public String getName() {return name;}
public char getType() {return type;}
public void setName(String name) {this.name = name;}
public void setType(char type) {this.type = type;}
}Weaponpublic class Weapon extends Item
{
private int power;
private int maxcartridge;
private int gemslot;
Weapon(String name, int power, int maxcartridge, int gemslot)
{
setName(name);
this.power = power;
this.maxcartridge = maxcartridge;
this.gemslot = gemslot;
}
public int getMaxCartirdge() {return maxcartridge;}
public int getPower() {return power;}
}Usable public class Usable extends Item
{
private int power;
private int amount;
Usable(String name, int power, char type)
{
setName(name);
this.power = power;
setType(type);
}
public int getPower() {return power;}
public int getAmount() {return amount;}
public void changeAmount(int amount){this.amount+=amount;}
}ItemBag import java.util.*;
public class ItemBag
{
LinkedList<Usable> itemlist = new LinkedList();
private boolean empty = true;
private int currentamount = 0;
private int select;
private Usable current = null;
ItemBag(){}
public void addItem(Usable item)
{
itemlist.add(item);
currentamount++;
empty=false;
}
public String getItemName(int number)
{
current=itemlist.get(number);
return current.getName();
}
public int getItemAmount(int number)
{
current=itemlist.get(number);
return current.getAmount();
}
public void getItems()
{
for(int a=0;a!=itemlist.size();a++)
{
current=itemlist.get(a);
System.out.println((a+1)+". "+current.getName()+" X"+current.getAmount());
}
}
public boolean checkEmpty() {return empty;}
}Initialization
public interface Initialization
{
Player User = new Player (40);
Enemy Goblin = new Enemy (50,3);
Weapon Blade = new Weapon ("Blade",5,4,1);
Usable Hp_Potion = new Usable ("Hp Potion", 30, 'H');
ItemBag itembag = new ItemBag();
//itembag.addItem(Hp_Potion);
Enemy current_enemy = Goblin;
Weapon current_weapon = Blade;
User.setMaxCartridge(5);
}BattleEngine import java.util.Scanner;
public class BattleEngine extends CoreEngine
{
Scanner in = new Scanner(System.in);
String[] normal = {"Attack","Catridge System","Skill","Item"};
String[] cs = {"Reload",null,null};
int dmg;
final int NORMAL = 1;
final int CART = 2;
final int SKILL = 3;
final int ITEM = 4;
final int PLAYER = 1;
final int ENEMY = 2;
public void Battle()
{
int select;
boolean turnover = false;
while (true)
{
User.showHealth();
User.showCartridge();
System.out.println("What will you do?");
showOptions(NORMAL);
select=in.nextInt();
System.out.println();
switch(select)
{
case 1:
Strike(PLAYER);
turnover = true;
break;
case 2:
showOptions(CART);
break;
case 3:
showOptions(SKILL);
break;
case 4:
showOptions(ITEM);
break;
default:
System.exit(3);
}
if (current_enemy.getHp()<1)
{
System.out.println("You win!");
System.exit(3);
}
if (turnover)
{
Strike(ENEMY);
turnover=false;
}
if (User.getHp()<1)
{
System.out.println("You lose");
System.exit(3);
}
}
}
public void Strike(int player)
{
switch(player)
{
case 1:
dmg = (int) Math.ceil(Math.random()*current_weapon.getPower()) ;
System.out.println("You deal "+dmg+" damage");
current_enemy.changeHp(-dmg);
System.out.println();
break;
case 2:
dmg = (int) Math.ceil(Math.random()*current_enemy.getPower());
System.out.println("Enemy deals "+dmg+" damage");
User.changeHp(-dmg);
System.out.println();
break;
}
}
public void showOptions(int option)
{
switch(option)
{
case 1:
for(int a=0;a!=normal.length;a++)
{
System.out.println((a+1)+". "+normal[a]);
}
break;
case 2:
for(int a=0;a!=cs.length;a++)
{
System.out.println((a+1)+". "+cs[a]);
}
break;
case 3:
break;
case 4:
itembag.showItems();
break;
}
}
}Main public class Main
{
public static void main()
{
BattleEngine Test = new BattleEngine();
Test.Battle();
}
}
The current bugs are both in the Initialization part,
//itembag.addItem(Hp_Potion);and
User.setMaxCartridge(5);it says "<identifier> expected". :/
I'm currently rewriting it though, using the old BattleJ as a reference of what I would need or how some things would work, although there are two weird bugs that stops me from compiling and I can't seem to work them out.
I'll post the current source.
Player
public class Player
{
private int hp;
private int maxhp;
private int maxcartridge;
private int cartridge;
Player(int maxhp)
{
this.maxhp = maxhp;
this.hp = maxhp;
}
public void changeHp(int amount) {hp += amount;}
public int getHp() {return hp;}
public void setMaxCartridge(int maxcartridge) { this.maxcartridge = cartridge;}
public void showCartridge()
{
System.out.print("Cartridges remaining: ");
for (int cart = cartridge; cart > 0; cart--)
{
System.out.print("0 ");
}
for (int cart = maxcartridge-cartridge;cart > 0; cart--)
{
System.out.print("X ");
}
System.out.println();
}
public void showHealth()
{
System.out.println("Hp: "+hp+"/"+maxhp);
}
}Enemypublic class Enemy
{
private int hp;
private int maxhp;
private int power;
Enemy(int maxhp, int power)
{
this.maxhp = maxhp;
this.hp = maxhp;
this.power = power;
}
public void changeHp(int amount) {hp += amount;}
public int getHp() {return hp;}
public int getPower() {return power;}
}Itempublic class Item
{
private String name;
private char type;
public String getName() {return name;}
public char getType() {return type;}
public void setName(String name) {this.name = name;}
public void setType(char type) {this.type = type;}
}Weaponpublic class Weapon extends Item
{
private int power;
private int maxcartridge;
private int gemslot;
Weapon(String name, int power, int maxcartridge, int gemslot)
{
setName(name);
this.power = power;
this.maxcartridge = maxcartridge;
this.gemslot = gemslot;
}
public int getMaxCartirdge() {return maxcartridge;}
public int getPower() {return power;}
}Usable public class Usable extends Item
{
private int power;
private int amount;
Usable(String name, int power, char type)
{
setName(name);
this.power = power;
setType(type);
}
public int getPower() {return power;}
public int getAmount() {return amount;}
public void changeAmount(int amount){this.amount+=amount;}
}ItemBag import java.util.*;
public class ItemBag
{
LinkedList<Usable> itemlist = new LinkedList();
private boolean empty = true;
private int currentamount = 0;
private int select;
private Usable current = null;
ItemBag(){}
public void addItem(Usable item)
{
itemlist.add(item);
currentamount++;
empty=false;
}
public String getItemName(int number)
{
current=itemlist.get(number);
return current.getName();
}
public int getItemAmount(int number)
{
current=itemlist.get(number);
return current.getAmount();
}
public void getItems()
{
for(int a=0;a!=itemlist.size();a++)
{
current=itemlist.get(a);
System.out.println((a+1)+". "+current.getName()+" X"+current.getAmount());
}
}
public boolean checkEmpty() {return empty;}
}Initialization
public interface Initialization
{
Player User = new Player (40);
Enemy Goblin = new Enemy (50,3);
Weapon Blade = new Weapon ("Blade",5,4,1);
Usable Hp_Potion = new Usable ("Hp Potion", 30, 'H');
ItemBag itembag = new ItemBag();
//itembag.addItem(Hp_Potion);
Enemy current_enemy = Goblin;
Weapon current_weapon = Blade;
User.setMaxCartridge(5);
}BattleEngine import java.util.Scanner;
public class BattleEngine extends CoreEngine
{
Scanner in = new Scanner(System.in);
String[] normal = {"Attack","Catridge System","Skill","Item"};
String[] cs = {"Reload",null,null};
int dmg;
final int NORMAL = 1;
final int CART = 2;
final int SKILL = 3;
final int ITEM = 4;
final int PLAYER = 1;
final int ENEMY = 2;
public void Battle()
{
int select;
boolean turnover = false;
while (true)
{
User.showHealth();
User.showCartridge();
System.out.println("What will you do?");
showOptions(NORMAL);
select=in.nextInt();
System.out.println();
switch(select)
{
case 1:
Strike(PLAYER);
turnover = true;
break;
case 2:
showOptions(CART);
break;
case 3:
showOptions(SKILL);
break;
case 4:
showOptions(ITEM);
break;
default:
System.exit(3);
}
if (current_enemy.getHp()<1)
{
System.out.println("You win!");
System.exit(3);
}
if (turnover)
{
Strike(ENEMY);
turnover=false;
}
if (User.getHp()<1)
{
System.out.println("You lose");
System.exit(3);
}
}
}
public void Strike(int player)
{
switch(player)
{
case 1:
dmg = (int) Math.ceil(Math.random()*current_weapon.getPower()) ;
System.out.println("You deal "+dmg+" damage");
current_enemy.changeHp(-dmg);
System.out.println();
break;
case 2:
dmg = (int) Math.ceil(Math.random()*current_enemy.getPower());
System.out.println("Enemy deals "+dmg+" damage");
User.changeHp(-dmg);
System.out.println();
break;
}
}
public void showOptions(int option)
{
switch(option)
{
case 1:
for(int a=0;a!=normal.length;a++)
{
System.out.println((a+1)+". "+normal[a]);
}
break;
case 2:
for(int a=0;a!=cs.length;a++)
{
System.out.println((a+1)+". "+cs[a]);
}
break;
case 3:
break;
case 4:
itembag.showItems();
break;
}
}
}Main public class Main
{
public static void main()
{
BattleEngine Test = new BattleEngine();
Test.Battle();
}
}
The current bugs are both in the Initialization part,
//itembag.addItem(Hp_Potion);and
User.setMaxCartridge(5);it says "<identifier> expected". :/
