0
votes

Good evening, i'm currently trying to make a short Textadventure my problem is that i dont know how i should connect the GUI with the story. To this point all i did was simple "when this button is pushed, change this label" and stuff like this, but now i have to get the text from the GUI so the story can proceed, and this is where my problem starts. To get the values is easy but i dont know how to work with them correctly, the story should wait for input and then proceed based on the input everything works on the console and i dont know how to go on.

Edit: So my question is how i can use the textfield input to advance in the story there are three things i dont know and would like to learn: 1. put the story on hold until an input is made 2. use this input right to advance in the story 3. show the right story in the label

this are two of my room classes

package Locations;

import java.util.ArrayList;
import java.util.Scanner;
import xmlModul.XmlLoader;

public class GameTile00 extends SuperLocation{
   XmlLoader loader = new XmlLoader();
   GameTile01 next = new GameTile01("Raum2","zweiter Raum");
   //später character für inventar einfügen
   ArrayList<String> items = new ArrayList();

   Scanner s = new Scanner(System.in);

    public GameTile00(String Name, String Beschreibung) {
        super(Name, Beschreibung);
    }

    @Override
    public void Storytelling() {
    story1();    
    }

    public void story1(){
        String story1 = "<html>Du wachst auf in einem Raum der dir gänzlich unbekannt ist.<br>"
                      + "Alles was du siehst ist ein Teller mit Keksen, daneben ein Schlüssel<br>"
                      + "Deine Möglichkeiten sind begrenzt, schließt du die Tür auf, oder isst du die Kekse?(Essen/Aufschließen)";

        System.out.println(story1);
        String eingabe = s.next();
        if(eingabe.equalsIgnoreCase("Essen")){
            System.out.println("Du fühlst dich zwar erfrischt, jedoch hast du jetzt durst und sitzt im Raum...");
        }
        else if(eingabe.equalsIgnoreCase("Aufschließen")){
            System.out.println("du nimmst deinen Mut zusammen und gehst in den nächsten raum.");
            next.Storytelling();

        }
        else{
            System.out.println("hö?");
        }
    }

and

package Locations;

import java.util.ArrayList;
import java.util.Scanner;
import xmlModul.XmlLoader;

public class GameTile01 extends SuperLocation{
   XmlLoader loader = new XmlLoader();
   //später character für inventar einfügen
   GameTile02 west = new GameTile02("Raum west","West Raum");
   GameTile03 ost = new GameTile03("Raum ost","Ost Raum");
   ArrayList<String> items = new ArrayList();

   Scanner s = new Scanner(System.in);

public GameTile01(String Name, String Beschreibung) {
    super(Name, Beschreibung);
}

@Override
public void Storytelling() {
    story1();
    }

public void story1(){
    String story1 = "<html>im neuen raum ist es schwarz doch findest du durch ertasten einen Lichtschalter <br>"
            + "ein leerer raum erstreckt sich mit zwei türen, wohin gehst du? (westen, osten)</html>";

    System.out.println(story1);
    String eingabe = s.next();
    if(eingabe.equalsIgnoreCase("Osten")){
        System.out.println("Was sich wohl im osten befindet");
        ost.Storytelling();
    }
       else if(eingabe.equalsIgnoreCase("Westen")){
        System.out.println("was sich wohl im westen befindet");
        west.Storytelling();
    }
    else{
        System.out.println("hö?");
    }
}

my GUI is atm only a Textfield to get the input, a button to send the input and a Label to show the story

thank you in advance.

1
You don't know how to go on, and so what is your question? - user3437460
i'm sorry i didnt know how i could put it in a question i hope i could make it a little bit clearer - Kyo Akashi
IOC - inversion of control. Take a look at ActionListener. If you utilize that, you don't have to put the story on hold, because you give control to the user. After he made his input, you get the control back plus the made input. You can use this to update the story on the screen. - mike

1 Answers

0
votes

What you ask is not that difficult to realize.

@1. You could use a boolean for this. If it's value is true then the story is running, when it's false then the story is not running. Maybe an eventhandler that sets the boolean to false when the textfield is selected.

@2 Not exactly sure with this, i assume you mean that you test the input value against a list of values, int's or strings, it propably depends on the answer needed.

@3 The Label class has inbuilt functions for that. For this it would be label.setText(string);

I hope this helps you, if you need help with specific code sections then please post that code. If you don't know how to start then look online, there are plenty of tutorials on how to handle button events and things like that with the soul purpose to teach you something.