Computer Help and Spyware Removal Computer Help and Spyware Removal Computer Help and Spyware Removal Computer Help Forums Windows Startup Programs Database Spyware and Malware Removal Guides Computer Tutorials Uninstall Database File Database Computer Glossary Computer Resources
 

Welcome Guest ( Log In | Click here to Register a free account now! )



Register a free account to unlock additional features at BleepingComputer.com
Welcome to Bleeping Computer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.
Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

 
Closed TopicStart new topic
> Simple Java Text-based Game
Ryan 3000
post Mar 6 2008, 05:38 PM
Post #1


Distinguished Member
*****

Group: Members
Posts: 660
Joined: 2-June 07
From: Maryland
Member No.: 134,342



I've hit a roadblock in creating a text-based adventure game in Java for my programming class. The roadblock I've hit is that I've made classes called 'World,' 'Area', and 'Thing,' all representing different aspects of the game. World is only a driver, but 'Area' represents the current physical area the person is in in the adventure game. 'Thing' is any object that the player can interact with. The problem I am having with this is that each 'Area' object has a method called command, which picks up the command that a person has typed in using Scanner. After the game sifts out what command was given, like move, open or take object, it asks the player for which object to interact with. At this point, the scanner only takes in Strings, which don't compare with Object names. Basically, I want an array of objects that compares with a string to see if any object has the name as the value of the string, but I can't. Code's below. I'm not very code-savvy, so please keep that in mind in your answers. Yes, I know that burning an oil field is probably more efficient than my code, but I'm worried about it working for now.

To make a long story short:
I need the command String taken in to be used as a method call and the object name given to be used as the object that executes the method.
(user-inputted object in string format).(user-inputted command in string format);

CODE
public class World
{
    Area CurrentArea;
    Area PreviousArea;
    Scanner in = new Scanner(System.in);
    public void goToArea(Area whichArea)
    {
        PreviousArea = CurrentArea;
        CurrentArea = whichArea;
    }
}


public class Area
{
    Scanner in = new Scanner(System.in);
    String description;//tells the person about the area, like "you find yourself in a dungeon" or stuff like that
    String name;
    boolean canWarehouse;//please don't comment on this stupidly long list
    boolean canRoof;
    boolean canBasement;
    boolean canRoom;
    boolean canDocks;
    boolean canBoat;
    boolean canShip;
    boolean canBridge;
    boolean canUtil;
    boolean canHeater;
    public Area(String aDescription, String aName, boolean warehouse, boolean roof, boolean basement, boolean pirateRoom, boolean docks, boolean ship, boolean boat, boolean bridge, boolean utilityRoom, boolean heaterRoom)//i know this is way inefficient
    {
        description = aDescription;
        name = aName;
        canWarehouse = warehouse;
        canRoof = roof;
        canBasement = basement;
        canPirateRoom = pirateRoom;
        canDocks = docks;
        canShip = ship;
        canBoat = boat;
        canBridge = bridge;
        canUtil = utilityRoom;
        canHeater = heaterRoom;
    }
    public void command(String command)
    {
        if (command == "examine")
        {
            System.out.println("What would you like to examine?");
            String jimmy = in.next();
            //now what? I want to reference all existing objects here to the String named jimmy to find any matches
        }
    }
}



public class Thing
{
    String name;
    String containerName;//which area it is in
    boolean taken = false;//did the person pick it up
    boolean takeable = false;//can the person pick it up
    boolean local = false;//is it in the current area
    boolean moveable = false;//can it be moved
    boolean openable = false;//can it be opened
    boolean attackable = false;//can it be attacked
    String examine;//what is the object's description when examined
    
    
    public Thing(String aName,String aContainer,boolean isTakeable,boolean isLocal,boolean isMoveable,boolean isOpenable,boolean isAttackable,String isExaminable)
    {
           containerName = aContainer;
           name = aName;
           takeable = isTakeable;
           local = isLocal;
           moveable = isMoveable;
           openable = isOpenable;
           attackable = isAttackable;
           examinable = isExaminable;
    }
    public void getObject()
    {
        if (takeable && local)
        {
               taken = true;
        }
        else
        {
            System.out.println("You cannot get this object");  
        }
    }
    public boolean getTaken()//checks if an object the user is trying to use has been picked up yet
    {
        return taken;  
    }
    public String getContainer()//returns the object's area it is in
    {
        return containerName;  
    }
}


Thanks a lot for any help. I'm guessing this knowledge is very important to all Java coding, turning a String into an object's name or command.

This post has been edited by Ryan 3000: Mar 6 2008, 05:43 PM
Go to the top of the page
 
+Quote Post
Billy O'Neal
post Mar 6 2008, 05:41 PM
Post #2


Multi Megaton Malware Munition
******

Group: HJT Team
Posts: 4,017
Joined: 17-January 08
From: Northfield, Ohio
Member No.: 184,215



Have you tried casting the object? I dont know if Java does that but....

Billy3


--------------------
The forum is always a busy place. In the event I fail to reply within twenty-four hours, feel free to send me a PM.
Have I helped you? If so, please say so in My Guestbook.
Go to the top of the page
 
+Quote Post
Ryan 3000
post Mar 6 2008, 05:49 PM
Post #3


Distinguished Member
*****

Group: Members
Posts: 660
Joined: 2-June 07
From: Maryland
Member No.: 134,342



I don't really know what it means to cast something. We are learning our vocabulary from a very narrow curriculum. Can you explain?
Go to the top of the page
 
+Quote Post
Billy O'Neal
post Mar 6 2008, 05:52 PM
Post #4


Multi Megaton Malware Munition
******

Group: HJT Team
Posts: 4,017
Joined: 17-January 08
From: Northfield, Ohio
Member No.: 184,215



Could you do this:

//pseudocode
foreach tmpItem as thing in Area.things {
if tmpItem.getname() == UserInput {
return tmpItem
}
}

Billy3


--------------------
The forum is always a busy place. In the event I fail to reply within twenty-four hours, feel free to send me a PM.
Have I helped you? If so, please say so in My Guestbook.
Go to the top of the page
 
+Quote Post
Billy O'Neal
post Mar 6 2008, 05:55 PM
Post #5


Multi Megaton Malware Munition
******

Group: HJT Team
Posts: 4,017
Joined: 17-January 08
From: Northfield, Ohio
Member No.: 184,215



What are you holding all the references to the Thing objects in? What variable?

Billy3


--------------------
The forum is always a busy place. In the event I fail to reply within twenty-four hours, feel free to send me a PM.
Have I helped you? If so, please say so in My Guestbook.
Go to the top of the page
 
+Quote Post
Ryan 3000
post Mar 6 2008, 05:58 PM
Post #6


Distinguished Member
*****

Group: Members
Posts: 660
Joined: 2-June 07
From: Maryland
Member No.: 134,342



I believe this is the gist of what you are saying, and I know I'm doing something wrong, but I've never seen this before and can't troubleshoot it. Can you help?

CODE
for Area.Thing{if Thing.getName() == jimmy}{return Thing;}
"(" expected


QUOTE
What are you holding all the references to the Thing objects in? What variable?

So are you saying that I need to make an Array that contains the name of every Thing? I've never made a variable to contain all of the references. As I said, we are not far into object-to-object pre-determined interactions. I have no idea what is going on with this game but I'm trying to learn as quickly as I can.

This post has been edited by Ryan 3000: Mar 6 2008, 06:04 PM
Go to the top of the page
 
+Quote Post
Billy O'Neal
post Mar 6 2008, 06:06 PM
Post #7


Multi Megaton Malware Munition
******

Group: HJT Team
Posts: 4,017
Joined: 17-January 08
From: Northfield, Ohio
Member No.: 184,215



No, what is the thing instance called?
You need an array of Things inside of Area.
I see now. You cannot just ask Java for all the refrences to a particular type. You have to hold those refrences yourself, in an array, or explicitly declared.

Your problem is that you are refrencing a Type. A type specifies the format for a data structure, you need an array that holds the refrences to each Object. Otherwise, it is impossible to access the objects.

In other words, how are you creating "thing" objects in the program?

Billy3

Edit:
You cant do this:
//now what? I want to reference all existing objects here to the String named jimmy to find any matches
where are you storing the things?

This post has been edited by Billy O'Neal: Mar 6 2008, 06:07 PM


--------------------
The forum is always a busy place. In the event I fail to reply within twenty-four hours, feel free to send me a PM.
Have I helped you? If so, please say so in My Guestbook.
Go to the top of the page
 
+Quote Post
Ryan 3000
post Mar 6 2008, 06:13 PM
Post #8


Distinguished Member
*****

Group: Members
Posts: 660
Joined: 2-June 07
From: Maryland
Member No.: 134,342



The original plan was to just have a big pile of Thing initializers in the World class, but i can see that will not work. So if I have all of the Things ready-made before the program starts, I can make an array of them directly after initializing them?
As if to say:
CODE
Thing[] bob;
Array bob = new Array(10);
Thing ax = new Thing(...);
Thing crowbar = new Thing(...);
bob[0] = axe;
bob[1] = crowbar;

later on....
for World.bob{if Thing.getName() == jimmy}{return Thing;}

Can you offer me the refinements of the above line so that I can perfectly understand how to reference an array?

This post has been edited by Ryan 3000: Mar 6 2008, 06:15 PM
Go to the top of the page
 
+Quote Post
Billy O'Neal
post Mar 6 2008, 06:14 PM
Post #9


Multi Megaton Malware Munition
******

Group: HJT Team
Posts: 4,017
Joined: 17-January 08
From: Northfield, Ohio
Member No.: 184,215



Sorry, Im having trouble describing what I mean.

When you declare the thing, no instances of it are created. Later in your code, you have to have something like:

thing torch;

which creates an instance of thing. Then the thing just becomes another data type like double or int or string. Just as you cannot ask Java for all the Doubles declared in your program, so too can you not ask for all the instance members of thing.

Inside of area, you need to have an array, which contains Things.

Here is an example (sorry its in vb, but what can I do?)
CODE
<Xml.Serialization.XmlInclude(GetType(OsOp))> <System.Serializable()> Public Class map
    Private oses As New ArrayList
    Public selected As Boolean
    Public name As String
    Public discription As String
    Public Sub toggle()
        selected = Not selected
    End Sub
    Public Sub addOsOp(ByVal item As OsOp)
        oses.Add(item)
    End Sub
    Public Function getOsOp(ByVal toString As String) As OsOp
        For Each item As OsOp In oses
            If item.ToString = toString Then
                Return item
            End If
        Next
        Return Nothing
    End Function
    Public Sub deleteOsOp(ByVal item As String)
        Dim tmp As New OsOp
        tmp = getOsOp(item)
        oses.Remove(tmp)
    End Sub
End Class


Note that class osop is another class I have created, and I have to keep references to it, just as to an int or string.

Billy3


--------------------
The forum is always a busy place. In the event I fail to reply within twenty-four hours, feel free to send me a PM.
Have I helped you? If so, please say so in My Guestbook.
Go to the top of the page
 
+Quote Post
Billy O'Neal
post Mar 6 2008, 06:17 PM
Post #10


Multi Megaton Malware Munition
******

Group: HJT Team
Posts: 4,017
Joined: 17-January 08
From: Northfield, Ohio
Member No.: 184,215



Sorry, here is osop, if that makes it any easier

CODE
Public Class OsOp
    '
    'This class is used to hold the operations that apply to one os inside of the more general map files
    'Features:
    '   Single Files
    '   Directories
    '   File Extentions (Search the entire disk for them)
    Public files As New ArrayList
    Public directories As New ArrayList
    Public extentions As New ArrayList
    Public fromOS As New ArrayList
    Public Sub New()
    End Sub
    Public Sub New(ByVal old As OsOp)
        'HACK HACK HACK
        'If I could figure out how to make it go by value when this is passed between functions I wouldnt need to do this hack;)
        'Considering the # of times that this object is dealt with, it should not be a problem
        '
        Dim str As New IO.MemoryStream
        Dim xml As New Xml.Serialization.XmlSerializer(GetType(OsOp))
        xml.Serialize(str, old)
        Dim tmp As OsOp
        str.Seek(0, IO.SeekOrigin.Begin)
        tmp = CType(xml.Deserialize(str), OsOp)
        str.Close()
        files = tmp.files
        directories = tmp.directories
        extentions = tmp.extentions
        fromOS = tmp.fromOS
    End Sub
    Public Overrides Function ToString() As String
        fromOS.Sort()
        Dim tmp As String = ""
        For Each os As String In fromOS
            tmp += os + ", "
        Next
        tmp = tmp.Substring(0, tmp.LastIndexOf(","))
        Return tmp
    End Function
    Public Shared Operator +(ByVal one As OsOp, ByVal two As OsOp) As OsOp
        Dim returnVar As New OsOp
        returnVar.directories.AddRange(one.directories)
        returnVar.directories.AddRange(two.directories)
        returnVar.extentions.AddRange(one.extentions)
        returnVar.extentions.AddRange(two.extentions)
        returnVar.files.AddRange(one.files)
        returnVar.files.AddRange(two.files)
        returnVar.fromOS.AddRange(one.fromOS)
        returnVar.fromOS.AddRange(two.fromOS)
        Return returnVar
    End Operator
End Class


Billy3


--------------------
The forum is always a busy place. In the event I fail to reply within twenty-four hours, feel free to send me a PM.
Have I helped you? If so, please say so in My Guestbook.
Go to the top of the page
 
+Quote Post
Ryan 3000
post Mar 6 2008, 06:18 PM
Post #11


Distinguished Member
*****

Group: Members
Posts: 660
Joined: 2-June 07
From: Maryland
Member No.: 134,342



I think the only barrier between our understanding now is that I don't know how to access an array and reference each variable in it. Can you tell me how to do this in Java? VB looks very similar but I can't make any comparisons between your code and mine.

This post has been edited by Ryan 3000: Mar 6 2008, 06:20 PM
Go to the top of the page
 
+Quote Post
groovicus
post Mar 6 2008, 06:22 PM
Post #12


Hail Groovicus!
******

Group: Site Admin
Posts: 5,992
Joined: 5-June 04
From: Vermillion, SD
Member No.: 689



All languages are capable of casting... err.. all real programming languages..... err... all modern programming languages....

Let me see if I can wrap my head around what you are asking. You have objects that are in some way manipulable, or is it manipulatable? Anyway, you want it so that when a player requests an action on an object, that you can check the object and see if that is a valid action to be taken on that object (thing)? Something along the lines of 'ball.throw", or something like that?

Somehow, you need to keep a collection of all objects in the 'area', let's call it an arena, so that when a player is in one particular spot in the arena, you can get a list of objects that are in that one area, even if it is just one object. Then once you have that list of objects. you want to act on them.
(I might mention at this point that this is a very difficult project. I do not know what your capabilities are.. might I suggest a nice dice game? smile.gif Just kidding)

I'm pretty sure I can answer your question, you will just have to bear with me a bit, because I am going to get there in a roundabout way.

For starters, you should consider that you really have 'props', not 'things'. A thing can be a floor, a tree, or air, and it is not descriptive of the objects. As with any prop, it should have a name. It should have a list of acceptable actions. That's it. When you get to whatever location in the arena, you retrieve the list of objects (or object) found in that area, iterate through the list of objects, and call their getName() method. So you might have a list that includes ball, chair, and dingo. Now perhaps the player types in "Dingo kick". You application would take that string, and look for a 'prop' who's getName() method returned Dingo. Once you have that object, then you call its getActions() to get a list of things you can do to that Dingo. Then you have to search through that list to see if "kick" is in that list. If it is, then you want to call the kick() method of that object, which will then presumably return a string like "The Dingo is crying'" or something like that.

You can not do something like this (which is what I think you might be trying to do):
Prop ball= new Prop();
String s = "ball kick"; <-- skipping the scanner stuff and assuming this is what the user typed in, for example
s(); <-- call your object this way

Did I just completely confuse you?


--------------------
Go to the top of the page
 
+Quote Post
Billy O'Neal
post Mar 6 2008, 06:22 PM
Post #13


Multi Megaton Malware Munition
******

Group: HJT Team
Posts: 4,017
Joined: 17-January 08
From: Northfield, Ohio
Member No.: 184,215



Not quite:
Thing[] bob;
Array bob = new Array(10);
Thing ax = new Thing(...);
Thing crowbar = new Thing(...);
bob[0] = axe;
bob[1] = crowbar;

later on....
for World.bob{if Thing.getName() == jimmy}{return Thing;}

You cannot access thing in the later method, because Thing is a type. At the end you'd need something like this:

for (int x, 0, bob.count()) {
if bob[x].getName()==jimmy {
return bob[x];
}}


--------------------
The forum is always a busy place. In the event I fail to reply within twenty-four hours, feel free to send me a PM.
Have I helped you? If so, please say so in My Guestbook.
Go to the top of the page
 
+Quote Post
groovicus
post Mar 6 2008, 06:24 PM
Post #14


Hail Groovicus!
******

Group: Site Admin
Posts: 5,992
Joined: 5-June 04
From: Vermillion, SD
Member No.: 689



Before anybody else pitches in, accessing an array is as simple as:
String[] a = new String[];
a[0]; <--references the first element
a[1]; <-- references the second element



--------------------
Go to the top of the page
 
+Quote Post
Ryan 3000
post Mar 6 2008, 06:25 PM
Post #15


Distinguished Member
*****

Group: Members
Posts: 660
Joined: 2-June 07
From: Maryland
Member No.: 134,342




for (int x, 0, bob.count()) {
if bob[x].getName()==jimmy {
return bob[x];
}}

I'm trying this out as you read this, but what does the second parameter, 0, represent?
Go to the top of the page
 
+Quote Post

Closed TopicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



Lo-Fi Version Time is now: 12th October 2008 - 09:52 AM


Advertise   |   About Us   |   Terms of Use   |   Privacy Policy   |   Contact Us   |   Site Map   |   Chat   |   Tutorials   |   Uninstall List
Discussion Forums   |   The Computer Glossary   |   Resources   |   RSS Feeds   |   Startups   |   The File Database   |   Malware Removal Guides

© 2003-2008 All Rights Reserved Bleeping Computer LLC.