Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved performance. Tested and working! #22

Merged
merged 2 commits into from
May 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 6 additions & 12 deletions project-rumble/basic-bots/src/se/lth/cs/etsa02/MessageReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,26 +74,20 @@ public Point getMyPos() {
* Returns the value of the friendPos line if the message contains it. Otherwise returns an empty array.
* @return a point created from the (x,y) values in the friendPos line or null if the line is not included in the message or parsing fails.
*/
public Point[] getFriendPos() {
public String[] getFriendPos() {
String[] values = getValues("friendPos");
Point[] p = new Point[values.length];
for (int i = 0; i < values.length; i++) {
p[i] = parsePoint(values[i]);
}
return p;
if (values.length > 0) return values;
return null;
}

/**
* Returns the values of the enemyPos lines if the message contains any. Otherwise returns an empty array.
* @return an array of points created from (x,y) values in the enemyPos lines or an empty array if no enemyPos line is included in the message.
*/
public Point[] getEnemyPos() {
public String[] getEnemyPos() {
String[] values = getValues("enemyPos");
Point[] p = new Point[values.length];
for (int i = 0; i < values.length; i++) {
p[i] = parsePoint(values[i]);
}
return p;
if (values.length > 0) return values;
return null;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ public MessageWriter() {
friendPosCount = 0;
enemyPos = new String[10];
enemyPosCount = 0;
enemyDetails = new String[10];
enemyDetailsCount = 0;
targetEnemy = new String();
targetPos = new String();
moveTo = new String();
Expand Down Expand Up @@ -90,8 +92,8 @@ public void addMyPos(double x, double y) {
* @param x
* @param y
*/
public void addFriendPos(double x, double y) {
friendPos[friendPosCount] = "friendPos;" + x + ";" + y;
public void addFriendPos(String name, double x, double y) {
friendPos[friendPosCount] = "friendPos;" + name + ";" + x + ";" + y;
friendPosCount++;
}

Expand All @@ -100,8 +102,8 @@ public void addFriendPos(double x, double y) {
* @param x
* @param y
*/
public void addEnemyPos(double x, double y) {
enemyPos[enemyPosCount] = "enemyPos;" + x + ";" + y;
public void addEnemyPos(String name, double x, double y) {
enemyPos[enemyPosCount] = "enemyPos;" + name + ";" + x + ";" + y;
enemyPosCount++;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ of this software and associated documentation files (the "Software"), to deal

import java.io.IOException;

import robocode.BulletHitEvent;


/**
* BasicDroid - a simple droid that fires based on leader bot's orders
Expand All @@ -49,7 +47,13 @@ public class BasicDroid extends TeamRobot implements Droid {
* run: Droid's default behavior
*/
public void run() {
out.println("MyFirstDroid ready.");
out.println("BasicDroid ready.");
try {
// Declare to team that this bot is a follower
MessageWriter writer = new MessageWriter();
writer.addLeadership("leadMe");
broadcastMessage(writer.composeMessage());
} catch (IOException ignored) {}
}

/**
Expand All @@ -66,7 +70,9 @@ public void onMessageReceived(MessageEvent e) {
setBulletColor(c.bulletColor);
} else {
MessageReader reader = new MessageReader((String)e.getMessage());
if (reader.getMoveTo() != null) goTo(reader.getMoveTo());
if (reader.getMoveTo() != null) {
goTo(reader.getMoveTo());
}

// If enemy position, fire!
String[] values = reader.getEnemyDetails();
Expand All @@ -82,24 +88,14 @@ public void onMessageReceived(MessageEvent e) {
double y = Double.parseDouble(ss[2]);
p = new Point(x,y);
} catch (RuntimeException err) {}
if (p != null) fireAtPoint(p);
if (p != null) {
fireAtPoint(p);
} else {
}
}
}
}

/**
* onHitBullet: What to do when out bullet hits a robot
*/
private void onHitBullet(BulletHitEvent e) {
if (e.getEnergy() <= 0) {
try {
// Message allies that enemy bot is dead
MessageWriter writer = new MessageWriter();
writer.addEnemyDetails(e.getName(), 0, 0, 0, e.getEnergy(), 0, 0);
broadcastMessage(writer.composeMessage());
} catch (IOException ignored) {}
}
}


private void fireAtPoint(Point p) {
// Calculate x and y to target
Expand All @@ -114,6 +110,7 @@ private void fireAtPoint(Point p) {
fire(3);
}


/**
* Set the robot to go to a certain point.
* The code comes from <a href="http://robowiki.net/wiki/GoTo"> http://robowiki.net/wiki/GoTo </a>
Expand Down

This file was deleted.

This file was deleted.

Loading