Skip to content
Open
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
Binary file added bots/Java/BotAPI.jar
Binary file not shown.
129 changes: 129 additions & 0 deletions bots/Java/src/Bot.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

import javax.swing.*;
import java.util.ArrayList;
import java.util.Objects;

/**
* Created by Aakash and Sourav on 2/19/2017.
*/

/**Disclaimer* this library doesn't have the feature to edit fields of a JSON directly
so, it was a pain in the ass to make all the functions that required editing */
public class Bot {
JSONObject gameState;
String name;
JSONArray moveObject = new JSONArray();
Bot(JSONObject json, String s) {
gameState = json;
name = s;
JSONArray bots= (JSONArray) gameState.get("bots");


for(int i=0; i<bots.size(); i++){
Object obj = bots.get(i);
JSONObject temp = (JSONObject) obj;
if(temp.get("botname").equals(name)){
long index = (long) temp.get("childno");
JSONObject jsonObject = new JSONObject();
jsonObject.put("childno", index);
jsonObject.put("relativeangle", 0);
jsonObject.put("ejectmass", Boolean.FALSE);
jsonObject.put("split", Boolean.FALSE);
jsonObject.put("pause", Boolean.FALSE);
moveObject.add((int)index, jsonObject);
}
}



}
void changeDirection(int childNo, float relativeAngle){
JSONObject object = (JSONObject) moveObject.get(childNo);
JSONObject temp = new JSONObject();
temp.put("childno", childNo);
temp.put("relativeangle", relativeAngle);
temp.put("ejectmass", object.get("ejectmass"));
temp.put("split", object.get("split"));
temp.put("pause", object.get("pause"));
moveObject.set(childNo, temp);

}
void ejectMass(int childNo){
JSONObject object = (JSONObject) moveObject.get(childNo);
JSONObject temp = new JSONObject();
temp.put("childno", childNo);
temp.put("relativeangle", object.get("relativeangle"));
temp.put("ejectmass", Boolean.TRUE);
temp.put("split", object.get("split"));
temp.put("pause", object.get("pause"));
moveObject.set(childNo, temp);
}
void split(int childNo){
JSONObject object = (JSONObject) moveObject.get(childNo);
JSONObject temp = new JSONObject();
temp.put("childno", childNo);
temp.put("relativeangle", object.get("relativeangle"));
temp.put("ejectmass", object.get("ejectmass"));
temp.put("split", Boolean.TRUE);
temp.put("pause", object.get("pause"));
moveObject.set(childNo, temp);
}
void pause(int childNo){
JSONObject object = (JSONObject) moveObject.get(childNo);
JSONObject temp = new JSONObject();
temp.put("childno", childNo);
temp.put("relativeangle", object.get("relativeangle"));
temp.put("ejectmass", object.get("ejectmass"));
temp.put("split", object.get("split"));
temp.put("pause", Boolean.TRUE);
moveObject.set(childNo, temp);
}
String makeMove(){
return moveObject.toJSONString();
}
ArrayList<JSONObject> getChildren(){
ArrayList<JSONObject> returnArray = new ArrayList<>();
JSONArray object = (JSONArray) gameState.get("bots");
for(int i=0; i<object.size(); i++){
JSONObject temp = (JSONObject) object.get(i);
if(temp.get("botname")==name){
returnArray.add(temp);
}

}
return returnArray;
}
ArrayList<JSONObject> getBots(){
ArrayList<JSONObject> returnArray = new ArrayList<>();
JSONArray object = (JSONArray) gameState.get("bots");
for(int i=0; i<object.size(); i++){
JSONObject temp = (JSONObject) object.get(i);
if(temp.get("botname")!=name){
returnArray.add(temp);
}

}
return returnArray;
}
JSONArray getFood(){
JSONArray returnArray = (JSONArray) gameState.get("food");
return returnArray;
}
JSONArray getViruses(){
JSONArray returnArray = (JSONArray) gameState.get("virus");
return returnArray;
}
JSONArray getFfieldCircle(){
JSONArray returnArray = (JSONArray) gameState.get("ffieldcircle");
return returnArray;
}
JSONArray getFfieldSquareCircle(){
JSONArray returnArray = (JSONArray) gameState.get("ffieldsquare");
return returnArray;
}



}
31 changes: 31 additions & 0 deletions bots/Java/src/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;

/**
* Created by Aakash and Sourav on 2/19/2017.
*/
public class Main {
public static void main(String[] args) throws IOException {
System.out.println("I'm Poppy!");
/*BufferedReader reader = new BufferedReader(new FileReader("JSONFiles/jsonjson"));
StringBuilder stringBuilder = new StringBuilder(2048);
String jsonText;
while((jsonText = reader.readLine())!=null){
stringBuilder.append(jsonText);
}*/
String stringBuilder = new String();
Scanner in = new Scanner(System.in);
stringBuilder=in.nextLine();
//System.out.println(stringBuilder);
JSONObject gameState = (JSONObject) JSONValue.parse(stringBuilder.toString());

Bot game = new Bot(gameState, "kevin");
System.out.println(game.makeMove());

}
}
7 changes: 7 additions & 0 deletions bots/Java/src/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
1) This API requires a library called JSON simple to be added

2) The input is a string given in one line {"bots":[{"botname":"kevin","childno":0,"radius":10,"velocity":0.59,"Xcoordinate":25,"Ycoordinate":30,"mass":20,"angle":180,"score":150},{"botname":"kevin","childno":1,"radius":90,"velocity":0.30,"Xcoordinate":5,"Ycoordinate":30,"mass":22,"angle":18,"score":150},{"botname":"juno","childno":0,"radius":90,"velocity":0.30,"Xcoordinate":23,"Ycoordinate":600,"mass":60,"angle":45,"score":90}],"maxX":14142,"maxY":14142,"food":[[1,2],[2,9],[23,35],[78,345],[89,233]],"virusrad":15,"virus":[[123,45],[3455,34],[333,77],[3455,33],[333,90],[23,55]],"ffieldcircle":[{"innerrad":30,"outerrad":45,"origin":[67,788]},{"innerrad":30,"outerrad":50,"origin":[6790,88]}],"ffieldsquare":[{"origin":[233,7888],"innerside":90,"outerside":110}]}

3) The output was [{"ejectmass":false,"split":false,"childno":0,"relativeangle":0,"pause":false},{"ejectmass":false,"split":false,"childno":1,"relativeangle":0,"pause":false}]

4) Its recommended to test this out using different inputs