Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, 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.

Photo

Simple Java Text-based Game


  • This topic is locked This topic is locked
14 replies to this topic

#1 Ryan 3000

Ryan 3000

  • Members
  • 834 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Maryland
  • Local time:11:20 PM

Posted 06 March 2008 - 05:38 PM

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);

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.

Edited by Ryan 3000, 06 March 2008 - 05:43 PM.

No pessimist ever discovered the secrets of the stars, or sailed to an uncharted land.

BC AdBot (Login to Remove)

 


#2 Billy O'Neal

Billy O'Neal

    Visual C++ STL Maintainer


  • Malware Response Team
  • 12,304 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:08:20 PM

Posted 06 March 2008 - 05:41 PM

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

Billy3
Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)
Posted Image

#3 Ryan 3000

Ryan 3000
  • Topic Starter

  • Members
  • 834 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Maryland
  • Local time:11:20 PM

Posted 06 March 2008 - 05:49 PM

I don't really know what it means to cast something. We are learning our vocabulary from a very narrow curriculum. Can you explain?
No pessimist ever discovered the secrets of the stars, or sailed to an uncharted land.

#4 Billy O'Neal

Billy O'Neal

    Visual C++ STL Maintainer


  • Malware Response Team
  • 12,304 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:08:20 PM

Posted 06 March 2008 - 05:52 PM

Could you do this:

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

Billy3
Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)
Posted Image

#5 Billy O'Neal

Billy O'Neal

    Visual C++ STL Maintainer


  • Malware Response Team
  • 12,304 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:08:20 PM

Posted 06 March 2008 - 05:55 PM

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

Billy3
Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)
Posted Image

#6 Ryan 3000

Ryan 3000
  • Topic Starter

  • Members
  • 834 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Maryland
  • Local time:11:20 PM

Posted 06 March 2008 - 05:58 PM

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?

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

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.

Edited by Ryan 3000, 06 March 2008 - 06:04 PM.

No pessimist ever discovered the secrets of the stars, or sailed to an uncharted land.

#7 Billy O'Neal

Billy O'Neal

    Visual C++ STL Maintainer


  • Malware Response Team
  • 12,304 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:08:20 PM

Posted 06 March 2008 - 06:06 PM

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?

Edited by Billy O'Neal, 06 March 2008 - 06:07 PM.

Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)
Posted Image

#8 Ryan 3000

Ryan 3000
  • Topic Starter

  • Members
  • 834 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Maryland
  • Local time:11:20 PM

Posted 06 March 2008 - 06:13 PM

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

Edited by Ryan 3000, 06 March 2008 - 06:15 PM.

No pessimist ever discovered the secrets of the stars, or sailed to an uncharted land.

#9 Billy O'Neal

Billy O'Neal

    Visual C++ STL Maintainer


  • Malware Response Team
  • 12,304 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:08:20 PM

Posted 06 March 2008 - 06:14 PM

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?)
<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
Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)
Posted Image

#10 Billy O'Neal

Billy O'Neal

    Visual C++ STL Maintainer


  • Malware Response Team
  • 12,304 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:08:20 PM

Posted 06 March 2008 - 06:17 PM

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

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
Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)
Posted Image

#11 Ryan 3000

Ryan 3000
  • Topic Starter

  • Members
  • 834 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Maryland
  • Local time:11:20 PM

Posted 06 March 2008 - 06:18 PM

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.

Edited by Ryan 3000, 06 March 2008 - 06:20 PM.

No pessimist ever discovered the secrets of the stars, or sailed to an uncharted land.

#12 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:09:20 PM

Posted 06 March 2008 - 06:22 PM

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? :thumbsup: 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?

#13 Billy O'Neal

Billy O'Neal

    Visual C++ STL Maintainer


  • Malware Response Team
  • 12,304 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:08:20 PM

Posted 06 March 2008 - 06:22 PM

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];
}}
Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)
Posted Image

#14 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:09:20 PM

Posted 06 March 2008 - 06:24 PM

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

#15 Ryan 3000

Ryan 3000
  • Topic Starter

  • Members
  • 834 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Maryland
  • Local time:11:20 PM

Posted 06 March 2008 - 06:25 PM

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?
No pessimist ever discovered the secrets of the stars, or sailed to an uncharted land.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users