Archive Forums (lite version).  Gaming, Game Development, and Anime community!


PDA

View Full Version : BattleJ v3~


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". :/

Google
--

Travis
01-09-2008, 03:29 PM
I'm sure it says a little more than just "<identifier> expected", Neo. Could you dump your error log in here?

neozf
01-09-2008, 07:50 PM
That's all it says, it doesn't make an error log either, although it has this little arrow thing that tries to explain it abit more <identifier> expected
The symbol named in the error message was expected to appear at this point in the code. It was not there; instead, there was some other symbol. Try to think about why this symbol may be expected here

Travis
01-09-2008, 10:03 PM
That... doesn't make any sense, and it doesn't really sound like an error. Good luck with that.

Insane2757
01-09-2008, 10:33 PM
Did you try compiling each file by itself?

Also:"
User.showHealth();
User.showCartridge();

What does User refer to? It's not the name of a class or defined variable.

Travis
01-09-2008, 10:59 PM
Nah, Insane... that's the player.

Player User = new Player (40);

Insane2757
01-09-2008, 11:26 PM
Gotcha.

Well, I am looking through this code for a variable that does have a declared variable type, or in other words, isn't identified.

Travis
01-10-2008, 12:06 AM
Well anyway... that doesn't even include a line number. It doesn't specify a symbol. That hardly qualifies as an error message.

Insane2757
01-10-2008, 12:33 AM
Well anyway... that doesn't even include a line number. It doesn't specify a symbol. That hardly qualifies as an error message.

I agree. Any more info on this?

neozf
01-10-2008, 04:55 AM
Travis: BlueJ is annoying when it comes to error messages, mainly since it doesn't say much about them. I'll try compiling it with another compiler and seeing if that works, or if it gives a better message.

Edit: Except I don't really know how to compile with any other compiler... I'll upload a screenshot of it.

Walrii
01-10-2008, 09:18 AM
I don't know if this is the cause of the error, but since when have you been able to put arbitrary code in an interface / class definition?

What I mean is, you're declaring all these variables that belong to an interface (that's ok I guess) and then you're ALSO trying to execute some code inside the interface.

neozf
01-10-2008, 09:28 AM
Walrii: I tried moving it to the CoreEngine class, same error, then I tried moving it to the BattleEngine class, still same error :/

Insane2757
01-10-2008, 11:24 AM
Use JCreator LE (http://www.jcreator.com/download.htm) (Lite Edition, meaning it is free). It is pretty simple to install and looks and feels like Virtual C++ or something. It is the editor I have always used since high school and it keeps getting better!

neozf
01-10-2008, 02:01 PM
Yea, I originally used JCreator but I found it hard to use when using more than one file, also BlueJ makes this nice tree diagram thing which shows what uses what etc, but it sucks when it comes to error messages.

Although I hated JCreator because it automatically added a ) or a } when you type a ( or a {.

JCreator gave the same error but also included another one "= expected".

Odd... I moved the line of code that was causing the problem to the BattleEngine class and now it works... but a few hours ago it didn't...

It works in both JCreator and BlueJ now, athough if put in the CoreEngine class it doesn't work and gives the same error in both.

Update: Found something out, if I put the "itembag.addItem(Hp_Potion);" code inside the public void Battle() part it works, if I put it outside where I'm declaring a few other things it gives the error it's been giving before.

Travis
01-10-2008, 11:14 PM
Wait, what?! You were putting functional code in with variable declarations?! Neo, WTK? I'm going to have to take away your programming badge if you keep this up.

neozf
01-11-2008, 02:37 AM
Travis: And you didn't notice it when I did it before?
Although right now it's mainly for testing parts.

Travis
01-11-2008, 10:27 AM
tl;dr, Neo. Believe it or not, I rarely ever go over your source code. I do that enough at work. Besides, as I've mentioned before, I don't think it benefits you to just have me troubleshoot your programs. You need direction in technical design and concepts, not syntax errors.

Insane2757
01-11-2008, 12:08 PM
What is sad is that I haven't used Java for a project in so long I no longer recall what anyone is talking about in the last 3 posts.