示例#1
0
def test_promotion():
    """
    When pawns reach the back rank they should become queens (currently we don't support under-promotion).
    When other pieces move to the back rank, they should remain as themselves.
    """
    position = empty_position()
    position[(6, 0)] = ChessPiece['WP']  # a7
    position[(1, 4)] = ChessPiece['BP']  # e2
    position[(0, 0)] = ChessPiece['WK']  # a1
    position[(0, 7)] = ChessPiece['BK']  # h1
    position[(5, 5)] = ChessPiece['WB']  # f6
    position[(1, 6)] = ChessPiece['BR']  # g2
    can_castle = {'WKS': False, 'WQS': False, 'BKS': False, 'BQS': False}
    board = ChessBoard(position=position,
                       can_castle=can_castle,
                       last_move=None,
                       player_to_move=Player['W'])

    nb = board.make_move(((6, 0), (7, 0)))  # a8=Q
    assert nb.position[(7, 0)] == ChessPiece['WQ'], "white promotion"

    nb = board.make_move(((5, 5), (7, 3)))  # Bd8
    assert nb.position[(
        7, 3)] == ChessPiece['WB'], "white piece moves to back rank"

    nb2 = nb.make_move(((1, 4), (0, 4)))  # e1=Q
    assert nb2.position[(0, 4)] == ChessPiece['BQ'], "black promotion"

    nb2 = nb.make_move(((1, 6), (0, 6)))  # Rg1
    assert nb2.position[(
        0, 6)] == ChessPiece['BR'], "black piece moves to back rank"
示例#2
0
def test_castling_KS():
    """Test king side castling."""
    b = ChessBoard()
    b = b.make_move(((1, 4), (3, 4)))  # e4
    b = b.make_move(((6, 4), (4, 4)))  # e5
    b = b.make_move(((0, 6), (2, 5)))  # Nf3
    b = b.make_move(((7, 6), (5, 5)))  # Nf6
    b = b.make_move(((0, 5), (3, 2)))  # Bc4
    b = b.make_move(((7, 5), (4, 2)))  # Bc5

    # check white can castle now
    white_castle = ((0, 4), (0, 6))
    legal_moves = b.legal_moves()
    assert white_castle in legal_moves

    b = b.make_move(((0, 4), (1, 4)))  # Ke2

    # check black can castle now
    black_castle = ((7, 4), (7, 6))
    assert (black_castle in b.legal_moves())

    b = b.make_move(((7, 4), (6, 4)))  # Ke7

    b = b.make_move(((1, 4), (0, 4)))  # Ke1
    b = b.make_move(((6, 4), (7, 4)))  # Ke8

    # check white cannot castle
    assert white_castle not in b.legal_moves()

    b = b.make_move(((0, 1), (2, 2)))  # Nc3

    # check black cannot castle
    assert black_castle not in b.legal_moves()
示例#3
0
 def __init__(self, gameEngine):
     """
     Constructor.
     :param gameEngine: The initial game engine.
     """
     self.chessBoard = ChessBoard()
     self.lightBoard = gameEngine.lightBoard
     self.display = gameEngine.display
     self.loopingCall = gameEngine.loopingCall
     self.validMovesCounter = 0
示例#4
0
 def __init__(self, whites, blacks, gameIndex):
     self.game_id = gameIndex
     self.validMovesCounter = 0
     self.white = whites
     self.white.game_id = self.game_id
     self.white.game = self
     self.black = blacks
     self.black.game_id = self.game_id
     self.black.game = self
     self.chessBoard = ChessBoard()
     self.lightBoard = LightBoard()
     self.notifyReady()
示例#5
0
def test_starting_pos_legal_moves():
    """Check that there are 20 legal moves in the starting position"""
    b = ChessBoard()
    lm = b.legal_moves()

    assert len(lm) == 20
示例#6
0
from flask import Flask, request
from chess import Board as ChessBoard
from chess import Piece, QUEEN, Color, WHITE, BLACK

from src.board import init_board, init_gui_board, print_board, game_over
from src.state import State, Turn
from src.checkers import move_function, play_move


BOARD = ChessBoard(None)
init_gui_board(BOARD)
state_board = init_board()

app = Flask(__name__)


def human_move(src, dest):
    piece = BOARD.remove_piece_at(src)
    if dest > 55:
        BOARD.set_piece_at(dest, Piece(QUEEN, Color(WHITE)))
    else:
        BOARD.set_piece_at(dest, piece)

    jump = play_move(state_board, [src, dest], Turn.WHITE)
    if jump is not None:
        BOARD.remove_piece_at(jump)

    print_board(state_board)


def computer_move(move):