Results 1 to 14 of 14
  1. #1
    abc123 is offline Member
    Join Date
    Dec 2010
    Posts
    8
    Rep Power
    0

    Default 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

  2. #2
    abc123 is offline Member
    Join Date
    Dec 2010
    Posts
    8
    Rep Power
    0

    Default

    like here is an example of what i have:

    if(name == "bob" || name == "tim" || name == "john")
    {
    health = 1;
    level = 1;
    armor = 1;
    damage = 1;
    }else{

    }


    but what do i put in the else clause so that the object doesnt become created and they need to enter one of the names given, bob, tim and john

  3. #3
    turc0033 is offline Member
    Join Date
    Aug 2010
    Posts
    7
    Rep Power
    0

    Default

    Put this in the else statement:

    Java Code:
    JOptionPane.showMessageDialog(null, "Your message goes here", "Warning", JOptionPane.WARNING_MESSAGE);
    
    // TODO: Show your input dialog again

  4. #4
    abc123 is offline Member
    Join Date
    Dec 2010
    Posts
    8
    Rep Power
    0

    Default

    it says "cannot find symbol = variable JOptionPane"

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,236
    Rep Power
    19

    Default

    1. Use a meaningful subject line. "Help me please please please" isn't one.

    2. Don't use == for comparing Strings or any other objects. Use the .equals(...) method instead.

    3. Wrap the input in a loop that terminates only when the input is valid and acceptable.

    4. Ignore the advice given by turc0033. It's totally off the mark.

    db

  6. #6
    abc123 is offline Member
    Join Date
    Dec 2010
    Posts
    8
    Rep Power
    0

    Default

    so what would it be like?

    while (roll != null)
    {
    if(name.equals "bob" || name.equals "tim" || name.equals "john")
    {
    health = 1;
    level = 1;
    armor = 1;
    damage = 1;
    break;
    }else{
    ???? what goes here?
    }

  7. #7
    abc123 is offline Member
    Join Date
    Dec 2010
    Posts
    8
    Rep Power
    0

    Default

    here ok this is my overview maybe this will help narrow my question. So i have this:
    public Player(String preName)
    {
    name = preName;
    nameCheck();
    }

    private void nameCheck()
    {

    if(name == "bob" || name == "tim" || name == "john")
    {
    health = 1;
    level = 1;
    armor = 1;
    damage = 1;
    }else{

    }

    and what i want in that else clause is it to make sure if the names the user chose arent bob, tim or john, then it recalls public Player and the parameter preName is asked again until the name is either bob, tim or john.

  8. #8
    paul pasciak is offline Senior Member
    Join Date
    Jul 2008
    Posts
    125
    Rep Power
    0

    Default console game response

    I assume you are trying to develope
    code for an object within a game.
    I threw this together.
    It compiles, so it may be a start
    for whatever you are trying to do.

    Java Code:
    class Player{
      String name;
      byte health, level, armor, damage;
    
      Player(String aName){
        while(!nameCheck()){
          getPreName();
        }
      }
    
      void getPreName(){}
    
      private boolean nameCheck(){
        if(name.equals("bob") || name.equals("tim") || name.equals("john")){
          health = 1;
          level = 1;
          armor = 1;
          damage = 1;
          return true;
        }
        else{
          return false;
        }
      }
    }

  9. #9
    abc123 is offline Member
    Join Date
    Dec 2010
    Posts
    8
    Rep Power
    0

    Default

    I also have another quick question. say i have an instance variable from class Player. how do i use that variable in class Monsters? thanks
    -abc

  10. #10
    paul pasciak is offline Senior Member
    Join Date
    Jul 2008
    Posts
    125
    Rep Power
    0

    Default 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:

    Java 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:

    Java 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.
    Last edited by paul pasciak; 12-23-2010 at 09:20 PM. Reason: reversed "cause" and "by" args in method

  11. #11
    abc123 is offline Member
    Join Date
    Dec 2010
    Posts
    8
    Rep Power
    0

    Default

    well heres the thing. In my monster class, i have the monsters health determined by the following equation:
    mHP = rand.nextInt(2);
    if(mHP == 0){
    health = (r.nextInt(51)) + Player.health;
    }else if(mHP == 1){
    health = Player.health - (r.nextInt(51));
    }
    and the only way to be able to use Player.health is if the field health in the Player class is static. Now i was wondering if there was a way i didnt need to make it static to use it to get the health of a specific Player object rather than the health of the class(static).
    -abc

  12. #12
    paul pasciak is offline Senior Member
    Join Date
    Jul 2008
    Posts
    125
    Rep Power
    0

    Default 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:

    Java 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:

    Java Code:
      Monster monster = null;

    Also give the Monster class a health variable:

    Java Code:
      int health = 100;

    Add your method that effects the health of
    the Monster object to your Player class:

    Java 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.

    Java 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.

    ==============================================


    Java 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();
        }
      }
    Last edited by paul pasciak; 12-28-2010 at 07:07 PM.

  13. #13
    abc123 is offline Member
    Join Date
    Dec 2010
    Posts
    8
    Rep Power
    0

    Default

    I have another question. here is the code and ill post my question after the code.



    public class mainFrame extends javax.swing.JFrame {

    /** Creates new form mainFrame */
    public mainFrame() {
    initComponents();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

    jPanel1 = new javax.swing.JPanel();
    jLabel1 = new javax.swing.JLabel();
    jLabel2 = new javax.swing.JLabel();
    jLabel3 = new javax.swing.JLabel();
    jLabel4 = new javax.swing.JLabel();
    jLabel5 = new javax.swing.JLabel();
    jLabel6 = new javax.swing.JLabel();
    jLabel7 = new javax.swing.JLabel();
    jLabel8 = new javax.swing.JLabel();
    jLabel9 = new javax.swing.JLabel();
    jLabel10 = new javax.swing.JLabel();
    jLabel11 = new javax.swing.JLabel();
    jLabel12 = new javax.swing.JLabel();
    jLabel13 = new javax.swing.JLabel();
    jLabel14 = new javax.swing.JLabel();
    jLabel15 = new javax.swing.JLabel();
    jLabel16 = new javax.swing.JLabel();

    setDefaultCloseOperation(javax.swing.WindowConstan ts.EXIT_ON_CLOSE);

    jPanel1.setBorder(javax.swing.BorderFactory.create EtchedBorder());

    jLabel1.setText("Helm");

    jLabel3.setIcon(new javax.swing.ImageIcon("/Users/chrismoritz/Desktop/Sprites of Game/blank.png")); // NOI18N

    jLabel4.setText("Chest");

    jLabel5.setIcon(new javax.swing.ImageIcon("/Users/chrismoritz/Desktop/Sprites of Game/blank.png")); // NOI18N

    jLabel6.setText("Legs");

    jLabel7.setIcon(new javax.swing.ImageIcon("/Users/chrismoritz/Desktop/Sprites of Game/blank.png")); // NOI18N

    jLabel8.setText("Arms");

    jLabel9.setIcon(new javax.swing.ImageIcon("/Users/chrismoritz/Desktop/Sprites of Game/blank.png")); // NOI18N

    jLabel10.setText("Weapon");

    jLabel11.setIcon(new javax.swing.ImageIcon("/Users/chrismoritz/Desktop/Sprites of Game/blank.png")); // NOI18N

    jLabel12.setIcon(new javax.swing.ImageIcon("/Users/chrismoritz/Desktop/Sprites of Game/blank.png")); // NOI18N

    jLabel13.setText("Weapon");

    jLabel14.setText("Health: " + Player.health);

    jLabel15.setText("Armor: " + Player.armor);

    jLabel16.setText("Damage: " + Player.damage);

    org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
    jPanel1Layout.createParallelGroup(org.jdesktop.lay out.GroupLayout.LEADING)
    .add(jPanel1Layout.createSequentialGroup()
    .add(34, 34, 34)
    .add(jPanel1Layout.createParallelGroup(org.jdeskto p.layout.GroupLayout.LEADING)
    .add(jLabel14)
    .add(jLabel15)
    .add(jPanel1Layout.createSequentialGroup()
    .add(jLabel16)
    .add(288, 288, 288)
    .add(jPanel1Layout.createParallelGroup(org.jdeskto p.layout.GroupLayout.LEADING)
    .add(jPanel1Layout.createSequentialGroup()
    .add(41, 41, 41)
    .add(jLabel2))
    .add(jPanel1Layout.createSequentialGroup()
    .add(jPanel1Layout.createParallelGroup(org.jdeskto p.layout.GroupLayout.TRAILING)
    .add(jLabel1)
    .add(jLabel4)
    .add(jLabel6))
    .add(18, 18, 18)
    .add(jPanel1Layout.createParallelGroup(org.jdeskto p.layout.GroupLayout.LEADING)
    .add(jLabel7)
    .add(jLabel5)
    .add(jLabel3))
    .add(158, 158, 158)
    .add(jPanel1Layout.createParallelGroup(org.jdeskto p.layout.GroupLayout.LEADING)
    .add(jPanel1Layout.createParallelGroup(org.jdeskto p.layout.GroupLayout.TRAILING)
    .add(jLabel10)
    .add(jLabel13))
    .add(jLabel8))
    .add(18, 18, 18)
    .add(jPanel1Layout.createParallelGroup(org.jdeskto p.layout.GroupLayout.LEADING)
    .add(jLabel9)
    .add(jLabel12)
    .add(jLabel11))))))
    .addContainerGap(110, Short.MAX_VALUE))
    );
    jPanel1Layout.setVerticalGroup(
    jPanel1Layout.createParallelGroup(org.jdesktop.lay out.GroupLayout.LEADING)
    .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1Layout.createSequentialGroup()
    .add(32, 32, 32)
    .add(jPanel1Layout.createParallelGroup(org.jdeskto p.layout.GroupLayout.TRAILING, false)
    .add(jPanel1Layout.createSequentialGroup()
    .add(jLabel1)
    .add(18, 18, 18)
    .add(jLabel14)
    .addPreferredGap(org.jdesktop.layout.LayoutStyle.R ELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    .add(jLabel4))
    .add(jPanel1Layout.createSequentialGroup()
    .add(jPanel1Layout.createParallelGroup(org.jdeskto p.layout.GroupLayout.LEADING)
    .add(jLabel2)
    .add(jLabel3)
    .add(jLabel12)
    .add(org.jdesktop.layout.GroupLayout.TRAILING, jLabel13))
    .add(44, 44, 44)
    .add(jPanel1Layout.createParallelGroup(org.jdeskto p.layout.GroupLayout.TRAILING)
    .add(jPanel1Layout.createParallelGroup(org.jdeskto p.layout.GroupLayout.LEADING)
    .add(jLabel11)
    .add(jLabel5))
    .add(jLabel10, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 16, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))))
    .addPreferredGap(org.jdesktop.layout.LayoutStyle.R ELATED)
    .add(jLabel15)
    .add(42, 42, 42)
    .add(jPanel1Layout.createParallelGroup(org.jdeskto p.layout.GroupLayout.TRAILING)
    .add(jLabel7)
    .add(jPanel1Layout.createParallelGroup(org.jdeskto p.layout.GroupLayout.BASELINE)
    .add(jLabel6)
    .add(jLabel16))
    .add(jLabel8)
    .add(jLabel9))
    .add(54, 54, 54))
    );

    org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
    layout.createParallelGroup(org.jdesktop.layout.Gro upLayout.LEADING)
    .add(layout.createSequentialGroup()
    .addContainerGap()
    .add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    .addContainerGap())
    );
    layout.setVerticalGroup(
    layout.createParallelGroup(org.jdesktop.layout.Gro upLayout.LEADING)
    .add(layout.createSequentialGroup()
    .addContainerGap()
    .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 263, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
    .addContainerGap(201, Short.MAX_VALUE))
    );

    pack();
    }// </editor-fold>//GEN-END:initComponents

    public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
    public void run() {
    new mainFrame().setVisible(true);
    }
    });
    }

    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;

    }



    my question is, Player.health, Player.armor and Player. damage keep coming up as 0 but when i inspect the Player class and see the fields, they are filled with the correct numbers.

  14. #14
    paul pasciak is offline Senior Member
    Join Date
    Jul 2008
    Posts
    125
    Rep Power
    0

    Default 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?

    Java 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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •