新人求机器人未果,干脆自己写一个java版本客户端,只有打印控制台,没有图形界面,但是机器人好写多了
优点:不用那个前宽后窄、左重右轻的mud脚本,就没有任何限制,直接用java写,理论上可以写出任何强大的机器人,而且作为一 个 程序员你把这代码在开发工具里面一跑,没人知道你在做啥....
缺点:所有的机器人都要从头写...
我把代码贴下,欢迎同行补充机器人啊!很简单,就两个类:
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.lang.reflect.Method;
import java.net.Socket;
public class SocketClient implements Runnable {
static Socket client;
private static String robotMethodName = null;
private static String printConsonleFlag = "true";
public static void main(String[] args) throws Exception {
client = new Socket("pkuxkx.com", 8080);
Thread reader = new Thread(new SocketClient());
reader.start();
PrintWriter out = new PrintWriter(client.getOutputStream());
boolean flag = true;
while (flag) {
BufferedReader wt = new BufferedReader(new InputStreamReader(System.in));
String keyIn = wt.readLine();
// mySet setRobotMethodName newBieDaZuo
// mySet setPrintConsonleFlag false
if (keyIn.startsWith("mySet")) {
String comman[] = keyIn.split(" ");
String setArg[] = { comman[2] };
invokeMethod(SocketClient.class, comman[1], setArg);
} else {
out.println(keyIn);
out.flush();
}
// end while
}
client.close();
}
public void run() {
try {
InputStream inputStream = client.getInputStream();
int ret_read;
String robotName = "";
String serverContent = "";
String command = "";
Object tmpObj = null;
byte[] buff = new byte[1024];
while (true) {
ret_read = inputStream.read(buff);
if (ret_read > 0) {
// s = new String(buff, 0, ret_read,"gbk").trim();
serverContent = new String(buff, 0, ret_read).trim();
serverContent = serverContent
.replaceAll(
"[(\\[\\d{1,2};\\d{1,2}m)|(\\[\\d{1,2};\\d{1,2};\\d{1,2}m)|(\\[\\d{1,2}m)]",
"");
if ("true".equals(printConsonleFlag))
System.out.print(serverContent);
robotName = getRobotMethodName();
if (robotName != null && robotName.length() > 0) {
String[] _tmp = { serverContent };
tmpObj = invokeMethod(Robot.class, robotName, _tmp);
if (tmpObj != null && tmpObj instanceof String)
command = (String) tmpObj;
}
if (command != null && command.length() > 0) {
PrintWriter out = new PrintWriter(client
.getOutputStream());
out.println(command);
out.flush();
command = "";
}
// s = s.replaceAll("\\[\\d{1,2};\\d{1,2}m","");
// System.out.print(s);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static Object invokeMethod(Class ownerClass, String methodName,
Object[] args) throws Exception {
Class[] argsClass = new Class[args.length];
for (int i = 0, j = args.length; i < j; i++) {
argsClass = args.getClass();
}
Method method = ownerClass.getMethod(methodName, argsClass);
return method.invoke(ownerClass.newInstance(), args);
}
public static String getRobotMethodName() {
return robotMethodName;
}
public static void setRobotMethodName(String robotMethodName) {
System.out.println("now use robot :"+robotMethodName);
SocketClient.robotMethodName = robotMethodName;
}
public static String getPrintConsonleFlag() {
return printConsonleFlag;
}
public static void setPrintConsonleFlag(String printConsonleFlag) {
System.out.println("now print ponsonle is :"+printConsonleFlag);
SocketClient.printConsonleFlag = printConsonleFlag;
}
}
public class Robot {
public String newBieDaZuo(String serverContent) throws Exception {
String backValue="";
if (serverContent.indexOf("你发呆了一会儿,发现自己居")!=-1) {
Thread.sleep(1000);
backValue = "dazuo 11";
}
if (serverContent.indexOf("你运功完毕,深深吸了口")!=-1) {
backValue = "dazuo 11";
}
if (serverContent.indexOf("你现在的气太少了,无法产生")!=-1) {
backValue = "fadai";
}
return backValue;
}
}
需要补充的就是这个机器人类,欢迎补充啊,路径可以考虑写一个文本,方便维护。 |