<commit_before># -*- coding: utf-8 -*-
# Copyright © 2013 <NAME>, <NAME>

import copy
import sys
import random
from Board import Board
from random import choice
import Player

random.seed(0)

class NegamaxPruningPlayer(Player.Player):
    def generate_move(self, game):
        """
        Generate and return a move for the current turn color.
        The caller ensures that a legal move exists.
        Takes a move based on a search down a few moves.
        """
        # negamax it!
        a = -500000
        b = -a
        value, move = self.negamax(game, 5, a, b)
        print("Value:", value)
        return move

    def negamax(self, state, max_depth, alpha, beta):
        # base case
        if max_depth <= 0:
            return (state.score(), None)

        # recursive case
        legal_moves = state.legal_moves()
        best_value = -sys.maxsize
        
        for move in legal_moves:
            newstate = Board.from_other(state)
            result = newstate.move(move)
            if result in ('W','B'):
                value = -newstate.score()
            elif result == '=':
                value = 0
            else:
                value = -self.negamax(newstate, max_depth-1, -beta, -alpha)[0]

            if value > beta:
                return (value, None)
            if value > alpha:
                alpha = value

            if value > best_value:
                best_value = value
                best_move = move
            elif value == best_value:
                if random.choice((True,False)):
                    best_value = value<commit_msg>performance improvement: pre-sort the moves to allow better pruning!<commit_after># -*- coding: utf-8 -*-
# Copyright © 2013 <NAME>, <NAME>

import copy
import sys
import random
from Board import Board
from random import choice
import Player

random.seed(0)

class NegamaxPruningPlayer(Player.Player):
    def generate_move(self, game):
        """
        Generate and return a move for the current turn color.
        The caller ensures that a legal move exists.
        Takes a move based on a search down a few moves.
        """
        # negamax it!
        a = -500000
        b = -a
        value, move = self.negamax(game, 5, a, b)
        print("Value:", value)
        return move

    def negamax(self, state, max_depth, alpha, beta):
        # base case
        if max_depth <= 0:
            return (state.score(), None)

        # recursive case
        legal_moves = state.legal_moves()
        
        # pre sort the moves! best first!
        sorted_moves = []
        for move in legal_moves:
            score = state.score_after(move)
            sorted_moves.append((score, move))

        # sort the moves on their score
        sorted_moves = sorted(sorted_moves, key=lambda t: t[0])

        best_value = -sys.maxsize
         
        for value, move in sorted_moves:
            newstate = Board.from_other(state)
            result = newstate.move(move)
            if result in ('W','B'):
                value = -newstate.score()
            elif result == '=':
                value = 0
            else:
                value = -self.negamax(newstate, max_depth-1, -beta, -alpha)[0]

            if value > beta:
                return (value, None)
            if value > alpha:
                alpha = value

            if value > best_value:
                best_value = value
                best_move = move
            elif value == best_value:
                if random.choice((True,False)):
                    best_value = value
{'fraction_non_alphanumeric': 0.09241108933071969, 'fraction_numerical': 0.009801176141136937, 'mean_word_length': 1.5978181818181818, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, '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': '2229064', 'n_tokens_mistral': 1024, 'n_tokens_neox': 971, 'n_words': 322}