Welcome to The Archive's Forums!
You have stumbled upon the best Gaming, Game Development, and Anime community ever, or at least we think so. We're pretty sure you will too after you see everything we have to offer. Look at our
list of current community projects, browse our
discussion forums, and when you're ready,
sign up! We're glad you're here, and hope you'll join our community!
View Full Version : New version of Battle!
neozf
10-03-2007, 06:50 PM
That's right, I've started another one, although this time in Java since that's the programming language I'm learning in my Computing class (Although I'm the only one in the class that can program :/)
Main changes is that I've removed the MP stuff and replaced it with Cartridges.
When you run out it needs to be reloaded (Although the weapon isn't really a gun).
The amount of remaining Cartridges is limited and it tells you how many you have left.
import java.io.*; // needed for BufferedReader, InputStreamReader, etc.
public class Battle
{
private static BufferedReader input =
new BufferedReader( new InputStreamReader( System.in)); //No idea how this works :/
public static void main(String[] args) throws IOException
{
//Creates the variables and declares a few of them.
int HP=40;
int EHP=50;
int DMG;
int Select;
int Bullet=4;
int MaxBullets=5;
int Spare=20;
int Cartridge;
boolean TurnMade=false;
while(1!=2)
{
System.out.println(HP+ " Hp remaining");
System.out.print("Cartridges remaining: ");
for (Cartridge=Bullet; Cartridge>0; Cartridge--)
{
System.out.print("0 ");
}
for (Cartridge=MaxBullets-Bullet;Cartridge>0;Cartridge--)
{
System.out.print("X ");
}
System.out.println("\nWhat will you do?\n1. Attack\n2. Cartridge System");
Select=Integer.parseInt(input.readLine());//Converts the text to an int in one go :D
switch(Select)
{
case 1://If the user selects 1 for attack.
DMG=(int) Math.ceil(Math.random()*6);
System.out.println("You deal "+DMG+" damage");
EHP=EHP-DMG;
TurnMade=true;
break;
case 2://If the user selects Cartridge System.
System.out.println("What do you do?\n1. Reload("+Spare+" Remaining)\n2. Flame Strike(1)\n3. Deus Shot(5)");
Select=Integer.parseInt(input.readLine());
switch(Select)
{
case 1:
Spare=Spare-(MaxBullets-Bullet);
Bullet=MaxBullets;
System.out.println("\t-RELOAD-");
TurnMade=true;
break;
case 2:
if(Bullet>0)
{
Bullet--;
DMG=(int) Math.ceil((Math.random()*5)+2);
System.out.println("\t-FLAME STRIKE-");
System.out.println("You deal "+DMG+ " damage");
EHP=EHP-DMG;
TurnMade=true;
break;
} else
{
System.out.println("Not enough Cartridges");
break;
}
case 3:
if(Bullet>4)
{
Bullet=Bullet-5;
DMG=(int) Math.ceil((Math.random()*10)+15);
System.out.println("\t-DEUS SHOT-");
System.out.println("You deal "+DMG+" damage");
EHP=EHP-DMG;
TurnMade=true;
break;
} else
{
System.out.println("Not enough Cartridges");
break;
}
}
}
if (EHP>0&&TurnMade==true)//Checks if the enemy is still alive or not.
{
DMG=(int) Math.ceil(Math.random()*5);
System.out.println("The enemy deals "+DMG+" damage");
HP=HP-DMG;
TurnMade=false;
}
if (EHP<1)//If the enemy has no life, he DIES.
{
System.out.println("Enemy died");
System.exit(0);
}
if (HP<1)//If the user has no life, they DIE.
{
System.out.println("You died");
System.exit(0);
}
}
}
}
There's the current code that I've managed to do in about an hour.
Any ideas of what I should add to it?
Neat... JBattle. :P
Seems like (when you've learnt how to in class), you should add a fancy graphical user interface :)
Walrii
10-05-2007, 06:55 PM
while (1!=2)
lol :) Other than that, it looks pretty decent. Since you're using Java, you could eventually figure out how to put it on the Internet as an applet.
neozf
10-05-2007, 06:57 PM
Yea, I originally had it as while (HP>0||EHP>0) then something happened so I changed it lol.
To quote Futurama: Don't worry Bender, there's no such thing as two! :P
It might be better to change it from 1!=2 to something like this:
bool done = false;
while (!done)
{
getuserinput();
doGameCalculations();
doRendering();
if (userwantstoquit())
done = true;
}That way the user can quit :)
neozf
10-18-2007, 11:25 PM
What I wanted to have was the options you could pick in an array, that way they could be changed depending on equipment etc. But my current set up is so if you input 1 you attack, or if you input 2 it shows the Cartridge System thing.
So I though of having something like "Array[1]()" so if Array[1] was Attack, it would be the same as "Attack()".
I guess I could always just have it see what the content of the array is but is it possible to do something like "Array[1]()"?
Also, how would I do functions in Java?
Something like;
Attack()
{
Damage=Randomnumber()*6;
EnemyHP=EnemyHP-Damage;
//Piece of code to take it to where the enemy attacks
}
Travis
10-19-2007, 02:05 AM
Okay, well... I have several things I need to ask or point out, but here's a few to think about.
1) Please clarify your first sentence. What do you mean by "that way they could be changed depending on equipment etc."?
2) No, you can't have an array of functions.
3) You also cannot define a function outside of a class. Everything in Java is a class, and all functions are member functions. I.e., you'll have to either stick your function in an existing class or create a new class for that function. *See Edit
4) There are ways to make classes that behave like functions, but if you can address point #1 better, that may not even be necessary.
Basically, I need to know what exactly this feature is... I'm still not quite sure what you want to do.
Also, since you're working in Java now, the one thing you really have to keep in mind is that everything is a class. You can't think in terms of functions or data that aren't conceptually part of some object. It may take some time, but as soon as you start thinking of your code and your design that way, Java will become a lot more understandable.
Edit: And looking back at your original source code... yeah, you need to learn how to make member functions. Sorry, I thought that might've been covered already. I'm sure someone can explain it, or I'll explain it later. Right now, I'm headed for bed.
neozf
10-19-2007, 04:08 AM
Well, lets say for example there are two different weapons, a Dagger and a Longsword.
For the Cartridge System (i.e the magic) when using the Dagger could be something like:
1. Reload
2. Double Strike
But if you were to equip the Long sword, you could have something like:
1. Reload
2. Flame Strike
Since the second one is different, instead of having loads of if commands to find out what weapon you are using, I'm planning on using arrays to store what you can do, i.e
CS[] = {"Reload","Flame Strike"};
Also, for 2), should I then have something like if (CS[a].equals("Reload"){ //Code for reloading the weapon
}
Travis
10-19-2007, 09:35 AM
No, Neo... you just need to learn what Object-Oriented Programming is. This happens to be one of its strong suits: creating objects that are conceptually the same "type" (e.g., in your example - weapons), but behave differently. We can discuss this more later when I'm around, but try to find some tutorials or something on Java and learn how to actually make a class with "member functions" and "encapsulated data." Jot down any issues that are confusing for you, and I'll try to explain them when we meet up again.
neozf
10-19-2007, 01:21 PM
Made some changes to the code.
I managed to some what create my second idea.
import java.util.Scanner;
public class Battle
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
//Creates the variables and declares a few of them.
int HP=40;
int MaxHP=40;
int EHP=50;
int DMG;
int NSelect; //For Normal Options
int CSelect; //For CS Options
int ISelect; //For Item Options
int Bullet=4;
int MaxBullets=5;
int Spare=20;
int Cartridge;
int HP_Pot=3;
int a=0;
String[] Normal = {"Attack","Cartridge System","Item"};
String[] CS = {"Reload","Flame Strike [1]","Deus Shot[5]"};
String[] Item = {"Hp Potion"};
boolean TurnMade=false;
while(1!=2)
{
System.out.println(HP+"/"+MaxHP+" Hp");
System.out.print("Cartridges remaining: ");
for (Cartridge=Bullet; Cartridge>0; Cartridge--)
{
System.out.print("0 ");
}
for (Cartridge=MaxBullets-Bullet;Cartridge>0;Cartridge--)
{
System.out.print("X ");
}
/////////////////////////////////////(START) FOR USER OPTIONS//////////////////////////////////////
System.out.println("\nWhat will you do?");
for (a=0;a!=Normal.length;a++)
{
System.out.print((a+1)+". "+Normal[a]+"\n");
}
NSelect=in.nextInt();
//// (START) FOR NORMAL ATTACKS ////
if (Normal[NSelect-1].equals("Attack"))
{
DMG=(int) Math.ceil(Math.random()*6);
System.out.println("You deal "+DMG+" damage");
EHP=EHP-DMG;
TurnMade=true;
}
//// (END) FOR NORMAL ATTACKS ////
//// (START) FOR CARTRIDGE SYSTEM ////
if (Normal[NSelect-1].equals("Cartridge System"))
{
System.out.println("What do you do?");
for (a=0;a!=CS.length;a++)
{
System.out.print((a+1)+". "+CS[a]);
if (CS[a].equals("Reload")){System.out.print(" ("+Spare+" Remaining)");}
System.out.print("\n");
}
CSelect=in.nextInt();
// (START) FOR RELOAD //
if (CS[CSelect-1].equals("Reload"))
{
if (MaxBullets-Bullet!=0)
{
Spare=Spare-(MaxBullets-Bullet);
Bullet=MaxBullets;
System.out.println("\t-RELOAD-");
TurnMade=true;
} else if (MaxBullets-Bullet==0){
System.out.println("\t-RELOAD FAILED-");
}
}
// (END) FOR RELOAD //
// (START) FOR FLAME STRIKE //
if (CS[CSelect-1].equals("Flame Strike [1]"))
{
if(Bullet>=1)
{
Bullet--;
DMG=(int) Math.ceil((Math.random()*5)+2);
System.out.println("\t-FLAME STRIKE-");
System.out.println("You deal "+DMG+ " damage");
EHP=EHP-DMG;
TurnMade=true;
} else
{
System.out.println("Not enough Cartridges");
}
}
// (END) FOR FLAME STRIKE //
// (START) FOR DEUS SHOT //
if(CS[CSelect-1].equals("Deus Shot[5]"))
{
if(Bullet>=5)
{
Bullet=Bullet-5;
DMG=(int) Math.ceil((Math.random()*10)+15);
System.out.println("\t-DEUS SHOT-");
System.out.println("You deal "+DMG+" damage");
EHP=EHP-DMG;
TurnMade=true;
} else
{
System.out.println("Not enough Cartridges");
}
}
// (END) FOR DEUS SHOT //
}
//// (END) FOR CARTRIDGE SYSTEM ////
//// (START) FOR ITEM USEAGE ////
if(Normal[NSelect-1].equals("Item"))
{
for (a=0;a!=Item.length;a++)
{
System.out.print((a+1)+". "+Item[a]);
if (Item[a].equals("Hp Potion")){System.out.print(" X"+HP_Pot);}
System.out.print("\n");
}
ISelect=in.nextInt();
// (START) FOR HEALTH POTION //
if (Item[ISelect-1].equals("Hp Potion"))
{
HP=HP+40;
if(HP>MaxHP){HP=MaxHP;}
HP_Pot--;
System.out.println("You use a Health Potion and heal 40 HP");
TurnMade=true;
}
// (END) FOR HEALTH POTION //
}
//// (END) FOR ITEM USEAGE ////
/////////////////////////////////////(END) FOR USER OPTIONS////////////////////////////////////////
if (EHP>0&&TurnMade==true)//Checks if the enemy is still alive or not.
{
DMG=(int) Math.ceil(Math.random()*5);
System.out.println("The enemy deals "+DMG+" damage");
HP=HP-DMG;
TurnMade=false;
}
if (EHP<1)//If the enemy has no life, he DIES.
{
System.out.println("Enemy died");
System.exit(0);
}
if (HP<1)//If the user has no life, they DIE.
{
System.out.println("You died");
System.exit(0);
}
}
}
}
Well, lets say for example there are two different weapons, a Dagger and a Longsword.
For the Cartridge System (i.e the magic) when using the Dagger could be something like:
1. Reload
2. Double Strike
But if you were to equip the Long sword, you could have something like:
1. Reload
2. Flame Strike
Since the second one is different, instead of having loads of if commands to find out what weapon you are using, I'm planning on using arrays to store what you can do, i.e
CS[] = {"Reload","Flame Strike"};
Also, for 2), should I then have something like if (CS[a].equals("Reload"){ //Code for reloading the weapon
}
Travis is right. One technique is to define what a weapon is and how it behaves through a class. Then create a child class of the weapon to indicate how a specific type of weapon behaves.
IE: A weapon has a generic function attack(target) that does nothing, whereas a sword (which is a type of weapon) has a function attack(target) that damages target for 50 hp. You write your combat code to handle the generic functions by interfacing with the common weapon class, while each specific weapon's code is executed.
Learn some basic OOP and it'll really help you out :)
Travis
10-20-2007, 01:23 AM
Well, Neo, I'm sure you could find some things online explaining classes, OOP, and inheritance (which is the particular feature Sim and I are mentioning)... but perhaps if I have some time I can draw up some tutorials or something to help you with some of the concepts. Go see what you can find for now, though... no guarantees on if nor when I'd be able to do so, and there's no reason you need to wait on me anyway. If you find something, let us know.
Plus Neo is taking a class on this right now, so sooner or later he'll have to learn it, right? ;)
neozf
10-20-2007, 05:28 PM
Plus Neo is taking a class on this right now, so sooner or later he'll have to learn it, right? ;)
Not really, I'm the only person in my class who has programmed before so everyone else is finding it hard. Plus, we only have one lesson a week for programming (the other lesson is more on how the computer works etc) and they've only started learning if statements after about 3-4 lessons.
Most of what I've learnt I taught myself using a book I got from the library.
Update: I found this site (http://java.sun.com/docs/books/tutorial/java/javaOO/classes.html) which I think is related to what you were suggesting I look up.
In that case you better learn OOP; it'll be a while before your class gets to it. The link you posted is, in fact, what we're talking about. Take a read through there, hopefully it will make sense to you.
Travis
10-21-2007, 03:22 PM
In that case you better learn OOP; it'll be a while before your class gets to it.
If ever. In my experience, classes geared towards those who've never before programmed never cover classes and such, even if the language is an OOP language.
This being Java, it'd be difficult to omit some sort of learning of classes, but I guess they could always resort to some gigantimous class like Neo's doing at the moment.
Regardless, I'll bet you a new server that Neo's class is never really taught what a class/object is and how they're used.
Walrii
10-22-2007, 11:27 PM
If ever. In my experience, classes geared towards those who've never before programmed never cover classes and such, even if the language is an OOP language.
...
Regardless, I'll bet you a new server that Neo's class is never really taught what a class/object is and how they're used.
And for good reason. OOP is a generally good thing, but you can't do crap with it unless you learn the basics which Neo already seems to know (having written different versions of Battle across ten million different programming languages, or something like that)
Neo, why are you even in that class :confused:? You obviously know how to use a computer and you'd be MUCH better off in a higher programming class (hopefully your school offers one)?
And for good reason. OOP is a generally good thing, but you can't do crap with it unless you learn the basics which Neo already seems to know (having written different versions of Battle across ten million different programming languages, or something like that)
Neo, why are you even in that class :confused:? You obviously know how to use a computer and you'd be MUCH better off in a higher programming class (hopefully your school offers one)?
I don't know... I'm still waiting for Battle in lolcode. :P
Neo seems ready to learn OOP. Get to learning it Neo, it'll be really helpful to your future programs (case example being the question you yourself asked to kick this thread off)
neozf
10-23-2007, 05:58 AM
Neo, why are you even in that class :confused:? You obviously know how to use a computer and you'd be MUCH better off in a higher programming class (hopefully your school offers one)?
There is no higher class :/
The only two computer related classes are "ICT" which I think was described as how we use technology and stuff, and "Computing" which seems to be a mix of Programming and how computers work. So I end up spending most of my time sitting there working on Battle or something else (Like a guessing game or a Decimal to Binary converter).
neozf
10-24-2007, 07:33 PM
I think I've somewhat worked it out, although I ended up guessing abit.
I added to the top class Damage {
public int Str;
public int DMG;
public Damage(int Strength)
{
Str = Strength;
}
public int Strike()
{
DMG=(int) Math.ceil(Math.random()*Str);
System.out.println("You deal "+DMG+" damage");
return DMG;
}
} put in where it delares the variables Damage Player = new Damage(6);and replaced //// (START) FOR NORMAL ATTACKS ////
if (Normal[NSelect-1].equals("Attack"))
{
DMG=(int) Math.ceil(Math.random()*6);
System.out.println("You deal "+DMG+" damage");
EHP=EHP-DMG;
TurnMade=true;
}
//// (END) FOR NORMAL ATTACKS ////with //// (START) FOR NORMAL ATTACKS ////
if (Normal[NSelect-1].equals("Attack"))
{
EHP=EHP-Player.Strike();
TurnMade=true;
}
//// (END) FOR NORMAL ATTACKS ////So far am I doing well?
Any suggestions on how I can clean up my code slightly or improve it?
Also, to write a basic version of Battle I would need to know, how to output text and variables, how to let users input stuff, random numbers, if statments, some kind of loop and declaring variables.
neozf
10-25-2007, 01:12 AM
Added abit more to the classes, there's now a class for Damage and for Status (Health, amount of cartridges left etc). Since I'm posting the current code I saw it would be better to make a new reply as it would just make the other post hard to read.
import java.util.Scanner;
class Damage {
public int Str;
public int DMG;
public Damage(int Strength)
{
Str = Strength;
}
public int Strike(boolean Player)
{
DMG=(int) Math.ceil(Math.random()*Str);
if(Player){System.out.println("You deal "+DMG+" damage");}
if(!Player){System.out.println("The enemy deals "+DMG+" damage");}
return DMG;
}
public int CartSys(String Name, int Power, int Bonus)
{
DMG=(int) Math.ceil((Math.random()*Power)+Bonus);
System.out.println("\t-"+Name.toUpperCase()+"-");
System.out.println("You deal "+DMG+" damage");
return DMG;
}
}
class Status {
public int HP;
public int MaxHP;
public int Bullet;
public int MaxBullets;
public Status(int Health, int MaxHealth, int Bullets, int MaximumBullets)
{
HP=Health;
MaxHP=MaxHealth;
Bullet=Bullets;
MaxBullets=MaximumBullets;
}
}
public class Battle
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
//Creates the variables and declares a few of them.
int NSelect; //For Normal Options
int CSelect; //For CS Options
int ISelect; //For Item Options
int Spare=20;
int Cartridge;
int HP_Pot=3;
int a=0;
String[] Normal = {"Attack","Cartridge System","Item"};
String[] CS = {"Reload","Flame Strike [1]","Deus Shot[5]"};
String[] Item = {"Hp Potion"};
boolean TurnMade=false;
Damage PlayerD = new Damage(6);
Damage EnemyD = new Damage(5);
Status Player = new Status(40,40,5,5);
Status Enemy = new Status(50,50,0,0);
while(1!=2)
{
System.out.println(Player.HP+"/"+Player.MaxHP+" Hp");
System.out.print("Cartridges remaining: ");
for (Cartridge=Player.Bullet; Cartridge>0; Cartridge--)
{
System.out.print("0 ");
}
for (Cartridge=Player.MaxBullets-Player.Bullet;Cartridge>0;Cartridge--)
{
System.out.print("X ");
}
/////////////////////////////////////(START) FOR USER OPTIONS//////////////////////////////////////
System.out.println("\nWhat will you do?");
for (a=0;a!=Normal.length;a++)
{
System.out.print((a+1)+". "+Normal[a]+"\n");
}
NSelect=in.nextInt();
//// (START) FOR NORMAL ATTACKS ////
if (Normal[NSelect-1].equals("Attack"))
{
Enemy.HP=Enemy.HP-PlayerD.Strike(true);
TurnMade=true;
}
//// (END) FOR NORMAL ATTACKS ////
//// (START) FOR CARTRIDGE SYSTEM ////
if (Normal[NSelect-1].equals("Cartridge System"))
{
System.out.println("What do you do?");
for (a=0;a!=CS.length;a++)
{
System.out.print((a+1)+". "+CS[a]);
if (CS[a].equals("Reload")){System.out.print(" ("+Spare+" Remaining)");}
System.out.print("\n");
}
CSelect=in.nextInt();
// (START) FOR RELOAD //
if (CS[CSelect-1].equals("Reload"))
{
if (Player.MaxBullets-Player.Bullet!=0)
{
Spare=Spare-(Player.MaxBullets-Player.Bullet);
Player.Bullet=Player.MaxBullets;
System.out.println("\t-RELOAD-");
TurnMade=true;
} else if (Player.MaxBullets-Player.Bullet==0){
System.out.println("\t-RELOAD FAILED-");
}
}
// (END) FOR RELOAD //
// (START) FOR FLAME STRIKE //
if (CS[CSelect-1].equals("Flame Strike [1]"))
{
if(Player.Bullet>=1)
{
Player.Bullet--;
Enemy.HP=Enemy.HP-PlayerD.CartSys("Flame Strike",5,2);
TurnMade=true;
} else
{
System.out.println("Not enough Cartridges");
}
}
// (END) FOR FLAME STRIKE //
// (START) FOR DEUS SHOT //
if(CS[CSelect-1].equals("Deus Shot[5]"))
{
if(Player.Bullet>=5)
{
Player.Bullet=Player.Bullet-5;
Enemy.HP=Enemy.HP-PlayerD.CartSys("Deus Shot",10,15);
TurnMade=true;
} else
{
System.out.println("Not enough Cartridges");
}
}
// (END) FOR DEUS SHOT //
}
//// (END) FOR CARTRIDGE SYSTEM ////
//// (START) FOR ITEM USEAGE ////
if(Normal[NSelect-1].equals("Item"))
{
for (a=0;a!=Item.length;a++)
{
System.out.print((a+1)+". "+Item[a]);
if (Item[a].equals("Hp Potion")){System.out.print(" X"+HP_Pot);}
System.out.print("\n");
}
ISelect=in.nextInt();
// (START) FOR HEALTH POTION //
if (Item[ISelect-1].equals("Hp Potion"))
{
Player.HP=Player.HP+40;
if(Player.HP>Player.MaxHP){Player.HP=Player.MaxHP;}
HP_Pot--;
System.out.println("You use a Health Potion and heal 40 HP");
TurnMade=true;
}
// (END) FOR HEALTH POTION //
}
//// (END) FOR ITEM USEAGE ////
/////////////////////////////////////(END) FOR USER OPTIONS////////////////////////////////////////
if (Enemy.HP>0&&TurnMade==true)//Checks if the enemy is still alive or not.
{
Player.HP=Player.HP-EnemyD.Strike(false);
TurnMade=false;
}
if (Enemy.HP<1)//If the enemy has no life, he DIES.
{
System.out.println("Enemy died");
System.exit(0);
}
if (Player.HP<1)//If the user has no life, they DIE.
{
System.out.println("You died");
System.exit(0);
}
}
}
}
So far so good :D
Travis
10-25-2007, 11:50 PM
I'll do a better critique later, but for starters, change everything that's not a function into "private" instead of "public" as a start.
neozf
10-28-2007, 05:11 PM
It started to freak out and break when I set the variables that were in the Status class to private.
Travis
10-29-2007, 01:24 AM
It started to freak out and break when I set the variables that were in the Status class to private.
Too bad. Make them private, and figure out a way to make your program not freak out. And in the meantime, look up "inheritance" and learn to use it. I'll give you a hint. Given your earlier example, start with a class called Weapon, and make child classes that inherit Weapon's traits, but are more specific. [Examples might include FireSword, WindScythe, or DoomHammer] Then, only ever refer to your child classes through their parent class. [If you have something like FireSword.attack(), you're doing something wrong.]
neozf
10-29-2007, 05:00 AM
I tried to use the inheritance thing, but it started freaking out and I can't find a good example anywhere that explains it in a way I would understand :/
Travis
10-29-2007, 09:09 AM
Neo, I'm sorry, but this is going to be difficult. Object-Oriented Programming is not something people simply pick up; it takes work. Yes, things are going to start freaking out... Yes, there are going to be some things that don't make sense immediately. Keep trying, though, and don't despair. If you have any questions about inheritance, just ask - that's what we're here for.
The best I can tell you right now is that inheritance works basically the way it does in real life. You have a base (or parent) class that defines the general way a class should behave. Then you get derived (or child) classes that borrow the parent's behavior, but specialize it in some way. So in the above example, the Weapon class may simply deal X points of damage when calling Weapon.attack(), but the FireSword may add a x2 multiplier for those weak to fire attacks.
Really, I would suggest putting Battle on a back burner until you can become more familiar with OOP. I still don't think you have a clear idea of what classes/objects are, and that's going to make any advanced topics like Inheritance difficult to understand and appreciate. Unfortunately, you're getting into territory that necessitates a better level of understanding of OOP in order to really do what you want.
So leave Battle alone for now. Go out and learn more about OOP. Here's a quick list:
1. Learn what a class/object is. When you have learned this, explain to me what they are. Tell me the difference between a class and an object (yes, they're not quite the same thing), and tell me the relationship between the two.
2. Learn what encapsulation (or "data encapsulation") is. Be sure you can explain this concept of OOP as well. Tell me why it is important and what it can offer to the OOP programmer. This is kind of crucial to understanding what a class/object is.
3. When you're more comfortable with OOP, you can move on and learn what "polymorphism" is (this is where inheritance comes to play). Tell me what it is, why it's important, and - as an example - how you can refer to a derived class through its base class.
Do these in order, and don't even touch Battle until I'm convinced that you know these things well enough to actually use them. You'll be tempted. You'll figure out what encapsulation is, and you'll say "Ah! So this is why Travis told me to make everything private!" but don't go changing anything in Battle. If you do, the WORLD WILL END! But seriously, just forget about Battle until you can do those three things.
</LongestPostEver>
neozf
10-29-2007, 03:28 PM
don't even touch Battle until I'm convinced that you know these things well enough to actually use them. You'll be tempted. You'll figure out what encapsulation is, and you'll say "Ah! So this is why Travis told me to make everything private!" but don't go changing anything in Battle. Oops, too late, I ended up changing it as soon as I worked out how to get it to work yet keep the variables private.
I have been reading though the pages though instead of just picking parts out.
And as for Objects and Classes, would I be right in saying, Objects are like boxes and Classes are the content?
Travis
10-29-2007, 05:59 PM
No, and don't touch Battle again until I tell you to.
neozf
10-29-2007, 08:14 PM
Travis: You do know that my ability to explain things is about as awesome as a grape right? You've seen many times my weird attempts to explain what I'm trying to do.
As for another attempt, Classes are like the original copy containing what variables are used and how. Objects are like copies, containing what the original copy had but can be changed except for how it works.
Travis
10-30-2007, 12:10 AM
Yeah, Neo, I know... but I feel confident that I'll be able to tell when you actually do know what a class/object is. You're still wrong, but you're close. So close that I'll just tell you...
Think of classes as a set of blueprints. Objects are the... well, objects... created from that set of blueprints. Objects are also referred to as "instances." I'll just use an example of cars to hopefully alleviate any confusion. For any given model of car, there will be only one blueprint, but there will be many of those cars actually produced. For the most part they're the same, they drive the same, etc., but they might have different colors or interiors.
Likewise, you'll have one class that defines... well, a class... of objects. It is some similar description that can be applied to all of these objects. Actually, in a very real sense, when you define a class, what you're actually doing is defining a custom "type" (like int, float, or string). Just like you'll have different ints, you'll have different Cars, or different Weapons, or whatever. They're all a part of the same class, they're just different objects.
I just thought I'd throw in something more code-related so you know what's a class and what's an object.
A class is something like,
class Car {
public Car() {};
public ~Car() {};
//etc.
};
Some objects are the following...
Car toyota = new Car();
Car mercedes = new Car();
Car porche = new Car();
toyota, mercedes, and porche are objects of the class Car.
Make sense? There's more to it than just that, of course, but that's the basic idea. If you have any questions, please ask. Otherwise, go on and learn what "encapsulation" is now, and try to explain it to me.
Walrii
11-02-2007, 11:14 AM
Travis: You do know that my ability to explain things is about as awesome as a grape right?
LOL
Umm, in order to make this post useful / on topic, "Porche" is spelled "Porsche."
Also, Neo in your Strike method you probably don't want to pass in a boolean saying whether a player is being hit or not. You'd probably rather pass in an Object that represents who is being hit. Then, you could do something like objectName.isPlayer() to determine if its a player.
neozf
11-02-2007, 12:22 PM
It's not so much a boolean saying whether a player is being hit or not, more of a boolean saying whether the player is doing the hitting or not :p
It's just to get the right message displayed really.
Travis
11-03-2007, 02:09 AM
Whatever, Walrii... you know what I meant.
So, Neo... how's the OOP coming? Learn what "data encapsulation" is yet? You can also look for "data abstraction."
neozf
11-03-2007, 05:48 AM
It's coming... swimmingly?
I managed to borrow a more advanced book from the library which has more sections on classes and a class on inheritance, also, I asked about the independent study thing, and at the end she said she might try to get some extra work for me to be able to do (she's the only computing teacher).
It's coming... swimmingly?
I managed to borrow a more advanced book from the library which has more sections on classes and a class on inheritance, also, I asked about the independent study thing, and at the end she said she might try to get some extra work for me to be able to do (she's the only computing teacher).
An independent study would be good - often it's hard to actually spend time learning something large, but being committed to it for grades ensures you'll spend that time ;)
neozf
11-07-2007, 04:40 PM
For encapsulation, would I be right in saying it's the ability of objects to be like a container so that things outside it can't directly alter the values?
Travis
11-07-2007, 08:52 PM
Very good, Neo. +10 for you. If you want, you can also look up what an "accessor function" is, but there's a limit to my madness - I'm not going to have you look up every little definition related to OOP (even though it may seem like it :twisted:).
Inheritance is a biggie, and I know you've already read up some on it, but I implore you to continue studying it. However, we're at a point where you can't just sit idly and read... you'll have to code. Now, I'm just too much of a skeptic to really believe you wouldn't touch Battle until I said so, but if you do, I don't want to hear about it. Here's your assignment:
Create a hierarchy of classes representing the following... (Shape, Circle, Rectangle, Square) It doesn't matter what you do with them; they can be completely empty for all I care. Just make those four classes using inheritance and post your code here.
neozf
11-08-2007, 06:17 PM
class Shape
{
private int PointOriginX;
private int PointOriginY;
}
class Circle extends Shape
{
private int Radius;
}
class Square extends Shape
{
private int x2, y2;
private int x3, y3;
private int x4, y4;
}
class Rectangle extends Square
{
}
K, so shape only has one variable, the location from where it starts drawing the shape, circle only needs one other variable which is the radius. The square has the location from where it starts drawing from the Shape class and has where the other points are, and the rectangle is like the square, just the lengths aren't all equal so it uses the same variables.
Is that right?
Travis
11-08-2007, 08:33 PM
I take exception to the way you internalize the structure of these classes, but that's beside the point. The point was to see how closely you could match up the conceptual relation within a hierarchy of shapes to actual code. I realize that sentence is kind of obtuse, but that's a huge strength of OOP - to easily have your code properly model your design - and I hope that becomes apparent to you.
Back to your example, though, I have to question the relationship between Rectangle and Square. Not even thinking about programming languages or code, is a Rectangle a Square? Or is a Square a Rectangle?
Inheritance [that is, public inheritance - but we can distinguish such things later] models an "is-a" relationship. That is, you should be able to say that DerivedClass is a BaseClass. If you can't say "DerivedClass is a BaseClass", then your code is wrong.
Here's some examples:
Donkey is a Mammal
Sword is a Weapon
Chocolate is a Sweet
Make sense? So let's move on to something a little closer to Battle, shall we? Don't do anything with Battle itself, but I want you to describe a hierarchy of items you want in your game. This is just design we're talking about (no code). Give me a list of items you want, and organize them using the "is a" relationship.
Neo, even if you were a little off with the square/rectangle thing, you did well. Good job :)
neozf
11-09-2007, 02:58 AM
I've always seen rectangles as a fatter/taller squares D:
Edit:
Just items? Well the only ones currently are;
Blade which is a weapon.
HP potion which is a usable item.
Cartridge which is a misc item.
Edit2:
Few more item ideas;
Long sword which is a weapon.
Great sword which is a weapon.
Dual dagger which is a weapon.
Bow which is a weapon.
Hand gun which is a weapon.
Launcher which is a weapon.
Hp Potion which is a usable item.
Bomb which is a usable item.
Plain Cartridge which is a Cartridge.
Flame Cartridge which is a Cartridge.
Rapid Cartridge which is a Cartridge.
Mana Cartridge which is a Cartridge.
rm_you
11-10-2007, 01:22 AM
Yeah, that does make sense, but TECHNICALLY a square is a specific type of rectangle. You can assume things about squares that you can't assume about rectangles, for example that the distance between any two connected points will be the same. I could speculate about how that would translate to code, but with something this simple it wouldn't really improve the effectiveness of your code, it would just add style points.
Travis
11-10-2007, 01:55 AM
Okay, the whole Rectangle/Square thing beside the point right now (as it was just an example)...
Good Neo, now that you've come up with a goodly list of things, organize them in a clear hierarchy. Try to think of the way certain items "behave"... What sort of actions can we do with them? How can we operate on them? Are there any items that act similarly? The Base item class should be Item. Again, no code. [And yes, it is clear that you've organized the items in a certain way... I just would like to see them hierarchically. Try some nested bulleted lists.]
neozf
11-10-2007, 10:11 AM
Item
|--Weapon
| |
| |---Range---Bow
| | |
| Melee Gun
|
|--Usable
| |
| |---Damage dealing
| |
| Healing
|
|--Cartridge
|
|---Standard
|
|---Extra Damage
|
|---Buffing
Update:
Item
|
|---Weapon
| |
| |--Long Sword
| |--Great Sword
| |--Dual Dagger
| |--Bow
| |--Hand Gun
| |--Launcher
|
|---Usable
| |
| |--Hp Potion
| |--Bomb
|
|---Cartridge
|
|--Plain Cartridge
|--Flame Cartridge
|--Rapid Cartridge
|--Mana Cartridge
Is that right?
Travis
11-11-2007, 04:15 PM
Awesome, Neo. That's great. So here's a little bit you can add... like your initial hierarchy, if you want to differentiate between Ranged and Melee weapons, you can of course split Weapon again into those, and have the appropriate weapons underneath, but that's fine for now.
I also want to point out something about most of the items on the list, which you may or may not have noticed... most of those items are actually objects of the type they're under. So, Long Sword and Great Swords are really just instantiated Weapons.
Like so...
Weapon weapon_ptr = new Weapon("LongSword");
Granted, that's just an example of what it might look like in code, but it doesn't include any of the Weapon's stats, like power, attack rate (a.k.a., cool-down), and so on.
So, do you think you can make some skeleton code from that? That is, make 4 empty classes for Item, Weapon, Usable, and Cartridge... just like you did for the shapes, but don't put any code or variables in them yet.
neozf
11-11-2007, 04:27 PM
class Items
{
}
class Weapon extends Items
{
}
class Usable extends Items
{
}
class Cartridge extends Items
{
}
Like so?
Travis
11-12-2007, 12:45 AM
Looks good. Think you can start making them into real classes and using them in your game?
neozf
11-12-2007, 06:25 PM
:x
Think I might have some problem coding, or it might just be the fact that I'm some what tired and I've got people on vent shouting.
Edit: Wait, I think I'm getting somewhat confused, the Weapon, Usable and Cartridge class are for creating the items, not for how they work right?
Travis
11-12-2007, 08:52 PM
I'm not entirely sure how to answer your question, Neo. The classes are just blueprints for the objects you're creating, but they most certainly define the behavior of those items.
So, for your Weapons, you'll probably have something like the following as your data:
String name;
Int damage;
Int cooldown;
You might also have methods such as...
public void Attack();
public string getName();
private bool CanStrike();
Somewhere else (say, inside of your Character class, or whatever), you'll actually declare objects of this class (e.g., Long Sword, Short Sword, etc.). We'll work out the details later on how everything will fit together, but you should probably start thinking of how you want these items to work. Those ideas will go inside the actual classes.
Edit: There's a few points I'll have to make about the way you'll define Item versus Weapon/Usable/Cartridge, but I want to see what you do with this first.
neozf
11-13-2007, 05:34 PM
class Items
{
}
class Weapon extends Items
{
private String Name;
private int Power;
private int CartSlot;
private String Element;
private String Type;
public Weapon(String Name, int Power, int CartSlot, String Element, String Type)
{
this.Name = Name;
this.Power = Power;
this.Cartslot = Cartslot;
this.Element = Element;
this.Type = Type;
}
}
class Usable extends Items
{
private String Name;
private int Power;
private int Ammount;
private String Type;
public Usable(String Name, int Power, String Type)
{
this.Name = Name;
this.Power = Power;
Ammount=0;
this.Type = Type;
}
}
class Cartridge extends Items
{
private String Name;
private String Type;
private int Ammount;
private String Element;
public Cartridge(String Name, String Type, String Element)
{
this.Name = Name;
this.Type = Type;
Ammount = 0;
this.Element = Element;
}
} This is what I've done so far, my mind is working better today.
Also, if I have something like this;
class ClassA
{
}
class ClassB extends ClassA
{
}
class ClassC extends ClassB
{
}
Stuff in ClassA could be accessed from ClassC right?
Walrii
11-13-2007, 06:57 PM
Stuff in ClassA could be accessed from ClassC right?
Yes, assuming that ClassA gave the classes that inherited from it the permission to.
Ex:
class ClassA
{
private int cantTouchMe;
protected int thoseThatExtendClassACanAccessMe;
public int anyoneCanTouchMe;
}
neozf
11-14-2007, 04:34 PM
Hmm, what would be better, setting the variables to protected, or having something like function(6); in the subclass (where function() is something from the superclass).
Travis
11-14-2007, 11:04 PM
Neo, that's not very descriptive. I know that you must've hit a road block with this, but do try to keep all of your data private. Protected is rarely necessary when you know how to make a derived class interact with its base class sensibly - and that's really sort of the point to all this. Keep trying to do what you're doing with private variables (and no accessor functions), and if you just cannot get it to work, then come back here with a concrete example of what you're trying to accomplish.
And if you don't care to listen to that advice, then go with protected... it's better for the children of a parent class to see its internals than to allow any external class to do so.
neozf
11-15-2007, 04:18 AM
What I currently have is;
class Status
{
private int Hp;
private int MaxHp;
private int Cart;
private int MaxCart;
Status(int MaxHp)
{
this.Hp=MaxHp;
this.MaxHp=Maxhp;
}
void CartMax(int MaxCart)
{
this.MaxCart = MaxCart;
}
}
class Items extends Status
{
}
class Weapon extends Items
{
private String Name;
private int Power;
private int CartSlot;
private String Element;
private String Type;
public Weapon(String Name, int Power, int CartSlot, String Element, String Type)
{
this.Name = Name;
this.Power = Power;
this.CartSlot = CartSlot;
this.Element = Element;
this.Type = Type;
CartMax(CartSlot); //<---------------
}
}
class Usable extends Items
{
private String Name;
private int Power;
private int Ammount;
private String Type;
public Usable(String Name, int Power, String Type)
{
this.Name = Name;
this.Power = Power;
Ammount=0;
this.Type = Type;
}
}
class Cartridge extends Items
{
private String Name;
private String Type;
private int Ammount;
private String Element;
public Cartridge(String Name, String Type, String Element)
{
this.Name = Name;
this.Type = Type;
Ammount = 0;
this.Element = Element;
}
}
The part I was wondering about is where there's an arrow in the comment.
I was wondering how I would go about doing that.
Travis
11-15-2007, 10:26 AM
Aha! You're trying to do a FF VII-esque "materia slot" system! At once your design becomes explicable. Cool.
So now it's time we turn our attention to other forms of inheritance, because you are seriously doing that all wrong. But first, a couple notes about the way you're doing inheritance now....
First things first, why is your Item class empty? You can clearly see some commonality between Weapon, Usable, and Cartridge. Namely, all of them have a name and a type. This should be inside Item. Similarly, any methods you create to, say... display the name or type should go inside Item as well.
The second thing is going back to what public inheritance is all about. Remember how I told you that it should model an "is-a" relationship? An Item is a Status? Not really. Welcome to private inheritance (alternatively, composition (http://en.wikipedia.org/wiki/Composition_in_object-oriented_programming) - which is what we'll actually be using). Composition uses a "has a" relationship. Basically, if you can say that one of your classes has a some other class object inside of it, then that's composition. It's as easy as just declaring a variable of the second class inside of the first class.
So, for Battle, Item is not a Status. Item has a Status. So, you should declare a Status variable inside of Item.
Relatedly, it is clear that your Weapons will have multiple slots for cartridges. See? Another "has a" relationship! So, try creating an array of Cartridges inside of your Weapon class. I think that'll solve a whole lot of the problems you're having right now.
Lesson Summary: There are multiple types of inheritance. Two main ones are public and private (composition). Public inheritance (using 'extends') models an "is a" relationship. Private inheritance (composition) models a "has a" relationship.
neozf
11-15-2007, 02:00 PM
Aha! You're trying to do a FF VII-esque "materia slot" system! At once your design becomes explicable. Cool.
Not really, I've done it so instead of MP to use skills you use Cartridges. Although the weapons will have a sort of like "Materia slot" thing but it would be for bonues (like +2 str etc).
And the Item class is empty because for now I wanted to see what variables the other classes would use first so I don't end up getting confused.
Also, would something like CartMax(CartSlot); work?
Edit: Wait, so do I do it like this?
class Weapon extends Items
{
private int Power;
private int CartSlot;
private String Element;
Status Stat;
public Weapon(String Name, int Power, int CartSlot, String Element, String Type, Status Stat)
{
this.Name = Name;
this.Power = Power;
this.CartSlot = CartSlot;
this.Element = Element;
this.Type = Type;
this.Stat = Stat;
Stat.CartMax(CartSlot);
}
}
Travis
11-16-2007, 02:04 AM
I don't really know what CartMax is, Neo. It looks like a function... but a function that does what? Or if it is not a function, then what is it?
Your Weapon class is better in that it declares a Status. You shouldn't have to do this.Variable, but if you've managed to name the input variables the same, then yes, you do have to. I'll just say right now that it "works," since I believe that's what you want to hear. You really should name your constructor parameters something other than what the corresponding member variables are named, but I'll leave you to that.
We'll talk about initialization lists later, but they're a better way to initialize objects of a class. For now, what you're doing seems appropriate. I just don't know what CartMax is.
neozf
11-16-2007, 02:55 AM
CartMax would be the same as MaxMP, it sets the max amount of cartridges you can have loaded at one time.
Er, how could I explain it, think of the weapons a gun, you need ammo in it so it works (Unless you start smacking people with it). The cartridges are like ammo, except they aren't needed to hit things, just so you can use skills.
tl;dr Cartridges = Mp.
Travis
11-16-2007, 10:38 AM
That still doesn't answer my question, Neo. What's with the crazy syntax? CartMax() looks like a function, not a variable. What are you trying to do with it?
neozf
11-16-2007, 02:57 PM
Sorry, I can see how that could be confusing, it's a function that sets the max amount of Cartridges you can have loaded at one time. I also worked on it quite a bit while at school, and most of it works.
Status Class
class Status
{
private int Hp;
private int MaxHp;
private int Cart;
private int Cartridge;
private int MaxCart;
Status(int MaxHp)
{
this.Hp=MaxHp;
this.MaxHp=MaxHp;
}
void CartMax(int MaxCart)
{
this.MaxCart = MaxCart;
}
void Recov(int Power)
{
Hp=Hp+Power;
if(Hp>MaxHp){Hp=MaxHp;}
}
void Damag(int Dmg)
{
Hp=Hp-Dmg;
}
void ShowHealth()
{
System.out.println(Hp+"/"+MaxHp+" Hp Remaining");
}
void ShowCart()
{
System.out.print("Cartridges remaining: ");
for (Cartridge=Cart; Cartridge>0; Cartridge--)
{
System.out.print("0 ");
}
for (Cartridge=MaxCart-Cart;Cartridge>0;Cartridge--)
{
System.out.print("X ");
}
}
}
Item Class
class Items
{
private String Name;
private String Type;
private boolean CType;
void SetName(String Name)
{
this.Name=Name;
}
void SetType(String Type)
{
this.Type=Type;
}
String GetName()
{
return Name;
}
String GetType()
{
return Type;
}
boolean CheckType(String type)
{
if (Type.equals(type))
{
CType=true;
}
return CType;
}
}
Usable Class
class Usable extends Items
{
private int Power;
private int Amount;
private Status Target;
public Usable(String Name, int Power,int Amount, String Type)
{
SetName(Name);
this.Power = Power;
this.Amount=Amount;
SetType(Type);
}
void Heal(Status Target)
{
if (CheckType("Healing"))
{
this.Target=Target;
Target.Recov(Power);
System.out.println("Healed "+Power+"Hp with "+GetName());
Amount--;
}
}
}
Weapon Class
class Weapon extends Items
{
private int Power;
private int CartSlot;
private String Element;
private Status Stat;
public Weapon(String Name, int Power, int CartSlot, String Element, String Type, Status Stat)
{
SetName(Name);
this.Power = Power;
this.CartSlot = CartSlot;
this.Element = Element;
SetType(Type);
this.Stat = Stat;
Stat.CartMax(CartSlot);
}
void ShowStats()
{
System.out.println(GetName());
System.out.println(Power);
System.out.println(CartSlot);
System.out.println(GetType());
System.out.println(Element);
}
int Strike(boolean Player)
{
int DMG=(int) Math.ceil(Math.random()*Power);
if(Player){System.out.println("You deal "+DMG+" damage");}
if(!Player){System.out.println("The enemy deals "+DMG+" damage");}
return DMG;
}
}
Cartridge Class
class Cartridge extends Items
{
private int Amount;
private String Element;
public Cartridge(String Name, String Type, int Amount, String Element)
{
SetName(Name);
SetType(Type);
this.Amount = Amount;
this.Element = Element;
}
}
Battle Class (Still being worked on, just started today)
public class Battle
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
//Declare HP for Player and Enemy
Status Player = new Status(40);
Status Enemy = new Status(50);
//Declare Items
Weapon Longsword = new Weapon("Long Sword", 6, 5, "NULL", "Blade", Player);
Usable Hp_Pot = new Usable("Hp Potion",30,3,"Healing");
Cartridge Plain = new Cartridge("Plain Cartridge","Standard", 20,"NULL");
//
Weapon Claw = new Weapon("Claw",5,0,"NULL","Blade",Enemy);
//Declare Variables
String[] Normal = {"Attack","Cartridge System","Item"};
int NSelect;
int a;
//Battle Start
Player.ShowHealth();
Player.ShowCart();
System.out.println("\nWhat will you do?");
for (a=0;a!=Normal.length;a++)
{
System.out.print((a+1)+". "+Normal[a]+"\n");
}
NSelect=in.nextInt();
if(Normal[NSelect-1].equals("Attack"))
{
Enemy.Damag(Longsword.Strike(true));
}
}
}
Walrii
11-16-2007, 08:30 PM
I just want to say Neo, that Travis is apparently an OO nazi. So, yes he's strict and yes if I were you I'd be ticked because it seems like every time you try something he yells at you for doing something wrong BUT I think you're in good hands. He knows what he's doing and in the end, you'll learn a TON.
neozf
11-16-2007, 08:53 PM
True, during this whole thing I have learnt quite alot, and trying to get it to work has also helped me learns somewhat.
neozf
11-18-2007, 01:21 PM
Latest source on http://forums.editingarchive.com/showthread.php?p=37860
Also Travis, Is there a way to put all the code that declares the stuff for the classes on something else like an include file?
Travis
11-18-2007, 06:37 PM
You should start referring to the JavaDocs (http://java.sun.com/j2se/1.5.0/docs/api/) for questions concerning the use of Java and its build-in library of classes and such. I'll list some of the more useful packages...
java.lang - This is where all the language features are held. You can learn more about how to use Java itself by reading its JavaDoc.
java.util - A very useful package with lots of classes and algorithms that are general purpose. Things like Lists, Hashes, sorting & searching algorithms can be found in there.
java.awt & javax.swing - both are Graphics APIs. Once you get most of the core functionality of your game working, assuming you want to throw some graphics on there, you can use these.
java.rmi - networking API. If you decide to try some multiplayer, you can use Java's Remote Method Invocation (RMI) to do so.
neozf
11-19-2007, 05:37 PM
From the IRC last night;
Travis: Changed it to use switches (Updated the thread with the new source code), also, for the Command class, would I be putting the switches there as well? And how do I make it so I can have a file with all the declarations (i.e "Skill Flame_Strike = new Skill(1,"Flame Strike",5,10,1,"Fire")") so I can just have like import File at the top or something like that.
Walrii
11-19-2007, 06:49 PM
(Note, I stink a OO programming because I prefer "hackish" solutions but I'd like to have Travis's comments)
Answer to Neo's question: Make a class!
Part which I'm guessing at and doesn't seem like a super awesome idea, but we'll see what Travis says:
Specifically, maybe have a "WorldSkills" class which keeps information on all possible skills. You could then do worldSkillsObject.find("flame strike") to find a skill. You could also store some additional data on the skills. For example, maybe you want to store information on the skills and who they might be used by:
worldSkillsObject.getBaseSkills(Humanoid); //maybe all humanoids know how to run away from battle?
worldSkillsObject.getBaseSkills(Mage, 2); //where 2 is the level
Travis
11-19-2007, 09:07 PM
Sorry, Neo... couldn't really get around to this earlier. Anyway, stop me if I'm mistaken, but it sounds to me like you just want to store all of the "attributes" for your objects in a separate file, so you don't have to initialize them at the start of every run. Yes?
If that's the case, there's no need for a super sophisticated solution. You'll just need to store your values in a file (like, "Skills.ini") [I would name the .ini file after whatever class you're initializing, just to keep it consistent and understandable.]. Then, create a method to load in the requisite data. (Skills.initFromFile("filename"); or something) Look for materials on File I/O with Java.
You shouldn't have to create any additional classes or craziness... just a method in each of your classes to initialize objects from a file. You might add the method to your base class and override it in the derived classes.
There is an alternative with which I'm not very familiar, but you can actually write Java objects to files if they implement Serializable. [I can't believe I actually remembered the interface for it. :P] This allows you to store the actual binary data in memory to a file and read it back in. It's a solution worth looking into if you have the time, as it's pretty powerful and will greatly simplify the process if you ever decide to make a level/creature editor.
If you want to go that route, we'll talk about what "interfaces" are, how they're useful (hint: VERY), and how to use them.
neozf
11-20-2007, 06:09 PM
Er, think I'll skip that for now :p
I've updated the source code thread again along with the new class tree.
I'm currently having a problem with the Itembag class, near the bottom there's a piece of code that moves all the arrays on to the left (contents of [1] go into [0] etc) when the item runs out. It works if you use the third item, then the second then the first. But for some reason, if you use the second item up first it only displays the first item, but the third one can still be used and it no longer cares about it's amount. And if you use the first item up first then second item stays where it is, but also replaces the first item's place, and the third item can still be used and it doesn't check how many it has left.
Any idea why that could be? (Sorry if it's a bad explanation, grapes and all).
Travis
11-21-2007, 10:23 AM
Neo, I believe I told you not to use arrays for this in IRC. This is not an OOP principle as much as it is a principle of data structures (as in, this applies to all languages)... removing elements from the middle of an array is an expensive operation. You should use a List (Linked List) instead. Now, I gave you a link to the JavaDoc page on Lists. I suggest you check that out and learn to use them. It may help for you to set Battle aside again and get familiar with them using a sample program. In the very least, read up on them... you're getting into territory wherein arrays are simply insufficient for what you wish to do.
Here's a wikipedia entry on linked lists (http://en.wikipedia.org/wiki/Linked_list) to get you started.
neozf
11-22-2007, 03:51 PM
Woo, the weird Item bug has been fixed thanks to Aad after I showed him the piece of code which checks the item type.
[20:46] <Aad> In that case
[20:46] <Aad> Else { CType = false;
[20:48] > Oh, awesome, thanks :D
[20:48] > I so did not expect that.
neozf
11-24-2007, 10:13 AM
Hmm, I think I finally understood what you were saying, so I'm wondering, is this correct?
Code in the Item class for loading the file.
public void FileLoad(String FileName) throws IOException
{
FileReader file=new FileReader(FileName);
BufferedReader buffer = new BufferedReader(file);
int b=0;
String Data[];
int WepAmount=2;
Data = new String[12];
String textline=null;
while ((textline = buffer.readLine())!=null)
{
Data[b]=textline;
b++;
}
}Thing in the Weapon class for when a new object is made
public Weapon(String FileName) throws IOException
{
FileLoad(FileName);
}Line in the Battle class for loading it
Weapon TestWeapon = new Weapon("Data/Weapons.ini");And as for the .ini file.
Long Sword //Weapon Name [String]
6 //Power [int]
5 //Cart slots [int]
NULL //Element [String]
B //Type [char]
Player //User [Status]
Flame_Strike //Skill name [String] (I'll fix that later~)
Flame_Strike //Skill object [Skill]
Deus_Shot //Skill name [String] (And this one~)
Deus_Shot //Skill object [Skill]
CurrentCart //Current Cartridge [Cartridge]
Update: Made it so there's no reason to have a string for the Skill name.
Current problem, I'm trying to use Serialization for the file loading, but I keep getting an error.
Source for the File loading.
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
public class File
{
private ObjectInputStream input;
public void OpenFile()
{
try
{
input = new ObjectInputStream (new FileInputStream("Data/Weapons.ini"));
}
catch ( IOException ioException )
{
System.err.println("Error during read from file.");
}
}
public void ReadData()
{
Weapon Data;
try
{
while (true)
{
Data=(Weapon) input.readObject();
System.out.println(Data.GetName());
System.out.println(Data.GetSkillID1());
}
}
catch ( EOFException endOfFileException )
{
return;
}
catch ( ClassNotFoundException classNotFoundException )
{
System.err.println("Unable to create object");
}
catch ( IOException ioException )
{
System.err.println("Error during read from file.");
}
}
public void CloseFile()
{
try
{
if(input!=null){input.close();}
}
catch ( IOException ioException )
{
System.err.println("Error closing file.");
System.exit(1);
}
}
}
Source for the testing
public class FileRead
{
public static void main(String[] args)
{
File Lawl = new File();
Lawl.OpenFile();
Lawl.ReadData();
Lawl.CloseFile();
}
}
The error is in the File loading part.
I'm assuming input = new ObjectInputStream (new FileInputStream("Data/Weapons.ini"));
Is somehow failing, even though the exact path of the file is "BattleJ\Data\Weapons.ini" With BattleJ being the folder the source code is in.
neozf
11-25-2007, 07:14 PM
Well, I finally managed to get it working, with the cartridge class, now to try with the other classes, main problem is that they have something like "Status Stat" or "Skill SkillID1" which makes it harder.
Also, I'm wondering how I would get it to work if I have multiple items, it loads a single item ok, but what if I have more then one? How would I know how to call each item?
Travis
11-26-2007, 10:31 AM
Well, you of course have 2 choices: Either put each object in its own file (Fire_Sword.weap, Bomb.itm, etc.) or learn to store more than one item in a file. File I/O is no different in the case of storing classes as it is with any other piece of data... you can read, write, and append. If you write, then you'll clobber whatever was already in that file. If you append, then you just add to the file without writing over the existing data.
neozf
11-26-2007, 01:55 PM
I was thinking of getting it to load the object when it needs it, which would include putting each object into their own file, it shouldn't take much time either.
neozf
11-27-2007, 04:45 PM
Woo, Finally got it working, I managed to move all that declaring code to another class (The whole file loading thing hated me >.>) but at least it works now.
Everything AFAIK is working, the cartridge you are using can be changed, reloading and changing the Cartridge you are using doesn't give the enemy a chance to attack (Since changing and reloading at one turn each). I managed to fix a bug with the Weapon class I didn't notice before, due to the way I had it set up, the max amount of Cartridges you could have loaded at once depended on the last Weapon declared instead of the Weapon you are using.
I think I've forgotten to put in a piece of code to stop you from using an item/changing to a cartridge that doesn't exist, I'll fix that next time.
That's all I can think of right now, even anyone finds a bug reply etc.
Also, how would I make a file so it can be executed? (So it can be played with out having to compile it).
neozf
11-29-2007, 05:58 PM
<Travis> Well... or you could just give up on BattleJ.
<Travis> I mean, you're already done... there couldn't possibly be anything else you would ever want to do with it.Lolwut? I don't have a reason to end it now, I've still got quite afew things to try to get working, things like Elements having a use, get some kind of class system working and sometime or another try to get it online. There's also a few more kinds of cartridges that can be used and a few other weapons.
P.S
[16:57] * Misery missed the Travis that was a jerk.
[02:24] * Jarclogen misses the nice Travis.
[02:27] <Travis> Get Neo to cut and paste you saying "What happened to the jerk Travis?" "I miss the jerk Travis?"
[02:27] <Travis> 'cause he'll do it... if he wasn't asleep.
:p
Jarclogen
11-29-2007, 06:03 PM
I don't remember ever saying the first line. >.>
neozf
11-29-2007, 06:06 PM
Well according to http://www.editingarchive.com/irc/logs/?day=23Nov2007
You did.
Travis
11-29-2007, 10:02 PM
I was being facetious, Neo. Also... [Re: Jar's quote] lol.
neozf
12-05-2007, 05:57 PM
I've made quite a few changes; I've added an Actor class (For players) and an Enemy class (For Enemies) and the main change are I've made it more like a game, Battle was originally (And for most of the versions) just an engine for the battle part of a game I might want to make. I've started working on it so it's more like a game, so far there's a town which gives the option of going into the field (Battle) or going to the healer (Full heal). You gain GP (the currency, for now at least) and Exp after each battle, there's a piece of code to check if you level up or not, but there's a slight bug in it that I'm trying to work out currently.
vBulletin® v3.7.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.