Robotics first job. Code Description
test environment
- Operating System: windows
- Programming languages: java
topic
Function to achieve
-
About content parsing implementation
-
Test code
-
public class week1 { public static void main(String[] args) throws IOException { // 测试输出referee hear解释是否正确 HearMessage message0 = new HearMessage(100, "referee", "passto(23, 24)"); System.out.println(message0.toString()); // 测试输出ObjName player解释是否正确 ObjInfo objInfo0 = new ObjInfo("player", "hfut1", 2, 23f, 45f, .5f, 1f, 22f, 40f); System.out.println(objInfo0.printToString()); // 测试输出ObjName ball解释是否正确 ObjInfo objInfo1 = new ObjInfo("ball", 23f, 45f, .5f, 1f); System.out.println(objInfo1.printToString()); // 测试输出ObjName player写入文件信息是否正确 ObjInfo objInfo3 = new ObjInfo("player", "hfut1", 2, 23f, 45f, .5f, 1f, 22f, 40f); System.out.println(objInfo0.saveToString()); // 测试输出ObjName ball解释写入文件信息是否正确 ObjInfo objInfo4 = new ObjInfo("ball", 23f, 45f, .5f, 1f); System.out.println(objInfo1.saveToString()); // 测试解释 SeeMessage seeMessage = new SeeMessage(2020, new String[]{"player,hfut1,2,23f,45f,.5f,1f,22f,40f", "ball,23f,45f,.5f,1f", "ball,23f,45f,.5f,1f"}); System.out.println(seeMessage.printToString()); // 测试输出ObjName player写入文件是否正确 ObjInfo objInfo5 = new ObjInfo("player", "hfut1", 2, 23f, 45f, .5f, 1f, 22f, 40f); objInfo0.saveToFile("D:\\Users\\leetu\\Documents\\_documents\\Projects\\fileIOTestFold\\a.txt"); } }
-
-
The results show
-
-
Realization of analytic content written to the file
-
Test code
-
// 测试输出ObjName player写入文件是否正确 ObjInfo objInfo5 = new ObjInfo("player", "hfut1", 2, 23f, 45f, .5f, 1f, 22f, 40f); // 路径可指定 objInfo0.saveToFile("D:\\Users\\leetu\\Documents\\_documents\\Projects\\fileIOTestFold\\a.txt");
-
-
The results show
-
-
Code
package robot;
import java.io.*;
class Sender {
private int property; // 属性
private int direction; // 属性为其他球员时的方向
private int coachDir;
final private String[] coachDirection = {"none", "_left", "_right"}; // 属性为在线教练时的方向
final private String[] senders = {"other player", "self", "referee", "online_coach"};
// 0 - 其他球员 - 返回方向
// 1 - 本人 - 返回 'self'
// 2 - 裁判 - 返回 'referee'
// 3 - 在线教练 - 返回 'online-coach-left(right)'
Sender (int direction) { // 其他球员
property = 0;
this.direction = direction;
}
Sender (String property) {
if (property.equals(senders[1])) {
this.property = 1;
}
else if (property.equals(senders[2])) {
this.property = 2;
}
else if (property.equals(senders[3] + coachDirection[1])) {
this.property = 3;
coachDir = 1;
}
else if (property.equals(senders[3] + coachDirection[2])) {
this.property = 3;
coachDir = 2;
}
}
@Override
public String toString() {
if (property == 0) {
return senders[property] + String.valueOf(direction);
} else if (property == 1 || property == 2) {
return senders[property];
} else if (property == 3) {
return senders[property] + coachDirection[coachDir];
}
return null;
}
}
class HearMessage {
private int time;
private Sender sender;
private String message;
final private String property = "hear";
HearMessage(int time, int direction, String message) {
this.time = time;
sender = new Sender(direction);
this.message = message;
}
HearMessage(int time, String sender, String message) {
this.time = time;
this.sender = new Sender(sender);
this.message = message;
}
@Override
public String toString() {
return "在" + time + "周期" + property + "从" + sender.toString() + "听到了" + message;
}
}
class Player {
// player
private String teamName; // 球队名
private int playerPro;
final private String[] playerProperty = {"TEAMMATE", "OPPONENT"};
private int playerIdx; // 球员序号
Player(String teamName, int playerPro, int playerIdx) {
this.teamName = teamName;
this.playerIdx = playerIdx;
this.playerPro = playerPro;
}
public String saveToString() {
return "OBJECT_" + playerProperty[playerPro] + "_" + String.valueOf(playerIdx);
}
public String printToString() {
return "player " + teamName + String.valueOf(playerIdx);
}
}
class Goal {
// goal
private int goalPro;
final private String[] goalProperty = {"L", "R"};
Goal(int goalPro) {
this.goalPro = goalPro;
}
Goal (String goalPro) {
if (goalPro.equals("l")) {
this.goalPro = 0;
}
else {
this.goalPro = 1;
}
}
public String saveToString() {
return "OBJECT_GOAL" + goalProperty[goalPro];
}
public String printToString() {
return "goal " + goalProperty[goalPro].toLowerCase();
}
}
class Ball {
// ball
public String saveToString() {
return "OBJECT_BALL";
}
public String printToString() {
return "ball";
}
}
class Flag {
// flag
// TO-DO
}
class Line {
// line
private int linePro;
final private String[] lineProperty = {"L", "R", "T", "B"};
Line(int linePro) {
this.linePro = linePro;
}
Line(String linePro) {
for (int i = 0; i < lineProperty.length; i++) {
if (linePro.equals(lineProperty[i].toLowerCase())) {
this.linePro = i;
break;
}
}
}
public String saveToString() {
return "OBJECT_LINE_" + lineProperty[linePro];
}
public String printToString() {
return "line " + lineProperty[linePro].toLowerCase();
}
}
class ObjName {
private int property;
Ball ball;
Goal goal;
Line line;
Player player;
String[] objName = {"BALL", "GOAL", "LINE", "Flag", "PLAYER"};
// objName
// 0 - 球
// 1 - 进球
// 2 - 线
// 3 - 旗
// 4 - 球员
ObjName(String property) {
this.property = 0;
ball = new Ball();
}
ObjName(String property, String pro) {
if (property.equals("goal")) {
this.property = 1;
goal = new Goal(pro);
}
else if (property.equals("line")) {
this.property = 2;
line = new Line(pro);
}
}
ObjName(String property, String teamName, int playerIdx) {
this.property = 4;
if (teamName.equals("hfut1")) {
player = new Player(teamName, 0, playerIdx);
}
else {
player = new Player(teamName, 1, playerIdx);
}
}
public String getName() {
return objName[property];
}
public String saveToString() {
switch (property) {
case 0:
return ball.saveToString();
case 1:
return goal.saveToString();
case 2:
return line.saveToString();
case 3:
// pass
break;
case 4:
return player.saveToString();
default:
break;
}
return null;
}
public String printToString() {
switch (property) {
case 0:
return ball.printToString();
case 1:
return goal.printToString();
case 2:
return line.printToString();
case 3:
// pass
break;
case 4:
return player.printToString();
default:
break;
}
return null;
}
}
class ObjInfo {
ObjName objName;
private float distance;
private float direction;
private float distChng;
private float dirChng;
private float bodyDir;
private float headDir;
ObjInfo(String objName, float distance, float direction, float distChng, float dirChng) {
if (objName.equals("ball")) {
this.objName = new ObjName(objName);
this.distance = distance;
this.direction = direction;
this.distChng = distChng;
this.dirChng = dirChng;
}
}
ObjInfo(String objName, String goalPro, float distance, float direction) {
if (objName.equals("goal")) {
this.objName = new ObjName("goal", goalPro);
this.distance = distance;
this.direction = direction;
}
}
ObjInfo(String objName, String teamName, int playerIdx, float distance, float direction, float distChng,
float dirChng, float bodyDir, float headDir) {
if (objName.equals("player")) {
this.objName = new ObjName(objName, teamName, playerIdx);
this.distance = distance;
this.direction = direction;
this.distChng = distChng;
this.dirChng = dirChng;
this.bodyDir = bodyDir;
this.headDir = headDir;
}
}
public String saveToString() {
if (objName.getName().toLowerCase().equals("ball")) {
return objName.saveToString() + "#" + distance + "-" + direction
+ "-" + distChng + "-" + dirChng;
}
else if (objName.getName().toLowerCase().equals("goal")) {
return objName.saveToString() + "#" + distance + "-" + direction;
}
else if (objName.getName().toLowerCase().equals("player")) {
return objName.saveToString() + "#" + distance + "-" + direction
+ "-" + distChng + "-" + dirChng + "-" + bodyDir
+ "-" + headDir;
}
return null;
}
public String printToString() {
if (objName.getName().toLowerCase().equals("ball")) {
return objName.printToString() + "距离我的Distance是" + distance + ",Direction是" + direction
+ ",DistChng是" + distChng + ",DirChng是" + dirChng;
}
else if (objName.getName().toLowerCase().equals("goal")) {
return objName.printToString() + "距离我的Distance是" + distance + ",Direction是" + direction;
}
else if (objName.getName().toLowerCase().equals("player")) {
return objName.printToString() + "距离我的Distance是" + distance + ",Direction是" + direction
+ ",DistChng是" + distChng + ",DirChng是" + dirChng + ",它的BodyDir是" + bodyDir
+ "和HeadDir是" + headDir;
}
return null;
}
public void saveToFile(String filePath) throws IOException {
FileOutputStream fos0 = new FileOutputStream(new File(filePath), true);
fos0.write(saveToString().getBytes());
fos0.close();
}
// test
public ObjName getObjName() {
return objName;
}
}
class SeeMessage {
private int time;
final private String property = "see";
ObjInfo[] objInfos = new ObjInfo[3];
SeeMessage (int time,String[] strings) {
this.time = time;
String[] tempString;
int len = 0;
for (int i = 0; i < strings.length; i++) {
tempString = strings[i].split(",");
if (tempString[0].equals("ball")) {
objInfos[len] = new ObjInfo("ball", Float.parseFloat(tempString[1]),
Float.parseFloat(tempString[2]), Float.parseFloat(tempString[3]),
Float.parseFloat(tempString[4]));
len = len + 1;
}
else if (tempString[0].equals("goal")) {
objInfos[len] = new ObjInfo("goal", tempString[1],
Float.parseFloat(tempString[2]), Float.parseFloat(tempString[3]));
len = len + 1;
}
else if (tempString[0].equals("player")) {
objInfos[len] = new ObjInfo("player", tempString[1],
Integer.parseInt(tempString[2]), Float.parseFloat(tempString[3]),
Float.parseFloat(tempString[4]), Float.parseFloat(tempString[5]),
Float.parseFloat(tempString[6]), Float.parseFloat(tempString[7]),
Float.parseFloat(tempString[8]));
len = len + 1;
// System.out.println(len);
}
}
}
public StringBuilder printToString() {
StringBuilder string = new StringBuilder("在" + String.valueOf(time) + "周期see");
// System.out.println(objInfos[1] == null);
for (int i = 0; i < 3 ; i++) {
if (objInfos[i] != null) {
string.append(objInfos[i].printToString());
}
// if (objInfos[i].objName.getName().toLowerCase().equals("ball")) {
// string.append(objInfos[i].printToString());
// }
// else if (objInfos[i].objName.getName().toLowerCase().equals("goal")) {
// string.append(objInfos[i].printToString());
// }
// else if (objInfos[i].objName.getName().toLowerCase().equals("player")) {
// string.append(objInfos[i].printToString());
// }
}
return string;
}
}
public class week1 {
public static void main(String[] args) throws IOException {
// 测试输出referee hear解释是否正确
HearMessage message0 = new HearMessage(100, "referee", "passto(23, 24)");
System.out.println(message0.toString());
// 测试输出ObjName player解释是否正确
ObjInfo objInfo0 = new ObjInfo("player", "hfut1", 2, 23f, 45f,
.5f, 1f, 22f, 40f);
System.out.println(objInfo0.printToString());
// 测试输出ObjName ball解释是否正确
ObjInfo objInfo1 = new ObjInfo("ball", 23f, 45f, .5f, 1f);
System.out.println(objInfo1.printToString());
// 测试输出ObjName player写入文件信息是否正确
ObjInfo objInfo3 = new ObjInfo("player", "hfut1", 2, 23f, 45f,
.5f, 1f, 22f, 40f);
System.out.println(objInfo0.saveToString());
// 测试输出ObjName ball解释写入文件信息是否正确
ObjInfo objInfo4 = new ObjInfo("ball", 23f, 45f, .5f, 1f);
System.out.println(objInfo1.saveToString());
// 测试解释
SeeMessage seeMessage = new SeeMessage(2020, new String[]{"player,hfut1,2,23f,45f,.5f,1f,22f,40f", "ball,23f,45f,.5f,1f", "ball,23f,45f,.5f,1f"});
System.out.println(seeMessage.printToString());
// 测试输出ObjName player写入文件是否正确
ObjInfo objInfo5 = new ObjInfo("player", "hfut1", 2, 23f, 45f,
.5f, 1f, 22f, 40f);
objInfo0.saveToFile("D:\\Users\\leetu\\Documents\\_documents\\Projects\\fileIOTestFold\\a.txt");
}
}