This code is for my upcoming challenge on PPCG. The key part of this challenge is supposed ability to overwrite functions that other people have written. Overwriting functions will happen at runtime, and a particular function on a bot instance can be overwritten many times

For example, CodeBot A has a function sendMessage() which returns a message to send. If CodeBot A were to overwrite CodeBot B's sendMessage(), then any time CodeBot B needed to send a message, CodeBot A's sendMessage() function would get called.

The CodeBot class:

package codebots.bot;

import codebots.gameobjects.*;

import java.util.List;

public abstract class CodeBot {

    private Bot underlyingBot;

    public final void setUnderlyingBot(Bot bot){
        this.underlyingBot = bot;

    public abstract List<IPAddress> selectMessageRecipients();
    public abstract Message sendMessage();
    public abstract void processMessage(Message message);
    public abstract IPAddress selectAttackTarget();
    public abstract void readData(ReadonlyBot bot);
    public abstract FunctionType selectFunctionToReplace();
    public abstract FunctionType selectFunctionToBlock();
    public abstract String getFlag();

    protected final AddressBook getAddressBook(){
        return underlyingBot.getAddressBook();

    protected final Log getLog(){
        return underlyingBot.getLog();

    protected final Variables getVariables(){
        return underlyingBot.getVariables();


The Bot class:

package codebots.bot;

import codebots.gameobjects.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public final class Bot {
    private final Map<FunctionType, CodeBot> functionOwners;
    private final AddressBook addressBook;
    private final Log log;
    private final Variables variables;

    public Bot(CodeBot parent){
        functionOwners = new HashMap<>();
        for (FunctionType type: FunctionType.values()){
            functionOwners.put(type, parent);
        addressBook = new AddressBook();
        log = new Log();
        variables = new Variables();

    private CodeBot getOwner(FunctionType type){
        CodeBot owner = functionOwners.get(type);
        return owner;

    public List<IPAddress> selectMessageRecipients(){
        return getOwner(FunctionType.SELECT_MESSAGE_RECIPIENTS).selectMessageRecipients();
    public Message sendMessage(){
        return getOwner(FunctionType.SEND_MESSAGE).sendMessage();
    public void processMessage(Message message){
    public IPAddress selectAttackTarget(){
        return getOwner(FunctionType.SELECT_ATTACK_TARGET).selectAttackTarget();
    public void readData(ReadonlyBot bot){
    public FunctionType selectFunctionToReplace(){
        return getOwner(FunctionType.SELECT_FUNCTION_TO_REPLACE).selectFunctionToReplace();
    public FunctionType selectFunctionToBlock(){
        return getOwner(FunctionType.SELECT_FUNCTION_TO_BLOCK).selectFunctionToBlock();
    public String getFlag(){
        return getOwner(FunctionType.GET_FLAG).getFlag();

    public AddressBook getAddressBook(){
        return addressBook;

    public Log getLog(){
        return log;

    public Variables getVariables(){
        return variables;

    public void replace(FunctionType type, CodeBot bot){
        functionOwners.put(type, bot);


As you can see, there is a lot of duplicated code. Bot contains a copy of a function in CodeBot, and simply selects the bot call the function on.

I'm looking for a better way to design my code, as well as general improvements. I'm using Java 8, so any feature from Java 8 is allowed.

  • \$\begingroup\$ I don't understand what you are trying to do enough to figure out what can be made better. \$\endgroup\$ – Justin Dec 8 '15 at 17:10
  • \$\begingroup\$ meta.codegolf.stackexchange.com/a/7658/9498 \$\endgroup\$ – Justin Dec 8 '15 at 17:12
  • \$\begingroup\$ @Justin In this competition, we literally replace each others code at run time. So, if I successfully attack a robot (at runtime) and replace his "sendMessage()" function, then instead of his sendMessage() function getting called, mine does. However, once I've copied over my code, that copy doesn't have access to the past bot, and has to perform all of its actions on the new bot. Does that make more sense? \$\endgroup\$ – Nathan Merrill Dec 8 '15 at 17:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.