<commit_before>from pentai.base.pente_exceptions import *
import pentai.base.game_state as gs_m
from pentai.base.defines import *

import datetime

class Game(object):

    def __init__(self, *args, **kwargs):
        self.game_id = None
        self.move_history = []
        self.time_history = [0, 0]
        self.capture_history = []
        self.resume_move_number = None
        self.date = datetime.date.today() # TODO
        self.setup(*args, **kwargs)
        self.ratings = [None, None, None]
        self.live = True

    def clone(self):
        import copy
        new_one = copy.copy(self)
        new_one.current_state = gs_m.GameState(self)
        return new_one

    def setup(self, rules=None, player1=None, player2=None):
        self.rules = rules
        if rules != None:
            self.current_state = gs_m.GameState(self)

            total_time = rules.time_control
            if total_time:
                # B/W
                self.time_history = [total_time, total_time, total_time]

        self.players = [None, player1, player2]
        if player1 != None:
            player1.attach_to_game(self)
        if player2 != None:
            player2.attach_to_game(self)
        self.autosave_filename = None

    def __eq__(self, other):
        return self.key() == other.key()

    def resume(self):
        """ Call this once all the observers are set up """
        if not self.resume_move_number is None:
            rmn = self.resume_move_number
            self.go_to_move(rmn)

    def get_date(self):
        return self.date

    def get_rules(self):
        return self.rules

    def key(self):
        return self.game_id

    # TODO: get_size only for consistency
    def size(self):
        return self.rules.size

    def get_size(self):
        return self.rules.size

    def get_rules_type_name(self):
        return self.rules.get_type_name()

    def get_current_state(self):
        return self.current_state

    def get_board(self):
        return self.current_state.board

    def off_board(self, pos):
        return self.get_board().off_board(pos)

    def get_player(self, player_number):
        return self.players[player_number]
    
    def get_player_type(self, player_number):
        ret = self.players[player_number].get_type()
        if ret == "AI":
            ret = "Computer"
        return ret
    
    def get_all_players(self):
        return self.players
    
    def get_player_name(self, player_number):
        return self.players[player_number].get_name()

    def get_current_player(self):
        return self.current_state.to_move_player()

    def get_current_player_type(self):
        return self.get_current_player().get_type()

    def get_colour_of_player(self, player):
        if self.players[P1] == player:
            return P1
        if self.players[P2] == player:
            return P2

    def to_move_colour(self):
        return self.current_state.to_move_colour()

    def is_live(self):
        return self.live

    def set_live(self, val, gui): # TODO: Remove gui
        import pentai.base.logger as log
        log.debug("set_live %s" % val)
        if gui:
            if val:
                if not self.live and not self.finished():
                    self.prompt_for_action(gui)
            else:
                gui.stop_ticking() # TODO!

        self.live = val

    # TODO: Move this out of game, it seems to be gui stuff
    def prompt_for_action(self, gui):
        if not self.live:
            return
        if self.finished():
            return "Game was won by: %s" % self.winner_name()
        return self.get_current_player().prompt_for_action(self, gui)

    # Not sure if these should even delegate
    def get_move_number(self):
        return self.current_state.get_move_number()

    def set_move_number(self, turn):
        self.current_state.set_move_number(turn)

    def get_captured(self, player_number):
        return self.current_state.get_captured(player_number)

    def set_captured(self, player_number, pieces):
        return self.current_state.set_captured(player_number, pieces)

    def get_last_move(self):
        return self.move_history[-1]

    def make_move(self, move):
        if move is None:
            raise IllegalMoveException("Null move")
        # Record this, then save to a file if required
        if len(self.move_history) > 0:
            del self.move_history[self.get_move_number()-1:]
            del self.time_history[self.get_move_number()+1:]
        self.move_history.append(move)
        colour = self.to_move_colour()
        self.time_history.append(self.remaining_time(opposite_colour(colour)))
        self.resume_move_number = len(self.move_history) + 1

        try:
            captured = self.current_state.make_move(move)
        except IllegalMoveException, e:
            # Wipe that one off.
            del self.move_history[-1]
            raise e

        self.capture_history.append(captured)

        '''
        # Temp: disable due to iOS restrictions
        if self.autosave_filename != None:
            self.save_history()
        '''

    def set_won_by(self, colour):
        self.current_state.set_won_by(colour)

    def finished(self):
        return self.current_state.get_won_by() > 0

    def get_won_by(self):
        return self.current_state.get_won_by()

    def get_win_method(self):
        won_by = self.get_won_by()
        if not won_by:
            return ""
        elif won_by == (P1 + P2):
            return "Drawn"
        elif self.get_captured(won_by) >= 10:
            return "Captures"
        return "Five in a row"

    def winner_name(self):
        return self.players[self.current_state.get_won_by()]

    def get_move(self, move_number):
        try:
            return self.move_history[move_number-1]
        except IndexError:
            return None

    def get_last_move(self):
        return self.get_move(self.get_move_number()-1)

    def go_to_the_beginning(self):
        self.go_to_move(1)

    def go_forwards_one(self):
        self.go_to_move(self.get_move_number() + 1)

    def go_backwards_one_for_gui(self):
        self.go_to_move(self.get_move_number() - 1)

    def go_backwards_one(self):
        current_move = self.get_move_number()
        if current_move <= 0:
            # Already at the beginning
            return

        try:<commit_msg>Lots of captures don't trigger a win for 5 in a row<commit_after>from pentai.base.pente_exceptions import *
import pentai.base.game_state as gs_m
from pentai.base.defines import *

import datetime

class Game(object):

    def __init__(self, *args, **kwargs):
        self.game_id = None
        self.move_history = []
        self.time_history = [0, 0]
        self.capture_history = []
        self.resume_move_number = None
        self.date = datetime.date.today() # TODO
        self.setup(*args, **kwargs)
        self.ratings = [None, None, None]
        self.live = True

    def clone(self):
        import copy
        new_one = copy.copy(self)
        new_one.current_state = gs_m.GameState(self)
        return new_one

    def setup(self, rules=None, player1=None, player2=None):
        self.rules = rules
        if rules != None:
            self.current_state = gs_m.GameState(self)

            total_time = rules.time_control
            if total_time:
                # B/W
                self.time_history = [total_time, total_time, total_time]

        self.players = [None, player1, player2]
        if player1 != None:
            player1.attach_to_game(self)
        if player2 != None:
            player2.attach_to_game(self)
        self.autosave_filename = None

    def __eq__(self, other):
        return self.key() == other.key()

    def resume(self):
        """ Call this once all the observers are set up """
        if not self.resume_move_number is None:
            rmn = self.resume_move_number
            self.go_to_move(rmn)

    def get_date(self):
        return self.date

    def get_rules(self):
        return self.rules

    def key(self):
        return self.game_id

    # TODO: get_size only for consistency
    def size(self):
        return self.rules.size

    def get_size(self):
        return self.rules.size

    def get_rules_type_name(self):
        return self.rules.get_type_name()

    def get_current_state(self):
        return self.current_state

    def get_board(self):
        return self.current_state.board

    def off_board(self, pos):
        return self.get_board().off_board(pos)

    def get_player(self, player_number):
        return self.players[player_number]
    
    def get_player_type(self, player_number):
        ret = self.players[player_number].get_type()
        if ret == "AI":
            ret = "Computer"
        return ret
    
    def get_all_players(self):
        return self.players
    
    def get_player_name(self, player_number):
        return self.players[player_number].get_name()

    def get_current_player(self):
        return self.current_state.to_move_player()

    def get_current_player_type(self):
        return self.get_current_player().get_type()

    def get_colour_of_player(self, player):
        if self.players[P1] == player:
            return P1
        if self.players[P2] == player:
            return P2

    def to_move_colour(self):
        return self.current_state.to_move_colour()

    def is_live(self):
        return self.live

    def set_live(self, val, gui): # TODO: Remove gui
        import pentai.base.logger as log
        log.debug("set_live %s" % val)
        if gui:
            if val:
                if not self.live and not self.finished():
                    self.prompt_for_action(gui)
            else:
                gui.stop_ticking() # TODO!

        self.live = val

    # TODO: Move this out of game, it seems to be gui stuff
    def prompt_for_action(self, gui):
        if not self.live:
            return
        if self.finished():
            return "Game was won by: %s" % self.winner_name()
        return self.get_current_player().prompt_for_action(self, gui)

    # Not sure if these should even delegate
    def get_move_number(self):
        return self.current_state.get_move_number()

    def set_move_number(self, turn):
        self.current_state.set_move_number(turn)

    def get_captured(self, player_number):
        return self.current_state.get_captured(player_number)

    def set_captured(self, player_number, pieces):
        return self.current_state.set_captured(player_number, pieces)

    def get_last_move(self):
        return self.move_history[-1]

    def make_move(self, move):
        if move is None:
            raise IllegalMoveException("Null move")
        # Record this, then save to a file if required
        if len(self.move_history) > 0:
            del self.move_history[self.get_move_number()-1:]
            del self.time_history[self.get_move_number()+1:]
        self.move_history.append(move)
        colour = self.to_move_colour()
        self.time_history.append(self.remaining_time(opposite_colour(colour)))
        self.resume_move_number = len(self.move_history) + 1

        try:
            captured = self.current_state.make_move(move)
        except IllegalMoveException, e:
            # Wipe that one off.
            del self.move_history[-1]
            raise e

        self.capture_history.append(captured)

        '''
        # Temp: disable due to iOS restrictions
        if self.autosave_filename != None:
            self.save_history()
        '''

    def set_won_by(self, colour):
        self.current_state.set_won_by(colour)

    def finished(self):
        return self.current_state.get_won_by() > 0

    def get_won_by(self):
        return self.current_state.get_won_by()

    def get_win_method(self):
        won_by = self.get_won_by()
        if not won_by:
            return ""
        elif won_by == (P1 + P2):
            return "Drawn"
        elif self.rules.can_capture_pairs and \
             self.get_captured(won_by) >= self.rules.stones_for_capture_win:
            return "Captures"
        return "Five in a row"

    def winner_name(self):
        return self.players[self.current_state.get_won_by()]

    def get_move(self, move_number):
        try:
            return self.move_history[move_number-1]
        except IndexError:
            return None

    def get_last_move(self):
        return self.get_move(self.get_move_number()-1)

    def go_to_the_beginning(self):
        self.go_to_move(1)

    def go_forwards_one(self):
        self.go_to_move(self.get_move_number() + 1)

    def go_backwards_one_for_gui(self):
        self.go_to_move(self.get_move_number() - 1)

    def go_backwards_one(self):
        current_move = self.get_move_number()
        if current_move <= 0:
            # Already at the beginning
            return

        try:
{'fraction_non_alphanumeric': 0.09239340710361371, 'fraction_numerical': 0.004720266192060667, 'mean_word_length': 2.202973977695167, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 9, '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': '4361147', 'n_tokens_mistral': 4124, 'n_tokens_neox': 3969, 'n_words': 880}