Esempio n. 1
0
 def __init__(self, level):
     self.ui_board = Board(level)
     self.board = chess.Board()
     self.parser = FenParser(self.board.fen())
     self.engine = Engine(level)  # Engine you're playing against.
     self.hint_engine = Engine(8)  # Engine used to help give you hints.
     self.board.san_move_stack_white = []
     self.board.san_move_stack_black = []
     self.board.help_engine_hint = None
Esempio n. 2
0
class Client(object):
    BACK = 'back'
    HINT = 'hint'

    def __init__(self, level, play_as):
        self.ui_board = Board(level, play_as)
        self.play_as = play_as
        self.board = chess.Board()
        self.parser = FenParser(self.board.fen())
        self.engine = Engine(level)  # Engine you're playing against.
        self.hint_engine = Engine(8)  # Engine used to help give you hints.
        self.board.san_move_stack_white = []
        self.board.san_move_stack_black = []
        self.board.help_engine_hint = None

    def run(self):
        try:
            while True:
                self.check_game_over()
                if self.is_user_move():
                    self.make_turn()
                else:
                    self.computer_turn()
        except BlackWinsException:
            self.ui_board.generate(self.fen(), self.board, self.engine,
                                   GameOver.BLACK_WINS)
        except WhiteWinsException:
            self.ui_board.generate(self.fen(), self.board, self.engine,
                                   GameOver.WHITE_WINS)
        except DrawException:
            self.ui_board.generate(self.fen(), self.board, self.engine,
                                   GameOver.DRAW)
        except ResignException:
            self.ui_board.generate(self.fen(), self.board, self.engine,
                                   GameOver.RESIGN)
        finally:
            self.engine.done()
            self.hint_engine.done()

    def check_game_over(self):
        if self.board.is_game_over():
            result = self.board.result()
            if result == '1-0':
                raise WhiteWinsException
            if result == '0-1':
                raise BlackWinsException
            if result == '1/2-1/2':
                raise DrawException

    def moves(self):
        return map(self.board.san, self.board.legal_moves)

    def closest_move(self, illegal_move):
        for move in self.moves():
            distance = editdistance.eval(move, illegal_move)
            if distance <= 1:
                return move
        return None

    def make_turn(self, meta=(False, None)):
        (failed, prev_move) = meta
        self.ui_board.generate(self.fen(), self.board, self.engine)
        if failed:
            if prev_move == self.BACK:
                print(
                    '{}{}  ⃠ You cannot go back, no moves were made.{}'.format(
                        Styles.PADDING_SMALL, Colors.RED, Colors.RESET))
            else:
                maybe_move = self.closest_move(prev_move)
                if maybe_move is not None:
                    error_string = '{}{}  ⃠ Illegal, did you mean {}{}{}{}{}'.format(
                      Colors.RED, Styles.PADDING_SMALL,\
                      Colors.WHITE, Colors.UNDERLINE, Colors.BOLD, maybe_move, Colors.RESET
                    )
                else:
                    error_string = '{}{}  ⃠ Illegal, try again.'.format(
                        Styles.PADDING_SMALL, Colors.RED)
                print(error_string)
        else:
            print('')
        try:
            move = input('{}{}{}┏━ Your move ━━━━━━━━━━━┓ \n{}┗{}{}'.format(
              Styles.PADDING_SMALL, Colors.WHITE, Colors.BOLD,\
              Styles.PADDING_SMALL, Styles.PADDING_SMALL, Colors.RESET)
            )
            if move == self.BACK:
                self.board.pop()
                self.board.pop()
            elif move == self.HINT:
                hint = self.hint_engine.play(self.board, 1.000)
                self.board.help_engine_hint = self.board.uci(hint.move)
            else:
                s = self.board.parse_san(move)
                if self.play_as == chess.WHITE:
                    self.board.san_move_stack_white.append(self.board.san(s))
                else:
                    self.board.san_move_stack_black.append(self.board.san(s))
                self.board.push_san(move)
                self.board.help_engine_hint = None  # Reset hint if you've made your move.
        except ValueError:
            self.board.help_engine_hint = None  # Reset hint if you wanna dismiss it by invalid moving.
            self.make_turn((True, move))
        except IndexError:
            self.make_turn((True, move))
        except:
            raise ResignException

    def computer_turn(self):
        self.ui_board.generate(self.fen(), self.board, self.engine)
        print('\n{}{}{}┏━ Opponent\'s move ━━━━━┓ \n{}┗{}{}{}thinking...{}'.format(
          Styles.PADDING_SMALL, Colors.WHITE, Colors.BOLD,\
          Styles.PADDING_SMALL, Styles.PADDING_SMALL, Colors.RESET, Colors.GRAY, Colors.RESET)
        )
        result = self.engine.play(self.board)
        if self.play_as == chess.WHITE:
            self.board.san_move_stack_black.append(self.board.san(result.move))
        else:
            self.board.san_move_stack_white.append(self.board.san(result.move))
        self.board.push(result.move)

    def fen(self):
        return self.board.fen()

    def is_user_move(self):
        to_move = self.parser.get_to_move(self.fen())
        if self.play_as == chess.WHITE:
            return to_move == 'w'
        else:
            return to_move == 'b'
Esempio n. 3
0
import os
import subprocess

from chs.ui.board import Board
from tests.framework.base_command_test_case import BaseCommandTestCase

ui = Board(1)


class TestInit(BaseCommandTestCase):
    def test_no_captured_pieces(self):
        fen = 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR'
        (w, b) = ui._get_captured_pieces(fen)
        self.assertEqual(w, '', 'Failed on no white pieces captured.')
        self.assertEqual(b, '', 'Failed on no black pieces captured.')

    def test_one_captured_pawn(self):
        fen = 'rnbqkbnr/pppp1ppp/8/8/8/8/PPP1PPPP/RNBQKBNR'
        (w, b) = ui._get_captured_pieces(fen)
        self.assertEqual(w, 'P', 'Failed on one captured white pawn.')
        self.assertEqual(b, 'p', 'Failed on one captured black pawn.')

    def test_multi_captured_pawns(self):
        fen = 'rnbqkbnr/p4ppp/8/2P1p3/8/8/PP5P/RNBQKBNR'
        (w, b) = ui._get_captured_pieces(fen)
        self.assertEqual(w, 'PPPP', 'Failed on multiple captured white pawns.')
        self.assertEqual(b, 'ppp', 'Failed on multiple captured black pawns.')

    def test_one_captured_knight(self):
        fen = 'r1bqkbnr/pppppppp/8/8/8/8/PPPPPPPP/R1BQKBNR'
        (w, b) = ui._get_captured_pieces(fen)
Esempio n. 4
0
import os
import subprocess
import chess

from chs.ui.board import Board
from tests.framework.base_command_test_case import BaseCommandTestCase

ui = Board(1, chess.WHITE)


class TestInit(BaseCommandTestCase):
    def test_no_captured_pieces(self):
        fen = 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR'
        (w, b) = ui._get_captured_pieces(fen)
        self.assertEqual(w, '', 'Failed on no white pieces captured.')
        self.assertEqual(b, '', 'Failed on no black pieces captured.')

    def test_one_captured_pawn(self):
        fen = 'rnbqkbnr/pppp1ppp/8/8/8/8/PPP1PPPP/RNBQKBNR'
        (w, b) = ui._get_captured_pieces(fen)
        self.assertEqual(w, 'P', 'Failed on one captured white pawn.')
        self.assertEqual(b, 'p', 'Failed on one captured black pawn.')

    def test_multi_captured_pawns(self):
        fen = 'rnbqkbnr/p4ppp/8/2P1p3/8/8/PP5P/RNBQKBNR'
        (w, b) = ui._get_captured_pieces(fen)
        self.assertEqual(w, 'PPPP', 'Failed on multiple captured white pawns.')
        self.assertEqual(b, 'ppp', 'Failed on multiple captured black pawns.')

    def test_one_captured_knight(self):
        fen = 'r1bqkbnr/pppppppp/8/8/8/8/PPPPPPPP/R1BQKBNR'