Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- /**
- * Class ini adalah bagian dari "World of Zuul".
- * "World of Zuul" sangat simple, game adventure berbasis text.
- *
- * parser ini membaca input pengguna dan mencoba menerjemahkannya sebagai
- * "Adventure" command. Setiap kali dipanggil Class membaca baris dari terminal dan
- * mencoba untuk menerjemahkan baris tersebut sebagai 2 kata command. Class me-return command
- * sebagai objek dari class Command.
- *
- * Parser memiliki sekumpulan kata command yang diketahui.
- * Ia memeriksa input pengguna terhadap command yang diketahui,
- * dan jika input bukan salah dari commands yang diketahui, maka
- * me-return objek command yang ditandai sebagai perintah yang tidak diketahui.
- *
- * @author thomasdwi.a
- * @version 20201115
- */
- public class Parser
- {
- private CommandWords commands; // holds all valid command words
- private Scanner reader; // source of command input
- /**
- * Membuat parser untuk membaca terminal.
- */
- public Parser()
- {
- commands = new CommandWords();
- reader = new Scanner(System.in);
- }
- /**
- * @return Command berikutnya dari pengguna.
- */
- public Command getCommand()
- {
- String inputLine; // akan menampung baris input
- String word1 = null;
- String word2 = null;
- System.out.print("> "); // print prompt
- inputLine = reader.nextLine();
- // Find up to two words on the line.
- Scanner tokenizer = new Scanner(inputLine);
- if(tokenizer.hasNext()) {
- word1 = tokenizer.next(); // mendapat kata pertama
- if(tokenizer.hasNext()) {
- word2 = tokenizer.next(); // mendapat kata kedua
- // note: kami hanya mengabaikan sisa baris input.
- }
- }
- // Sekarang periksa apakah kata ini dikenal. Jika demikian, buat command
- // dengan itu. Jika tidak, buat command "null" (untuk command yang tidak diketahui).
- if(commands.isCommand(word1)) {
- return new Command(word1, word2);
- }
- else {
- return new Command(null, word2);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement