Results 1 to 5 of 5
Like Tree2Likes
  • 2 Post By SurfMan

Thread: How to actually design programs with multiple classes?

  1. #1
    Join Date
    Aug 2011
    Posts
    5
    Rep Power
    0

    Default How to actually design programs with multiple classes?

    I'm learning java and am a little confused on how big programs are made in java. Say I want to create a little text adventure game. In that game are different zones. Would each zone have to be a different class? Or would I just run everything from a while loop in the main class? If different areas, combat, quests are different classes wouldn't that cause my program to have so many import statements? Like importing the users stats in each class?
    I'm pretty sure you're not supposed to have everything running from one while loop in the main class, but that's the only way I know how. I've looked online at how to construct OOP programs but found nothing.

    I learned basic python a while back and made a text adventure game. In that game I just had the entire game run from one while loop and I imported the combat and areas into that. The java book I'm reading says that my program shouldn't be like that, it should have objects or classes talking and interacting with each other.

    I'm just looking for some advice. How would you design a text based adventure? Would you just run everything from one while loop? What about bigger programs?

    Sorry if this question isn't allowed as it's not really code related.

  2. #2
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: How to actually design programs with multiple classes?

    The concept you are struggling with is called modeling. Armed with that keyword, perhaps you can find some articles or tutorials that will get you started in the right direction.
    Get in the habit of using standard Java naming conventions!

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,453
    Blog Entries
    7
    Rep Power
    20

    Default Re: How to actually design programs with multiple classes?

    imho it's more difficult to model anything a bit substantial in a single class (nested classes don't count ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,900
    Rep Power
    5

    Default Re: How to actually design programs with multiple classes?

    Quote Originally Posted by BigBlueBuffalo View Post
    How would you design a text based adventure? Would you just run everything from one while loop?
    That's the wrong question: the answer is YES; the main game loop. But that doesn't mean that all the code to be run is inside that one while loop. Even the smallest game that does anything exciting I've made consists of at least two dozen classes.

    the Java Enterprise application I maintain and expand at work consists of 2000+ classes, working against a database that consists of 230+ tables. That's what I consider an application of moderate size, not big. Quite easy to work with if you use an IDE such as Eclipse properly.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    936
    Rep Power
    2

    Default Re: How to actually design programs with multiple classes?

    A good way to start is to identify all the "things" or nouns in your design. Good candidates for classes are the nouns: Zone, Weapon, Sword, Player, Cave, Area, WeaponStats, SpellBook, Map, Inventory. The behaviour is often defined in interfaces: Killable, Movable, Searchable. GWT uses a lot of Has* interfaces, which is nice: HasStats, HasHealth, HasInventory.

    You can then connect this all together like this:
    Java Code:
    abstract class Weapon {
       public int getMaxDamage();
       public int getMinDamage();
    }
    
    class Sword extends Weapon {
       public int getMaxDamage() {
          return 30;
       }
       public int getMinDamage() {
          return 20;
       }
    }
    
    class Player implements HasHealth, HasInventory {
       private Inventory;
       private int health;
    
       public int getHealth() {
          return health;
       } 
    
       public Inventory getInventory() {
          return inventory;
       }
    }
    
    public class Orc extends Enemy implements HasHealth, Killable {
       private int health;
    
       public void doDamage(int amount) {
          this.health -= amount;
       }
    
       public int getHealth() {
          return health;
       } 
    }
    Then create a game engine that uses these objects. You can go crazy when it comes to creating stuff like this. And I can guarantee that your design will change. A lot.
    gimbal2 and BigBlueBuffalo like this.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

Similar Threads

  1. Replies: 0
    Last Post: 07-26-2013, 05:04 AM
  2. Replies: 0
    Last Post: 12-02-2011, 04:56 PM
  3. Multiple classes
    By Lund01 in forum New To Java
    Replies: 1
    Last Post: 03-16-2011, 02:58 PM
  4. help with GUI and multiple classes
    By sssss in forum Advanced Java
    Replies: 14
    Last Post: 01-16-2011, 10:08 PM
  5. Design of classes
    By cselic in forum New To Java
    Replies: 24
    Last Post: 06-10-2010, 11:18 AM

Posting Permissions

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