diff --git a/src/SnakeBot.java b/src/SnakeBot.java deleted file mode 100644 index 6f7abc0..0000000 --- a/src/SnakeBot.java +++ /dev/null @@ -1,70 +0,0 @@ -package src; - -public class SnakeBot extends Bot { - int stepCounter = 0; - int turnCount = 0; - int viewRange = 5; - int straightLength = 1; - boolean isOnPath = true; - - protected SnakeBot(String[] args) { - super(args); - } - - public static void main(String[] args) { - SnakeBot bot = new SnakeBot(args); - bot.run(); - } - - @Override - protected char nextMove(View view) throws Exception { - - if (!view.data.contains("@") && isOnPath) { - - if (turnCount == 2) { - turnCount = 0; - straightLength++; - } - if (stepCounter < straightLength * viewRange) { - stepCounter++; - return '^'; - } else { - stepCounter = 0; - turnCount++; - return '>'; - } - } else if (!view.data.contains("@") && !isOnPath) { - - } else { - isOnPath = false; - //check for minerals to the left of the rover (high prio) - for (int i = 0; i < view.data.length(); i += viewRange) { - if (view.data.substring(i, i + 2).contains("@")) { - return '<'; - } - } - //check for minerals in front of the rover (mid prio) - if (view.data.substring(0, view.data.length() / 2 - viewRange / 2).contains("@")) { - return '^'; - } - //check for minerals to the right of the rover (low prio) - for (int i = 3; i < view.data.length(); i += viewRange) { - if (view.data.substring(i, i + 2).contains("@")) { - return '>'; - } - } - -// if (view.data.substring(0, 10).contains("@")) { -// return '^'; -// } else if (view.data.substring(10, 12).contains("@")) { -// return '<'; -// } else if (view.data.substring(13, 15).contains("@")) { -// return '>'; -// } else if (view.data.substring(15, 25).contains("@")) { -// return 'v'; -// } - } - - return 0; - } -} diff --git a/src/Bot.java b/src/src/Bot.java similarity index 97% rename from src/Bot.java rename to src/src/Bot.java index 6ff94ba..26577a6 100644 --- a/src/Bot.java +++ b/src/src/Bot.java @@ -38,8 +38,9 @@ public abstract class Bot { try { char ch = nextMove(view); out.write(ch); + } catch (Exception e) { + break; } - catch (Exception e) { break; } } socket.close(); } catch (IOException e) { diff --git a/src/EscapeBot.java b/src/src/EscapeBot.java similarity index 62% rename from src/EscapeBot.java rename to src/src/EscapeBot.java index b37b7c7..404441a 100644 --- a/src/EscapeBot.java +++ b/src/src/EscapeBot.java @@ -1,10 +1,11 @@ package src; -public class EscapeBot extends Bot{ +public class EscapeBot extends Bot { int stepCounter = 0; int turnCount = 0; int viewRange = 5; int straightLength = 1; + protected EscapeBot(String[] args) { super(args); } @@ -13,31 +14,32 @@ public class EscapeBot extends Bot{ EscapeBot bot = new EscapeBot(args); bot.run(); } + @Override protected char nextMove(View view) throws Exception { - if(!view.data.contains("o")){ + if (!view.data.contains("o")) { - if(turnCount == 2){ + if (turnCount == 2) { turnCount = 0; straightLength++; } - if(stepCounter < straightLength * viewRange){ + if (stepCounter < straightLength * viewRange) { stepCounter++; return '^'; - }else{ + } else { stepCounter = 0; turnCount++; return '>'; } - }else{ - if(view.data.substring(0,10).contains("o")){ + } else { + if (view.data.substring(0, 10).contains("o")) { return '^'; - }else if(view.data.substring(10,12).contains("o")){ + } else if (view.data.substring(10, 12).contains("o")) { return '<'; - }else if (view.data.substring(13,15).contains("o")) { + } else if (view.data.substring(13, 15).contains("o")) { return '>'; - }else if(view.data.substring(15,25).contains("o")){ + } else if (view.data.substring(15, 25).contains("o")) { return 'v'; } } diff --git a/src/ManualBot.java b/src/src/ManualBot.java similarity index 82% rename from src/ManualBot.java rename to src/src/ManualBot.java index 87b45d6..8482c58 100644 --- a/src/ManualBot.java +++ b/src/src/ManualBot.java @@ -2,7 +2,7 @@ package src; import java.util.Scanner; -public class ManualBot extends Bot{ +public class ManualBot extends Bot { Scanner scanner = new Scanner(System.in); @@ -14,11 +14,12 @@ public class ManualBot extends Bot{ ManualBot bot = new ManualBot(args); bot.run(); } + @Override protected char nextMove(View view) throws Exception { char ch = 0; - if(scanner.hasNextLine()){ - ch = scanner.nextLine().toCharArray()[0]; + if (scanner.hasNextLine()) { + ch = scanner.nextLine().toCharArray()[0]; } return switch (ch) { diff --git a/src/src/SnakeBot.java b/src/src/SnakeBot.java new file mode 100644 index 0000000..5863396 --- /dev/null +++ b/src/src/SnakeBot.java @@ -0,0 +1,125 @@ +package src; + +public class SnakeBot extends Bot { + int straightLength = 1; //length of the straight the bot still needs to travel(in "FOV-Tiles") + int stepCounter = 0; //steps the bot has already taken on the straight + int turnCount = 2; //amount of times the bot needs to turn before increasing straightLength + int viewRange = 5; //distance from one end to the bots FOV to the other (assumes square FOV) + int wagonCount = 0; //amount of wagons the rover is currently pulling + int angle = 0; //current angle of the rover, compared to its initial angle + boolean isOnPath = true; //if the bot is on its normal search path (not pathing to a mineral) + char[] clearSequence = {'<', '^', '<', '^', '^', '>', '^', '>', '^', '^', '^', '^', '>', '^', '>', '^', '<', '^', '^', '>', + '^', '^', '^', '<', '^', '<', '^', '^', '^', '^', '<', '^', '^', '>'}; + char[] cornerClearSequence = {'<', '^', '<', '^', '^', '>', '^', '>', '^', '^', '^', '^', '>', '^', '>', '^', '<', '^', + '<', '^', '>', '^', '^', '>', '^', '>', '^', '<', '^', '^', '^', '<', '^', '<', '^', '^', '>'}; + int clearSequenceCounter = 0; + boolean isClearing = false; + + protected SnakeBot(String[] args) { + super(args); + } + + public static void main(String[] args) { + SnakeBot bot = new SnakeBot(args); + bot.run(); + } + + @Override + protected char nextMove(View view) throws Exception { + +// if (!view.data.contains("@") && isOnPath) { +// +// if (turnCount <= 0) { +// turnCount = 2; +// straightLength++; +// } +// if (stepCounter < straightLength * viewRange) { +// stepCounter++; +// return '^'; +// } else { +// stepCounter = 0; +// turnCount--; +// angle = (angle + 90) % 360; +// return '>'; +// } +// } else if (!view.data.contains("@") && !isOnPath) { +// +// } else { +// isOnPath = false; +// //check for minerals to the left of the rover (high prio) +// for (int i = 0; i < view.data.length(); i += viewRange) { +// if (view.data.substring(i, i + 2).contains("@")) { +// angle = (angle + 270) % 360; +// return '<'; +// } +// } +// //check for minerals in front of the rover (mid prio) +// if (view.data.substring(0, view.data.length() / 2 - viewRange / 2).contains("@")) { +// return '^'; +// } +// //check for minerals to the right of the rover (low prio) +// for (int i = 3; i < view.data.length(); i += viewRange) { +// if (view.data.substring(i, i + 2).contains("@")) { +// angle = (angle + 90) % 360; +// return '>'; +// } +// } + +// if (view.data.substring(0, 10).contains("@")) { +// return '^'; +// } else if (view.data.substring(10, 12).contains("@")) { +// return '<'; +// } else if (view.data.substring(13, 15).contains("@")) { +// return '>'; +// } else if (view.data.substring(15, 25).contains("@")) { +// return 'v'; +// } +// } + + if (stepCounter % 5 == 0 && !isClearing && view.data.contains("@")) { + isClearing = true; + } + if (isClearing) { + return clearFov(view); + } else { + if (turnCount <= 0) { + turnCount = 2; + straightLength++; + } + if (stepCounter < straightLength * viewRange) { + stepCounter++; + return '^'; + } else { + stepCounter = 0; + turnCount--; + return '>'; + } + } + //return 0; + } + + protected char clearFov(View view) { + char move; + //check if rover is at a corner of its search path + if (stepCounter >= straightLength * viewRange) { + move = cornerClearSequence[clearSequenceCounter++]; + + if (clearSequenceCounter >= cornerClearSequence.length) { + isClearing = false; + stepCounter = 2; + turnCount--; + clearSequenceCounter = 0; + } + } else { + move = clearSequence[clearSequenceCounter++]; + + if (clearSequenceCounter >= clearSequence.length) { + isClearing = false; + stepCounter += 2; + clearSequenceCounter = 0; + } + } + + return move; + } +}