battlecode / battlehack20

🐍
https://bh2020.battlecode.org
GNU General Public License v3.0
12 stars 3 forks source link

Ability to override functions like __le__ and __ne__. #132

Closed cooljoseph1 closed 4 years ago

cooljoseph1 commented 4 years ago

This code gives a guaranteed win:

class int2(int):
    def __ne__(self, val):
        return False

def turn():
    board_size = get_board_size()
    team = get_team()
    robottype = get_type()
    if robottype == RobotType.OVERLORD:
        if team == Team.BLACK:
            index = 0
        else:
            index = board_size - 1

        for i in range(board_size):
            i = int2(i)
            index = int2(index)
            row, col = index, i
            if not check_space(index, i):
                spawn(index, i)
                return

How it works is the following: We make a subclass of int called int2. We override __ne__ so that it is not not equal to anything. We then can place pawns anywhere on the board we want because the engine only checks that we are not placing pawns somewhere other than the back row. Because we overrided __ne__, this check returns false and we can place anywhere we want on the board.

image

jay20162016 commented 4 years ago

Duplicate of Issue #121

arvid220u commented 4 years ago

i really like this!! it’s a very clever hack. but yeah, duplicate of #121

jay20162016 commented 4 years ago

And the RestrictedPython is supposed to prevent that, but it doesn't work on some functions