📄 adventuretextui.java
字号:
package ope.adventure.ui;
import java.util.Scanner;
import ope.adventure.Area;
import ope.adventure.Adventure;
import ope.adventure.Player;
/**
* This class contains a fully text-based user interface for the
* Adventure game project. The UI reads its input from the console
* using a <CODE>Scanner</CODE> object.
*
* @see ope.adventure.ui.AdventureGUI
* @see java.util.Scanner
*/
public class AdventureTextUI {
private Adventure adventure; // fixed value: a session only has one adventure and ends when that one finishes
private Player player; // fixed value
private Scanner keyboard; // fixed value: used to read keyboard input
/**
* Sets up a new user interface and starts a new adventure
* from the beginning.
*/
public AdventureTextUI() {
this.adventure = new Adventure();
this.player = this.adventure.getPlayer();
this.keyboard = new Scanner(System.in);
}
/**
* Runs the user interface. First, a welcome message
* is displayed. Then the user is repeatedly asked to
* play a turn until the adventure is over. Finally,
* a goodbye message is printed.
*/
public void run() {
System.out.println(this.adventure.getWelcome());
while (!this.adventure.isOver()) {
this.printAreaInfo();
this.playTurn();
}
System.out.println("\n" + this.adventure.getGoodbye());
}
/**
* Prints a description of the player's current location.
*/
private void printAreaInfo() {
Area area = this.player.getLocation();
System.out.println("\n\n" + area.getName());
System.out.println(area.getName().replaceAll(".", "-"));
System.out.println(area.getFullDescription() + "\n");
}
/**
* Lets the user play a turn: asks for a command,
* executes it, and prints out the consequences.
*/
private void playTurn() {
System.out.println();
System.out.print("Command: ");
String command = this.keyboard.nextLine();
String turnReport = this.adventure.playTurn(command);
System.out.println(turnReport);
}
/**
* Creates a user interface and starts it.
*/
public static void main(String[] arguments) {
AdventureTextUI ui = new AdventureTextUI();
ui.run();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -