<commit_before>
from pente_exceptions import *
from board import *
from pos import *

class GameState():
    """ This is for the state of a game as of a particular move. 
    """
    def __init__(self, game, parent=None):
        self.game = game
        self.parent = parent
        if parent == None:
            self.board = Board(game.size())
            # 3 for convenience, should only use [1] and [2]
            self.captured = [0,0,0]
            self.set_won_by(False)
            self.move_number = 1
        else:
            self.board = parent.board.clone()
            self.captured = parent.captured[:]
            self.set_won_by(parent.get_won_by())
            self.move_number = parent.move_number # not + 1, that will be triggered by a move
            # TODO: copy AI observer manually

    def __repr__(self):
        return "TODO: game representation."
        #return self.game.__repr__()

    def get_board(self):
        return self.board

    def get_move_number(self):
        return self.move_number

    def get_captured(self, player_num):
        return self.captured[player_num]

    # these two should only be used for testing
    def set_move_number(self, turn):
        self.move_number = turn

    def set_captured(self, player_num, pieces):
        self.captured[player_num] = pieces
    
    def make_move(self, move_pos):
        if self.board.off_board(move_pos):
            raise IllegalMoveException("That position is off the board")
        if self.board.get_occ(move_pos) > 0:
            raise IllegalMoveException("That position is already occupied")

        my_colour = self.to_move_colour()
        # Place a stone
        self.move_number += 1
        other_colour = self.to_move_colour()
        self.board.set_occ(move_pos, my_colour)
        board_size = self.board.get_size()

        MC = my_colour
        OC = other_colour

        # Process captures
        for ds in self.board.get_direction_strips():
            captures = ds.get_captures(move_pos, MC)
            for capture_pos in captures:
                # Remove stone
                self.board.set_occ(capture_pos, EMPTY)

                # Keep track of capture count
                self.captured[my_colour] += 1
                if self.captured[my_colour] >= 10:
                    self.set_won_by(MC)

        # OLD. We only need to check for 5 in a row wins if we are not
        # an AI player - the AI data structure detects this already,
        # so it's duplicate work.

        # Check for a win by checking all the lines that run through
        # the move position.
        # We only need to check half of the directions,
        # because for each we need to check the opposite direction
        # in case the last stone was not placed at an end.
        for ds in self.board.get_direction_strips():
            self.check_direction_for_5_in_a_row(ds, move_pos, my_colour)

    def check_direction_for_5_in_a_row(self, ds, move_pos, my_colour):
        s, strip_num = ds.get_strip(move_pos)
        move_ind = ds.get_index(move_pos)
        if s.match_five_in_a_row(move_ind, my_colour):
            self.set_won_by(my_colour)

    def set_won_by(self, wb):
        self._won_by = wb
<commit_msg>Extracted capture processing function<commit_after>
from pente_exceptions import *
from board import *
from pos import *

class GameState():
    """ This is for the state of a game as of a particular move. 
    """
    def __init__(self, game, parent=None):
        self.game = game
        self.parent = parent
        if parent == None:
            self.board = Board(game.size())
            # 3 for convenience, should only use [1] and [2]
            self.captured = [0,0,0]
            self.set_won_by(False)
            self.move_number = 1
        else:
            self.board = parent.board.clone()
            self.captured = parent.captured[:]
            self.set_won_by(parent.get_won_by())
            self.move_number = parent.move_number # not + 1, that will be triggered by a move
            # TODO: copy AI observer manually

    def __repr__(self):
        return "TODO: game representation."
        #return self.game.__repr__()

    def get_board(self):
        return self.board

    def get_move_number(self):
        return self.move_number

    def get_captured(self, player_num):
        return self.captured[player_num]

    # these two should only be used for testing
    def set_move_number(self, turn):
        self.move_number = turn

    def set_captured(self, player_num, pieces):
        self.captured[player_num] = pieces
    
    def make_move(self, move_pos):
        if self.board.off_board(move_pos):
            raise IllegalMoveException("That position is off the board")
        if self.board.get_occ(move_pos) > 0:
            raise IllegalMoveException("That position is already occupied")

        my_colour = self.to_move_colour()
        # Place a stone
        self.move_number += 1
        other_colour = self.to_move_colour()
        self.board.set_occ(move_pos, my_colour)
        board_size = self.board.get_size()

        MC = my_colour
        OC = other_colour

        # Process captures
        for ds in self.board.get_direction_strips():
            self.process_direction_captures(ds, move_pos, MC)

        # OLD. We only need to check for 5 in a row wins if we are not
        # an AI player - the AI data structure detects this already,
        # so it's duplicate work.

        # Check for a win by checking all the lines that run through
        # the move position.
        for ds in self.board.get_direction_strips():
            self.check_direction_for_5_in_a_row(ds, move_pos, my_colour)

    def check_direction_for_5_in_a_row(self, ds, move_pos, my_colour):
        s, strip_num = ds.get_strip(move_pos)
        move_ind = ds.get_index(move_pos)
        if s.match_five_in_a_row(move_ind, my_colour):
            self.set_won_by(my_colour)

    def process_direction_captures(self, ds, move_pos, my_colour):
        captures = ds.get_captures(move_pos, my_colour)
        for capture_pos in captures:
            # Remove stone
            self.board.set_occ(capture_pos, EMPTY)

            # Keep track of capture count
            self.captured[my_colour] += 1
            if self.captured[my_colour] >= 10:
                self.set_won_by(my_colour)

    def set_won_by(self, wb):
        self._won_by = wb

{'fraction_non_alphanumeric': 0.08133374883141166, 'fraction_numerical': 0.004985976939856653, 'mean_word_length': 2.1840277777777777, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '3338555', 'n_tokens_mistral': 2018, 'n_tokens_neox': 1900, 'n_words': 543}