Help me please please please
Hey i am very new to java and programming and im making a console rpg type game which i will develop into something more once i get a better grip of everything. But here is my question. If i have a parameter of type string, and say it is called name. is there a way that I can specify so that users can only enter certain names or else they cannot continue and they dont create the object until they put in an acceptable name? Thanks
-abc123
Simple game interaction example
I assume you mean that you want
a variable in one object to be
modified by the actions of another
object.
Maybe a "Monster" will effect the
"health" of a specific "Player".
There are many ways to do this in general.
From what little I understand of
your game, I offer the following:
1. Write a method in the Player class called
"healthChange(String, String)" and enumerate
all conditions that can effect a Player object's
health:
Code:
class Player{
byte health = 100;
void healthChange(String by, String cause){
if(by.equals("monster")){
if(cause.equals("bite")){ health -= 10; }
else if(cause.equals("claw")){ health -= 5; }
else if(cause.equals("decapitation")){ health = 0; }
}
else if(by.equals("fairy")){
if(cause.equals("bite")){ health -= 1; }
else if(cause.equals("slap")){ ; }
}
else if(by.equals("dog")){
if(cause.equals("bite")){ health -= 4; }
else if(cause.equals("claw")){ health -= 2; }
}
else{
System.out.println("Undefined combination");
System.out.println( by + "s do not " + cause );
}
}
}
2.
Write your Monster class so that it
has access to your Player class, and
add methods that will effect the health
within the Player object, referred to as
"victim" in the Monster and Fairy classes
below:
Code:
class Monster{
Player victim = null;
Monster(Player victim){
this.victim = victim;
}
void biteVictim(){
victim.healthChange("monster", "bite");
}
void clawVictim(){
victim.healthChange("monster", "claw");
}
void decapitateVictim(){
victim.healthChange("monster", "decapitate");
}
}
class Fairy{
Player victim = null;
Fairy(Player victim){
this.victim = victim;
}
void biteVictim(){
victim.healthChange("fairy", "bite");
}
void slapVictim(){
victim.healthChange("fairy", "slap");
}
}
All of these classes will compile if placed in a
single file.
You will find these examples a little restricted
and unsophisticated, but I'm only guessing that
this is what you are trying to accomplish.
The basic point is that an object must access
a reference to another object in order to effect
it.
The second point is that the nature and behaviors
of the objects; their actions and reactions are
defined within their methods.
More monster / player advice
I created objects to satisfy a very narrow
understanding of what I think you want to
accomplish; a monster attacking a player.
If you want a player to effect the health
of a monster, allow the Player object to
gain access to a Monster object by giving
the Player class a constructor that requires
a reference to a Monster:
Code:
Player(Monster mon){
this.monster = mon;
}
Make sure you add into the Player class a place
to hold the reference to the Monster object he
is encountering:
Code:
Monster monster = null;
Also give the Monster class a health variable:
Add your method that effects the health of
the Monster object to your Player class:
Code:
void attackMonster(){
mHP = rand.nextInt(2);
if(mHP == 0){
monster.health = (r.nextInt(51)) + Player.health;
}
else if(mHP == 1){
monster.health = Player.health - (r.nextInt(51));
}
}
The following code compiles.
The monster can effect the player's health, and
the player can effect the monster's health.
They both contain the variables and methods
to do this.
Code:
class Monster{
Player victim = null;
int health = 100;
Monster(Player victim){
this.victim = victim;
}
void biteVictim(){
victim.healthChange("monster", "bite");
}
void clawVictim(){
victim.healthChange("monster", "claw");
}
void decapitateVictim(){
victim.healthChange("monster", "decapitate");
}
}
class Player{
Monster monster = null;
int health = 100;
Player(Monster mon){
this.monster = mon;
}
void healthChange(String by, String cause){
if(by.equals("monster")){
if(cause.equals("bite")){ health -= 10; }
else if(cause.equals("claw")){ health -= 5; }
else if(cause.equals("decapitation")){ health = 0; }
}
else if(by.equals("fairy")){
if(cause.equals("bite")){ health -= 1; }
else if(cause.equals("slap")){ ; }
}
else if(by.equals("dog")){
if(cause.equals("bite")){ health -= 4; }
else if(cause.equals("claw")){ health -= 2; }
}
else{
System.out.println("Undefined combination");
System.out.println( by + "s do not " + cause );
}
}
void attackMonster(){
// The code you gave for this method does not compile.
// You will have to iron out the math to make
// it perform to your specifications.
// as a subststitute I'll use this:
monster.health -= 1;
}
}
==============================================
EDIT: Below is the above code with modifications.
To add better symetry to the code, I have modified the
Player classes' attackMonster() method, and have given
the Monster class a healthChange() method.
Also, because I have no outline of your game, I have
added two more constructors.
==============================================
Code:
class Monster{
Player victim = null;
int health = 100;
Monster(){}
Monster(Player victim){
this.victim = victim;
}
void biteVictim(){
victim.healthChange("monster", "bite");
}
void clawVictim(){
victim.healthChange("monster", "claw");
}
void decapitateVictim(){
victim.healthChange("monster", "decapitate");
}
void healthChange(){
// The code you gave for this method does not compile.
// You will have to iron out the math to make
// it perform to your specifications.
// as a subststitute I'll use this:
health -= 1;
}
}
class Player{
Monster monster = null;
int health = 100;
Player(){}
Player(Monster mon){
this.monster = mon;
}
void healthChange(String by, String cause){
if(by.equals("monster")){
if(cause.equals("bite")){ health -= 10; }
else if(cause.equals("claw")){ health -= 5; }
else if(cause.equals("decapitation")){ health = 0; }
}
else if(by.equals("fairy")){
if(cause.equals("bite")){ health -= 1; }
else if(cause.equals("slap")){ ; }
}
else if(by.equals("dog")){
if(cause.equals("bite")){ health -= 4; }
else if(cause.equals("claw")){ health -= 2; }
}
else{
System.out.println("Undefined combination");
System.out.println( by + "s do not " + cause );
}
}
void attackMonster(){
monster.healthChange();
}
}
Object variables are zero
The code you provided looks like
the format for items in a frame.
It doesn't have a direct connection
to strategy of the game you are
experimenting on.
I have no idea what data types you
are using for this game.
In my past responses I have used
either byte or int. But what I used
in those examples is meaningless.
What data types are you using?
Code:
mHP = rand.nextInt(2);
if(mHP == 0){
health = (r.nextInt(51)) + Player.health;
}else if(mHP == 1){
health = Player.health - (r.nextInt(51));
}
This code you provided, which I don't
completely understand, gives me a
hint that you might be using java's
Math.random() method, which means
you are working with a double data
type.
It could be that you are casting
your double values into int and
they are all being truncated to 0.
With practically no knowledge of
what you are doing, I advise you
to perform all your calculations
using double data variables.
For my second guess, you might
be accessing a non-interactive
object while under the impression
you are accessing an interactive
object.