<commit_before>import pygame

import settings
import bots
from core.players import Player


class Game(object):
    def __init__(self, *args):
        self.args = args
        self.occupied_cells = []
        self.init_screen()
        self.init_bots()
        self.init_players()
        self.done = False
        self.victorious_player = None

    def set_occupied_cells(self):
        """
        Update the list of the cells currently occupied by units
        """
        # TODO: Optimize by adding add update_occupied_cells instead
        self.occupied_cells = [(unit.x, unit.y) for unit in self.units]

    def init_screen(self):
        """
        Initialize screen
        """
        self.screen = pygame.display.get_surface()
        self.screen_rect = self.screen.get_rect()
        self.clock = pygame.time.Clock()
        self.fps = settings.FPS

    def init_players(self):
        """
        Create Players and load their personal bot
        """
        # I can't initialize players with a list comprehension because
        # Player __init__ need to access Game.players attribute
        # TODO: Code smell?
        self.players = []
        for i, bot_class in enumerate(self.bots):
            player = Player(pk=i, bot_class=bot_class, game=self)
            self.players.append(player)

    def init_bots(self):
        """
        Create bot action functions by getting the name of the
        package/module from the args
        """
        for arg in self.args:
            __import__('bots.{}'.format(arg))
        self.bots = [getattr(bots, arg).Bot for arg in self.args]

    @property
    def units(self):
        """
        All the units in the Game
        """
        return [unit for player in self.players for unit in player.units]

    def event_loop(self):
        """
        Fetch for events
        """
        for event in pygame.event.get():
            self.keys = pygame.key.get_pressed()
            if event.type == pygame.QUIT or self.keys[pygame.K_ESCAPE]:
                self.done = True

    def update(self):
        """
        Fetch all unit positions and action units
        """
        for player in self.players:
            for unit in player.units:
                unit.action()
                self.set_occupied_cells()

    def draw(self):
        """
        Main drawing function called in the infinite loop
        """
        self.screen.fill(settings.BG_COLOR)
        for unit in self.units:
            unit.render()
        self.draw_grid()

    def draw_grid(self):
        """
        Draw a grid according to the game settings
        """
        X, Y = settings.SCREEN_SIZE
        gap_x, gap_y = settings.UNIT_SIZE
        for i in xrange(0, X+1, gap_x):
            pygame.draw.line(
                self.screen,
                settings.GRID_COLOR,
                (i, 0),
                (i, Y),
                1,
            )
        for i in xrange(0, Y+1, gap_y):
            pygame.draw.line(
                self.screen,
                settings.GRID_COLOR,
                (0, i),
                (X, i),<commit_msg>Randomize order of actions take each turn<commit_after>from random import shuffle

import pygame

import settings
import bots
from core.players import Player


class Game(object):
    def __init__(self, *args):
        self.args = args
        self.occupied_cells = []
        self.init_screen()
        self.init_bots()
        self.init_players()
        self.done = False
        self.victorious_player = None

    def set_occupied_cells(self):
        """
        Update the list of the cells currently occupied by units
        """
        # TODO: Optimize by adding add update_occupied_cells instead
        self.occupied_cells = [(unit.x, unit.y) for unit in self.units]

    def init_screen(self):
        """
        Initialize screen
        """
        self.screen = pygame.display.get_surface()
        self.screen_rect = self.screen.get_rect()
        self.clock = pygame.time.Clock()
        self.fps = settings.FPS

    def init_players(self):
        """
        Create Players and load their personal bot
        """
        # I can't initialize players with a list comprehension because
        # Player __init__ need to access Game.players attribute
        # TODO: Code smell?
        self.players = []
        for i, bot_class in enumerate(self.bots):
            player = Player(pk=i, bot_class=bot_class, game=self)
            self.players.append(player)

    def init_bots(self):
        """
        Create bot action functions by getting the name of the
        package/module from the args
        """
        for arg in self.args:
            __import__('bots.{}'.format(arg))
        self.bots = [getattr(bots, arg).Bot for arg in self.args]

    @property
    def units(self):
        """
        All the units in the Game
        """
        return [unit for player in self.players for unit in player.units]

    def event_loop(self):
        """
        Fetch for events
        """
        for event in pygame.event.get():
            self.keys = pygame.key.get_pressed()
            if event.type == pygame.QUIT or self.keys[pygame.K_ESCAPE]:
                self.done = True

    def update(self):
        """
        Fetch all unit positions and action units
        """
        units = [unit for player in self.players for unit in player.units]
        shuffle(units)
        for unit in units:
            unit.action()
            self.set_occupied_cells()

    def draw(self):
        """
        Main drawing function called in the infinite loop
        """
        self.screen.fill(settings.BG_COLOR)
        for unit in self.units:
            unit.render()
        self.draw_grid()

    def draw_grid(self):
        """
        Draw a grid according to the game settings
        """
        X, Y = settings.SCREEN_SIZE
        gap_x, gap_y = settings.UNIT_SIZE
        for i in xrange(0, X+1, gap_x):
            pygame.draw.line(
                self.screen,
                settings.GRID_COLOR,
                (i, 0),
                (i, Y),
                1,
            )
        for i in xrange(0, Y+1, gap_y):
            pygame.draw.line(
                self.screen,
                settings.GRID_COLOR,
                (0, i),
                (X, i),
{'fraction_non_alphanumeric': 0.09412137537632705, 'fraction_numerical': 0.002218348914593567, 'mean_word_length': 1.64321608040201, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4405100', 'n_tokens_mistral': 1828, 'n_tokens_neox': 1756, 'n_words': 508}