52 lines
1.5 KiB
Java
52 lines
1.5 KiB
Java
package com.github.dtschust.zork.utils;
|
|
|
|
import java.io.File;
|
|
import java.io.FileNotFoundException;
|
|
import java.util.Scanner;
|
|
|
|
/**
|
|
* CommandReader reads the .txt file containing expected input and output
|
|
*/
|
|
public class CommandReader{
|
|
private String instruction;
|
|
private static Scanner scanner;
|
|
|
|
public enum Type{ SEND, RECV, END }
|
|
|
|
/** CommandReader Constructor
|
|
* @param filename file containing command sent (with leading ">") and expected responses
|
|
*/
|
|
public CommandReader(String filename) {
|
|
try {
|
|
scanner = new Scanner(new File(filename));
|
|
} catch (FileNotFoundException e){
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
/** Load the next instruction (overwrite current instruction) and detect its type
|
|
* @return Type of the next instruction:
|
|
* - END if the game ended
|
|
* - SEND if it's th command to send
|
|
* - RECV if it's a game output
|
|
*/
|
|
public Type getInstructionType(){
|
|
if(!scanner.hasNextLine())
|
|
return Type.END;
|
|
instruction = scanner.nextLine();
|
|
if(instruction.startsWith(">")) {
|
|
instruction = instruction.substring(1);
|
|
return Type.SEND;
|
|
}
|
|
|
|
return Type.RECV;
|
|
}
|
|
|
|
/** Returns a text line (can be both an input or output depending on the Type)
|
|
* @return The next text line
|
|
*/
|
|
public String getInstruction() {
|
|
return instruction;
|
|
}
|
|
}
|