From 2c60818327bf5b4dc48d55a702d1ca6d3ac8e5c9 Mon Sep 17 00:00:00 2001 From: Niklas Fiekas Date: Fri, 27 Mar 2015 16:11:43 +0100 Subject: [PATCH] Remove scripts --- scripts/ecotool | 137 ---------------- scripts/python-chess | 367 ------------------------------------------- setup.py | 1 - 3 files changed, 505 deletions(-) delete mode 100755 scripts/ecotool delete mode 100755 scripts/python-chess diff --git a/scripts/ecotool b/scripts/ecotool deleted file mode 100755 index 069a6a05a..000000000 --- a/scripts/ecotool +++ /dev/null @@ -1,137 +0,0 @@ -#!/usr/bin/python -# -*- coding: utf-8 -*- -# -# This file is part of the python-chess library. -# Copyright (C) 2012-2014 Niklas Fiekas -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . - -import argparse -import chess -import collections -import json -import sys - -def read_eco(source): - result = { } - - tokens = collections.deque() - - eco = None - name = None - position = chess.Board() - state = 0 - - for lineno, unstripped_line in enumerate(source): - # Skip emtpy lines and comments. - line = unstripped_line.strip() - if not line or line.startswith("#"): - continue - - # Split line into tokens. - tokens.extend(line.split()) - - # Consume tokens on the fly. - while tokens: - try: - token = tokens.popleft() - - if state == 0: - # State 0: Expecting ECO code. - eco = token - if eco in result: - state = 4 - else: - state = 1 - elif state == 1: - # State 1: Expecting variation name. - if not token.startswith("\""): - name = token - state = 3 - elif not token.endswith("\""): - name = token[1:] - state = 2 - else: - name = token[1:-1] - state = 3 - elif state == 2: - # State 2: Expecting rest of a quoted name. - if not token.endswith("\""): - name += " " + token - else: - name += " " + token[:-1] - state = 3 - elif state == 3: - # State 3: Expecting moves. - if token == "*": - result[eco] = { - "eco": eco, - "fen": position.fen(), - "hash": position.zobrist_hash(), - "name": name, - } - state = 0 - eco = None - name = None - position = chess.Board() - else: - san = token.split(".")[-1] - if san in ["0-0", "o-o"]: - san = "O-O" - elif san in ["0-0-0", "o-o-o"]: - san = "O-O-O" - position.push_san(san) - elif state == 4: - # State 4: Waiting for end of record. - if token == "*": - state = 0 - except: - # Dump last line and token. - sys.stderr.write("Line %d:\n" % (lineno + 1, )) - sys.stderr.write(" ") - sys.stderr.write(unstripped_line) - sys.stderr.write(" ") - sys.stderr.write(" " * unstripped_line.index(token)) - sys.stderr.write("^" * len(token)) - sys.stderr.write("\n") - - # Dump current variables. - sys.stderr.write("State: %d\n" % state) - sys.stderr.write("ECO: %s\n" % eco) - sys.stderr.write("FEN: %s\n" % position.fen()) - sys.stderr.write("Name: %s\n" % name) - sys.stderr.flush() - raise - - - return result - - -if __name__ == "__main__": - parser = argparse.ArgumentParser( - description="Compiles ECO files to JSON for faster lookups.") - - parser.add_argument("source", - type=argparse.FileType("r"), - nargs="?", - default=sys.stdin, - help="The input ECO file.") - - args = parser.parse_args() - - json.dump(read_eco(args.source), sys.stdout, - ensure_ascii=False, - indent=4, - separators=(",", ": "), - sort_keys=True) diff --git a/scripts/python-chess b/scripts/python-chess deleted file mode 100755 index b54e07a7a..000000000 --- a/scripts/python-chess +++ /dev/null @@ -1,367 +0,0 @@ -#!/usr/bin/python -# -*- coding: utf-8 -*- -# -# This file is part of the python-chess library. -# Copyright (C) 2012-2014 Niklas Fiekas -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . - -import sys -import random -import chess -import chess.polyglot - - -def print_bitboard(bitboard): - PIECE_CHARS = [ - [ " ", u"\u2659", u"\u2658", u"\u2657", u"\u2656", u"\u2655", u"\u2654" ], - [ " ", u"\u265F", u"\u265e", u"\u265d", u"\u265c", u"\u265b", u"\u265a" ] - ] - - SEQ_DARK_SQUARE = "\033[0;30;46m" - SEQ_LIGHT_SQUARE = "\033[0;30m" - SEQ_END = "\033[0m" - - for square in chess.SQUARES_180: - if chess.BB_SQUARES[square] & chess.BB_FILE_A: - for border in range(square, square + 8): - if chess.BB_SQUARES[border] & chess.BB_DARK_SQUARES: - sys.stdout.write(SEQ_DARK_SQUARE) - else: - sys.stdout.write(SEQ_LIGHT_SQUARE) - - sys.stdout.write(" ") - sys.stdout.write(SEQ_END) - - sys.stdout.write("\n") - - if chess.BB_SQUARES[square] & chess.BB_DARK_SQUARES: - sys.stdout.write("\033[0;30;46m") - else: - sys.stdout.write("\033[0;30m") - - piece = bitboard.piece_at(square) - if piece: - sys.stdout.write(" ") - sys.stdout.write(PIECE_CHARS[piece.color][piece.piece_type]) - sys.stdout.write(" ") - else: - sys.stdout.write(" ") - - sys.stdout.write("\033[0m") - - if chess.BB_SQUARES[square] & chess.BB_FILE_H: - sys.stdout.write("\n") - - sys.stdout.flush() - - -def minimax_value(bitboard, depth, eval_fn): - if bitboard.halfmove_clock >= 50: - return 0 - - if depth == 0: - if bitboard.is_stalemate(): - return 0 - elif bitboard.is_checkmate(): - return -1000 - - return (1 - bitboard.turn * 2) * eval_fn(bitboard) - - best = None - - for move in bitboard.generate_pseudo_legal_moves(): - bitboard.push(move) - - if not bitboard.was_into_check(): - value = -1 * minimax_value(bitboard, depth - 1, eval_fn) - if best is None or value > best: - best = value - - bitboard.pop() - - if best is None: - if not bitboard.is_check(): - return 0 - else: - return -1000 - - else: - return best - -def minimax(bitboard, depth, eval_fn): - best = None - - for move in bitboard.generate_pseudo_legal_moves(): - bitboard.push(move) - - if not bitboard.was_into_check(): - value = -1 * minimax_value(bitboard, depth, eval_fn) - if best is None or value > best[0]: - best = (value, move) - - bitboard.pop() - - return best - -def alphabeta_value(bitboard, depth, alpha, beta, eval_fn): - if bitboard.halfmove_clock >= 50: - return 0 - - if depth == 0: - if bitboard.is_stalemate(): - return 0 - elif bitboard.is_checkmate(): - return -1000 - - return (1 - bitboard.turn * 2) * eval_fn(bitboard) - - found_legal_move = False - - for move in bitboard.generate_pseudo_legal_moves(): - bitboard.push(move) - - if not bitboard.was_into_check(): - found_legal_move = True - - opp_alpha = None if beta is None else -1 * beta - opp_beta = None if alpha is None else -1 * alpha - - value = -1 * alphabeta_value(bitboard, depth - 1, opp_alpha, opp_beta, eval_fn) - - if alpha is None or value > alpha: - alpha = value - - if (alpha is not None) and (beta is not None) and alpha >= beta: - bitboard.pop() - return beta - - bitboard.pop() - - if not found_legal_move: - if not bitboard.is_check(): - return 0 - else: - return -1000 - else: - return alpha - -def alphabeta(bitboard, depth, eval_fn): - best = None - - for move in bitboard.generate_pseudo_legal_moves(): - bitboard.push(move) - - if not bitboard.was_into_check(): - opp_beta = None if best is None else -1 * best[0] - - value = -1 * alphabeta_value(bitboard, depth, None, opp_beta, eval_fn) - if best is None or value > best[0]: - best = value, move - - bitboard.pop() - - return best - - -def negascout_value(bitboard, depth, alpha, beta, eval_fn): - if depth == 0: - if bitboard.is_stalemate(): - return 0 - elif bitboard.is_checkmate(): - return -1000 - - return (1 - bitboard.turn * 2) * eval_fn(bitboard) - - a = alpha - b = beta - - found_legal_move = False - - for move in bitboard.generate_pseudo_legal_moves(): - bitboard.push(move) - - if not bitboard.was_into_check(): - t = -negascout_value(bitboard, depth - 1, -b, -a, eval_fn) - if t > a and t < beta and found_legal_move and depth > 1: - a = -negascout_value(bitboard, depth - 1, -beta, -t, eval_fn) - - found_legal_move = True - - a = max(a, t) - - if a >= beta: - bitboard.pop() - return a - - b = a + 0.1 - - bitboard.pop() - - if not found_legal_move: - if not bitboard.is_check(): - return 0 - else: - return -1000 - else: - return a - -def negascout(bitboard, depth, eval_fn): - best = None - - for move in bitboard.generate_pseudo_legal_moves(): - bitboard.push(move) - - if not bitboard.was_into_check(): - value = -1 * negascout_value(bitboard, depth, -3000, 3000, eval_fn) - if best is None or value > best[0]: - best = value, move - - bitboard.pop() - - return best - - -def material_evaluator(bitboard): - value = 0.0 - - value += chess.pop_count(bitboard.pawns & bitboard.occupied_co[chess.WHITE]) - value -= chess.pop_count(bitboard.pawns & bitboard.occupied_co[chess.BLACK]) - - value += 3.0 * chess.sparse_pop_count(bitboard.knights & bitboard.occupied_co[chess.WHITE]) - value -= 3.0 * chess.sparse_pop_count(bitboard.knights & bitboard.occupied_co[chess.BLACK]) - - value += 3.1 * chess.sparse_pop_count(bitboard.bishops & bitboard.occupied_co[chess.WHITE]) - value -= 3.1 * chess.sparse_pop_count(bitboard.bishops & bitboard.occupied_co[chess.BLACK]) - - value += 5.0 * chess.sparse_pop_count(bitboard.rooks & bitboard.occupied_co[chess.WHITE]) - value -= 5.0 * chess.sparse_pop_count(bitboard.rooks & bitboard.occupied_co[chess.BLACK]) - - value += 9.0 * chess.sparse_pop_count(bitboard.queens & bitboard.occupied_co[chess.WHITE]) - value -= 9.0 * chess.sparse_pop_count(bitboard.queens & bitboard.occupied_co[chess.BLACK]) - - return value - -def mobility_evaluator(bitboard): - value = 0.0 - - turn = bitboard.turn - - bitboard.turn = chess.WHITE - value += 0.02 * bitboard.pseudo_legal_move_count() - - bitboard.turn = chess.BLACK - value -= 0.02 * bitboard.pseudo_legal_move_count() - - bitboard.turn = turn - - return value - - -if __name__ == "__main__": - sys.stdout.write("python-chess {0} by {1} <{2}>\n".format(chess.__version__, chess.__author__, chess.__email__)) - - log = open("log.txt", "a") - - book = None - try: - book = chess.polyglot.open_reader("data/opening-books/performance.bin") - except IOError: - pass - - position = chess.Board() - - while True: - line = sys.stdin.readline() - log.write(line) - log.flush() - line = line.strip() - if not line: - continue - - if line == "uci": - # Initialize. - sys.stdout.write("id name python-chess {0}\n".format(chess.__version__)) - sys.stdout.write("id author {0}\n".format(chess.__author__)) - sys.stdout.write("uciok\n") - sys.stdout.flush() - elif line == "isready": - # Always ready. - sys.stdout.write("readyok\n") - sys.stdout.flush() - elif line == "ucinewgame": - # No special setup for a new game. - continue - elif line == "quit": - # Quit. - sys.exit(0) - elif line == "print": - print_bitboard(position) - print(position) - else: - parts = line.split() - command = parts.pop(0) - - if command == "position": - # Setup a position. - while parts: - part = parts.pop(0) - if part == "startpos": - position.reset() - elif part == "moves": - while parts: - position.push(chess.Move.from_uci(parts.pop(0))) - break - elif len(parts) >= 6: - position.set_fen(" ".join([part, parts.pop(0), parts.pop(0), parts.pop(0), parts.pop(0), parts.pop(0)])) - else: - position.reset() - elif command == "go": - # Do not ponder. - if "ponder" in parts: - continue - - best_move = None - - # Consider the opening book. - if book and False: - choices = [ (entry.weight, entry.move()) for entry in book.get_entries_for_position(position) ] - total = sum(weight for weight, move in choices) - - rand = random.randint(0, total) - upto = 0 - - for weight, move in choices: - if upto + weight > rand or True: - best_move = move - break - upto += weight - - if not best_move: - book = None - - # Search. - if not best_move: - best = negascout(position, 3, lambda b: material_evaluator(b)) - if best: - value, best_move = best - - # Output. - if best_move: - sys.stdout.write("bestmove {0}\n".format(best_move.uci())) - sys.stdout.flush() - else: - # Unknown command. - sys.stderr.write("Unknown command: {0}\n".format(line)) - sys.stderr.flush() diff --git a/setup.py b/setup.py index 575a56175..e51c8fb88 100755 --- a/setup.py +++ b/setup.py @@ -57,7 +57,6 @@ def read_description(): keywords="chess fen pgn polyglot syzygy uci", url="https://github.com/niklasf/python-chess", packages=["chess"], - scripts=["scripts/ecotool", "scripts/python-chess"], test_suite="test", classifiers=[ "Development Status :: 2 - Pre-Alpha",