Solved

The Sorcerer's Cave game Java

Posted on 2014-04-15
11
992 Views
Last Modified: 2014-04-22
I am working on a program for one of my courses and am having difficulty with certain parts.  The Sorcerer’s Cave game:  Our goal with this project is to… First, implement a solid GUI interface to control and display the results of this program.  Read a text data file, instantiate appropriate classes, create an internal data structure (multi-tree), display the structure in a convincing way using a JTextArea, and finally to search the structure (by index name and type).
                I am having issues determining how to search my program.  I have my information stored in ArrayLists.  The point of this project is not to do the best possible way, but to build this program over the course of the semester.  In this project we are supposed to iterate through the arraylist to find the required information, a slow but effective method.  
What is the best way to implement this type of searching into my program?  Will I store the search text as a string and compare that string to each element of the array list(s)?  Would something like myList.indexOf(string) work for these purposes?  Where string is the user input search term.  Then I can use that index generated by indexOf to generate the searched items?  Any help will be greatly appreciated.
0
Comment
Question by:Autkast
  • 4
  • 2
  • 2
  • +2
11 Comments
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
What is the best way to implement this type of searching into my program?
"Best" is subjective. What do *you* mean by "best"?
0
 

Author Comment

by:Autkast
Comment Utility
Ah good point.  The simplest method of implementing a search into this program using ArrayList would be a better description of what I'm asking.
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
So are you maintaining multiple ArrayLists, one for each kind of datum? If so, are you correlating each element to its related information by way of its index within each ArrayList?
0
 
LVL 37

Accepted Solution

by:
Bing CISM / CISSP earned 250 total points
Comment Utility
What is the best way to implement this type of searching into my program?

> Read a text data file, instantiate appropriate classes, create an internal data structure (multi-tree)

i guess you actually already give the answer. :-)

if the data entries are only required to be searched in memory, you no need to worry about how to to store them in an external file. however, you do need to find a way to store data in a multi-tree. commonly this can be done by a modified Binary Tree though sometimes additional structure members are required for best describing the parents, brothers and children nodes.

Will I store the search text as a string and compare that string to each element of the array list(s)?

a tree can be stored in an array list, only if necessary.
0
 
LVL 26

Expert Comment

by:dpearson
Comment Utility
If you know what you want to search for (index name and type) then the fastest solution would be to build a HashMap as you build the multi-tree.

You'd have something like:

// A class I assume you'll create
MultiTree myMultiTree = new MultiTree() ;

Map<String, Object> mapByName = new HashMap<String, Object>() ;
Map<String, Object> mapByType = new HashMap<String, Object>() ;

Everytime you add a node to the tree, also add it to the HashMaps as well:

public void addToTree(Object node, String name, String type) {
   myMultiTree.add(node) ;
   mapByName.put(name, node) ;
   mapByType.put(type, node) ;
}

Then you can search by name or type:

public Object findByName(String name) {
   return mapByName.get(name) ;
}

public Object findByType(String name) {
   return mapByType.get(name) ;
}

If name and type aren't unique, then rather than storing a single value, you should store a list of values with that name or type.  Then to search you'd retrieve the list of matches.

A lookup from a HashMap is constant time - i.e. much faster than anything you'd write to search the multi tree.

Doug
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:Autkast
Comment Utility
@kaufmed:
Yes, I am maintaining multiple ArrayLists, party, members, treasure, and artifacts.  In my program to this point they are used very simply.  The data is read from the text file, based off a switch, the text line will be stored in a new ArrayList.  This information is then split inside of the method it calls into various information (index, name, etc.).  I am attaching some example code:

switch(line.charAt(0)) {
                        case 'P':
                        case 'p':
                            addParty(sLine);
                            break;

Open in new window


// adding parties
    void addParty(Scanner input) {
        String st = input.nextLine();
        cave.parties.add(new Party(st));
        jta.append(st + "\n");
    }  // end addParty

Open in new window


//  party constructor to use specified variables
    public Party(String input) {
        
        String newLine[] = input.split("\\s*:\\s*");
        
        // assign information to variable name and index
        index = Integer.parseInt(newLine[1]);
        name = newLine[2];
        
        System.out.println(toString());
    }  // end party constructor

Open in new window

0
 

Author Comment

by:Autkast
Comment Utility
@dpearson:
Unfortunately using a HashMap is in the next project.  This project uses ArrayLists, however once I complete this part this information will be invaluable, thank you.
0
 
LVL 35

Assisted Solution

by:mccarl
mccarl earned 250 total points
Comment Utility
Ok, so since your ArrayList is store a custom object (Party in the above code), then you won't be able to use indexOf() to search through the List. So the alternative is a little bit more manual work, something like this...
String searchTerm = "blah";
Party locatedParty = null;
for (Party party : cave.parties) {
    if (searchTerm.equals(party.getName()) {
        locatedParty = party;
        break;
    }
}
if (locatedParty != null) {
    // It was found, do something with it
}

Open in new window

(Note: a few assumptions have been made about details that I was NOT able to get from your examples above, but it should give you the idea)
0
 

Author Closing Comment

by:Autkast
Comment Utility
Thank all of you for the assistance.  While reviewing the comments I had an epiphany that I was not using the ArrayLists to their fullest potential.  Once I redesigned how I was reading in the provided data the search solution became much more simple.  Basically I was not properly adding the creatures to the party or treasures/artifacts to the creatures.  I knew it was something obvious and right under my nose.
0
 
LVL 35

Expert Comment

by:mccarl
Comment Utility
Glad that you were able to get a solution! :)
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
topping2 challenge 13 55
Magic Software info 18 100
thymeleaf natural templating vs JSP 2 22
eclipse java  build path 6 16
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now