Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (582)
games submitted by our members
Games in WIP (503)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
   Home   Help   Search   Login   Register   
  Show Posts
Pages: [1]
1  Game Development / Newbie & Debugging Questions / Re: Java Text RPG on: 2012-02-24 01:39:29
Your entire switch can be expressed as a one-liner:

1  
2  
3  
public boolean containsExit(String exit) {
    return linkedRooms.containsKey(exit.toUpperCase());
}


Now it's not exactly the same logic as above, because it allows for exits other than N/S/E/W/U/D, but that is probably a bonus.

Secondly, now that you're using an unboxed boolean, you no longer need to compare it with .equals(true).  You just say "if currentLocation.containsExit("N"))" instead


Oh my god. You just condensed my 43 lines of code to 3. Freakin' bravo.

Thanks a ton! Cheesy
2  Game Development / Newbie & Debugging Questions / Re: Java Text RPG on: 2012-02-24 01:35:21
Ah. I had though a break; after every case (including default) was just the safe bet sort of thing to do.


Think I've got my return issue figured out. It's a little more involved, and I had to add a boolean to compensate for my use of the switch statement.

Anyone have any suggestions or is this fine for all practical purposes?

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
    public Boolean containsExit( String exit ){
        switch (exit.toUpperCase()){
            case "N":
                if (linkedRooms.containsKey("N"))
                    containsExit = true;
                else
                    containsExit = false;
                break;
            case "S":
                if (linkedRooms.containsKey("S"))
                    containsExit = true;
                else
                    containsExit = false;
                break;
            case "E":
                if (linkedRooms.containsKey("E"))
                    containsExit = true;
                else containsExit = false;
                break;
            case "W":
                if (linkedRooms.containsKey("W"))
                    containsExit = true;
                else
                    containsExit = false;
                break;
            case "U":
                if (linkedRooms.containsKey("U"))
                    containsExit = true;
                else
                    containsExit = false;
                break;
            case "D":
                if (linkedRooms.containsKey("D"))
                    containsExit = true;
                else
                    containsExit = false;
                break;
            default:
                containsExit = false;
        }
       
        return containsExit;    
    }
3  Game Development / Newbie & Debugging Questions / Java Text RPG on: 2012-02-24 01:16:13
Hello, all!

Trying my hand at making a text-based RPG in Java. Just working on getting the framework set up and I'm running into a slight problem.

I'm trying to create a method of a Location class that returns whether or not a room contains a specific exit. My "map" (or list of connected rooms, rather) is stored as a hashmap. Each room/Location object has one that stores the connected rooms in the format ( Direction of Exit, Connected Room).

My method to pull whether or not the room contains a specified exit doesn't work, and I'm not sure where I need to head to be on the right track, here.

This is the method:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
public Boolean containsExit( String exit ){
        switch (exit.toUpperCase()){
            case "N":
                if (linkedRooms.containsKey("N"))
                    return true;
                break;
            case "S":
                if (linkedRooms.containsKey("S"))
                    return true;
                break;
            case "E":
                if (linkedRooms.containsKey("E"))
                    return true;
                break;
            case "W":
                if (linkedRooms.containsKey("W"))
                    return true;
                break;
            case "U":
                if (linkedRooms.containsKey("U"))
                    return true;
                break;
            case "D":
                if (linkedRooms.containsKey("D"))
                    return true;
                break;
            default:
                return false;
                break;
        }


Likewise, in my main class I'm calling the method as such:

1  
2  
3  
4  
5  
    private void moveNorth(){
        if (currentLocation.containsExit("N").equals(true)){
           
        }
    }


My method is informing me that I have an unreachable statement, and that I'm not returning anything (and should be).

Any help would be excellent!

Thanks in advance.
Pages: [1]
 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (72 views)
2014-04-15 18:08:23

BurntPizza (68 views)
2014-04-15 03:46:01

UprightPath (79 views)
2014-04-14 17:39:50

UprightPath (65 views)
2014-04-14 17:35:47

Porlus (80 views)
2014-04-14 15:48:38

tom_mai78101 (104 views)
2014-04-10 04:04:31

BurntPizza (164 views)
2014-04-08 23:06:04

tom_mai78101 (260 views)
2014-04-05 13:34:39

trollwarrior1 (210 views)
2014-04-04 12:06:45

CJLetsGame (220 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!