#Java textbased game.

82 messages · Page 1 of 1 (latest)

slow sundial
#

This assigntment is almost done. The problem that me and my group is facing is about the doors of each room. For an example, in the first Room the player only has one option and it's [s] and in the next room there is both [s] and [e].

How do we make a arraylist of rooms in the class room and connect it with the main method for each room? We tried if statements but didn't work...or perhaps it does. How do we connect the doors with each path for each room? And how do we put a message like "Wrong way!" when you click on the wrong letter. We use n, s, w, e in this game.

Also, every room doesn't have 4 doors.

cursive waveBOT
#

Hey, @slow sundial!
Please remember to /close this post once your question has been answered!

jovial oasis
#

hashmap

#

key pair value

#

the key is a string "s"

#

the value is the door to go to

slow sundial
#

What is hashmap? Our teacher haven't told us anything about it

jovial oasis
#

on user input try to get the value from the hashmap based on the input which serves as the key

#

a hashmap is a key value pair based data structure

#
// Create a HashMap object called capitalCities
    HashMap<String, String> capitalCities = new HashMap<String, String>();

    // Add keys and values (Country, City)
    capitalCities.put("England", "London");
    capitalCities.put("Germany", "Berlin");
    capitalCities.put("Norway", "Oslo");
    capitalCities.put("USA", "Washington DC");
#

this is a String String hashmap

slow sundial
#

Oh, the problem is that we didn't make our game like that

#

we made a arraylist of rooms

#

and the players location

jovial oasis
#

if we do capitalCities.get("USA") then "Washington DC" is returned

#

refactoring your whole program to use this might be hard then

#

and probably not time efficient

slow sundial
#

Yeah

#

The only thing standing in our way right now

#

is the doors

#

the game is pretty much done

#

Do you want to see the code?

jovial oasis
#

you could do something like this

if(playerPosition == 1 && input.equals("s") {
  doorS();
}

sure you can show code

slow sundial
#

alright!

#

1 sec

jovial oasis
#

but my idea is based on the player position which I guess is just stored by an int you can have if statements based on what next method to call

slow sundial
#

So, its a bit hard to zoom out on netbeans to get the whole code

#

but you can find it here

jovial oasis
#

just paste in the text for it if you can

slow sundial
#

sure

jovial oasis
#

oh du ar svensk?

slow sundial
#

we tried to make if statements after the blue lines for each room

#

ja

#

XD

jovial oasis
#

jag ar svensk american

slow sundial
#

Coolt! Trevligt.

#

Vi har suttit med det här problemet hela dagen

#

Spelet är för odynamiskt, jag tror att vi har gjort det svårare än vad det är

#

vi försökte göra if statements för varje rum, men det gick inte

#

eller så är vi bara riktigt korkade

#

package dragontreasuregame;

import java.util.ArrayList;

public class Door {

private boolean locked;
private char position;

public void isLocked(boolean locked) {
ArrayList<String> Door = new ArrayList<String>();
Door.add("doorN");
Door.add("doorS");
Door.add("doorE");
Door.add("doorW");

}




public void setLocked(boolean locked) {
    this.locked = locked;
}

}

jovial oasis
#

well its not a terrible idea the way you did it with the array list of Rooms

slow sundial
#

Kravet för uppgiften var en arraylist

#

vi hade inget val haha

#

men nu sitter vi fast

jovial oasis
#

hmm maybe just use String array list if doing it with Room object complicates it too much

slow sundial
#

Hur menar du?

jovial oasis
#

well what is the problem you are having specifically

slow sundial
#

Skapa en lista med dörrar i klassen door

#

Skaffa en boolean för att få meddelande om vilka dörrar som är låsta.

#

för varje rum

#

alla rum har inte 4 dörrar, text första rummet har bara 1 dörr som går mot syd

#

dörrarna ska kunna läsa läsas in i main

#

som sagt, vi testade if statements

#

gick inte så bra

jovial oasis
#

it doesnt look like a boolean for if the door is locked is ever set when you create new Rooms to the list

#

roomList.add(new Room("play",
"\nDu står utanför en grotta. Det luktar svavel från öppningen",
"\nGrottöppningen är söderut. Skriv "s" och tryck på [Enter] för att komma in i grottan",
-1, 1, -1, -1, true));

you could add an extra field to the door class called locked which is a boolean, and when you create a door you set that parameter to either true or false

slow sundial
#

HMM

#

hmm*

jovial oasis
#

new Room(String text, String text2, String text3, int x, int y, int z, int w, boolean locked);

#

this is the method prototype meaning all of its parameter types

#

what I did was add a locked boolean

#

which is stored in the Room object when you create it

#

so then you can be like if (door.locked == true) when doing level stuff

slow sundial
#

Where do I add this new room

#

in the class room?

jovial oasis
#

see how this works

#

in this example Main class has an int and in its constructor its set to 5

#

so you can do this equivalent with your Door class but for a boolean

slow sundial
#

Alright!

jovial oasis
#

this will be a very easy way to get if a door is locked or not, and just any other properties you want, such as what room it will lead to and all that stuff

slow sundial
#

Do I create this in my main class

#

if I understood this correctly

jovial oasis
#

Door class

slow sundial
#

Alright.

#

Thanks!!

jovial oasis
#

your welcome