def play_turn(self, wm, turn): """ This method is called once for every turn. This specific example solution stores a matrix with the snakes bodies (as 0 or 1, with -1 as empty cell) and returns a random valid direction. Parameters: wm - an instance of the WorldModel class that contains a field called players which is a list of Players. A Player has a field named body which is a list of Coordinates, and which represent the coordinates of the body parts of the snake, in order. A Coordinate has two fields, x and y, of type int. turn - an integer, the index of the turn. If you receive twice the same number, don't worry, just ignore it. Returns: A Command instance - a Command contains a field called direction of type int, which can be one of the Direction values: RIGHT, UP, LEFT, DOWN. """ print len(self.table) # Place new heads self.table[wm.players[0].body[-1].x][wm.players[0].body[-1].y] = 0 self.table[wm.players[1].body[-1].x][wm.players[1].body[-1].y] = 1 # Print the board print "Turn", turn for i in range(self.width): for j in range(self.height): if self.table[i][j] == self.me: print 'M', elif self.table[i][j] == 1 - self.me: print 'O', else: print '.', print print command = Command() command.direction = Direction.DOWN # Randomly choose a direction indexes = range(4) shuffle(indexes) for i in indexes: newx = wm.players[self.me].body[-1].x + dx[i] newy = wm.players[self.me].body[-1].y + dy[i] if self.isValid(newx, newy) and self.table[newx][newy] == -1: command.direction = dirs[i] break return command
def play_turn(self, wm, moveList, turn): """ This method is called once for every turn. This specific example solution stores a matrix with the snakes bodies (as 0 or 1, with -1 as empty cell) and returns a random valid direction. Parameters: wm - an instance of the WorldModel class that contains a field called players which is a list of Players. A Player has a field named body which is a list of Coordinates, and which represent the coordinates of the body parts of the snake, in order. A Coordinate has two fields, x and y, of type int. turn - an integer, the index of the turn. If you receive twice the same number, don't worry, just ignore it. Returns: A Command instance - a Command contains a field called direction of type int, which can be one of the Direction values: RIGHT, UP, LEFT, DOWN. """ command = Command() command.direction = Direction.UP if moveList: command = choice(moveList) print str(turn) + ": " + str(command.direction) return command