Beschreibungen von Objekten variieren

Die Beschreibung eines Objekts wird durch das Attribut description festgelegt, das vom Typ text ist, also quasi ein String. Wenn man als Spieler ein Objekt untersucht oder betrachtet, wird standardmäßig die Beschreibung als Antwort ausgegeben. Manchmal möchte man diese Beschreibung ganz oder teilweise variieren.


Beschreibung abhängig von einem Zustand

Dazu baut man Blöcke mit [if (Bedingung)], [otherwise if (Bedingung)], [otherwise], [end if] in den Text ein.

In diesem Beispiel wird zuerst der Spieler mit den booleschen Attributen “hungrig” und “durstig” versehen, und die Beschreibung des Spielers angepasst an diese Werte. Um das testen zu können, muss man noch etwas zu essen und zu trinken bereitstellen und Regeln für die Änderung des Zustands angeben:

"Hungry and thirsty" by Thomas Rau

The World is a room.
A person can be thirsty.
A person can be hungry.
The player is hungry and thirsty.

The apple and the lemonade are edible things in the World.

After eating the apple:
	now the player is not hungry;
	say "You eat the apple. Not bad." 
After eating the lemonade:
	now the player is not thirsty;
	say "You drink the lemonade. Not bad." 

Instead of drinking something (called the drink), try eating the drink.

The description of the player is "You look [if the player is hungry and the player is thirsty]hungry and thirsty[otherwise if the player is hungry]hungry[otherwise if the player is thirsty]thirsty[otherwise]well fed and full of energy[end if]."

Test me with "x self / eat apple / x self / drink lemonade / x self"

In Java sähe das ganze so aus:

public String getDescription() {
	String answer = "You look ";
	if (isHungry && isThirsty) {
		answer = answer+"hungry and thirsty";
	else if (isHungry) {
		answer = answer+"hungry";
	}
	else if (isThirsty) {
		answer = answer+"thirsty";
	}
	else {
		answer = answer+"well fed and full of energy";
	}
	answer = answer + ".";
	return answer;
}

Eckige Klammern in einem Textstring bedeuten immer, dass hier Code ausgeführt wird. Das sind entweder Kontrollstrukturen wie oben, oder die Bezeichner von Variablen, Objekten oder Methoden.

Alternativ zur Lösung oben könnte man eine say-Methode definieren und aufrufen. So eine Methode bietet sich wie immer dann an, wenn der Codeblock eventuell mehrfach verwendet wird:

The description of the player is "You look [description of your state]."

To say description of your state:
	if the player is hungry and the player is thirsty:
		say "hungry and thirsty";
	otherwise if the player is hungry:
		say "hungry";
	otherwise if the player is thirsty:
		say "thirsty";
	otherwise:
		say "well fed and full of energy".

Wechselnde Beschreibung

Oder man legt eine Reihe verschiedener Varianten fest, die in zufälliger oder nicht zufälliger Weise ausgewählt werden. Das geschieht, in dem man an eine Stelle in der Beschreibung [one of] gefolgt von mehreren durch [or] getrennten Alternativen setzt:

"Magic 8-Ball" by Thomas Rau

The World is a room.

The Magic 8-Ball is a thing in the World. The description is "It looks like a big black pool ball. Through a little window, you can see the message: '[one of]It is certain[or]It is decidedly so[or]Without a doubt[or]Yes – definitely[or]You may rely on it[or]As I see it, yes[or]Most likely[or]Outlook good[or]Yes[or]Signs point to yes[or]Reply hazy, try again[or]Ask again later[or]Better not tell you now[or]Cannot predict now[or]Concentrate and ask again[or]Don't count on it[or]My reply is no[or]My sources say no[or]Outlook not so good[or]Very doubtful [purely at random].'"

Understand "ball" as the 8-Ball.

Test me with "x ball / again / again"

Abgeschlossen wird diese Reihe dann entweder mit

  • [purely at random]
    (zufällige Auswahl)
  • [at random]
    (zufällige Auswahl, aber niemals zweimal hintereinander das gleiche)
  • [then purely random]
    (erst mal in genau der angegebenen Reihenfolge, bis alle mal dran waren, und danach kommt immer ein zufälliges Element)
  • [then at random]
    (erst mal in genau der angegebenen Reihenfolge, danach zufällig, aber nie zweimal hintereinander das gleiche)
  • [sticky random]
    (eines zufällig, aber dann jedesmal dasselbe)
  • [as decreasingly likely outcomes]
    (zufällig, aber mit sinkender Wahrscheinlichkeit – das letzte von n Elementen ist n-mal unwahrscheinlicher als das erste)
  • [cycling]
    (zyklisch der Reihe nach, angefangen beim ersten)
  • [in random order]
    (in zufälliger Reihenfolge solange, bis jedes mal dran war, und dann in neuer zufälliger Reihenfolge wiederholt)
  • [stopping]
    (der Reihe nach alle bis zur letzten Alternative, und danach immer nur diese)

Das alles gilt natürlich nicht nur für das Beschreibungs-Attribut, sondern für jeden beliebigen String.

Schreibe einen Kommentar